Gets an existing variable with this name or create a new one.
(self,
var_store,
name,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
reuse=None,
trainable=None,
collections=None,
caching_device=None,
partitioner=None,
validate_shape=True,
use_resource=None,
custom_getter=None,
constraint=None,
synchronization=VariableSynchronization.AUTO,
aggregation=VariableAggregation.NONE)
| 1453 | return self.get_collection(ops.GraphKeys.LOCAL_VARIABLES) |
| 1454 | |
| 1455 | def get_variable(self, |
| 1456 | var_store, |
| 1457 | name, |
| 1458 | shape=None, |
| 1459 | dtype=None, |
| 1460 | initializer=None, |
| 1461 | regularizer=None, |
| 1462 | reuse=None, |
| 1463 | trainable=None, |
| 1464 | collections=None, |
| 1465 | caching_device=None, |
| 1466 | partitioner=None, |
| 1467 | validate_shape=True, |
| 1468 | use_resource=None, |
| 1469 | custom_getter=None, |
| 1470 | constraint=None, |
| 1471 | synchronization=VariableSynchronization.AUTO, |
| 1472 | aggregation=VariableAggregation.NONE): |
| 1473 | """Gets an existing variable with this name or create a new one.""" |
| 1474 | if regularizer is None: |
| 1475 | regularizer = self._regularizer |
| 1476 | if caching_device is None: |
| 1477 | caching_device = self._caching_device |
| 1478 | if partitioner is None: |
| 1479 | partitioner = self._partitioner |
| 1480 | if custom_getter is None: |
| 1481 | custom_getter = self._custom_getter |
| 1482 | if context.executing_eagerly(): |
| 1483 | reuse = False |
| 1484 | use_resource = True |
| 1485 | else: |
| 1486 | if reuse is None: |
| 1487 | reuse = self._reuse |
| 1488 | if use_resource is None: |
| 1489 | use_resource = self._use_resource |
| 1490 | |
| 1491 | full_name = self.name + "/" + name if self.name else name |
| 1492 | # Variable names only depend on variable_scope (full_name here), |
| 1493 | # not name_scope, so we reset it below for the time of variable creation. |
| 1494 | with ops.name_scope(None): |
| 1495 | # Check that `initializer` dtype and `dtype` are consistent before |
| 1496 | # replacing them with defaults. |
| 1497 | if (dtype is not None and initializer is not None and |
| 1498 | not callable(initializer)): |
| 1499 | init_dtype = ops.convert_to_tensor(initializer).dtype.base_dtype |
| 1500 | if init_dtype != dtype: |
| 1501 | raise ValueError("Initializer type '%s' and explicit dtype '%s' " |
| 1502 | "don't match." % (init_dtype, dtype)) |
| 1503 | if initializer is None: |
| 1504 | initializer = self._initializer |
| 1505 | if constraint is None: |
| 1506 | constraint = self._constraint |
| 1507 | if dtype is None: |
| 1508 | dtype = self._dtype |
| 1509 | return var_store.get_variable( |
| 1510 | full_name, |
| 1511 | shape=shape, |
| 1512 | dtype=dtype, |