tf.compat.v1.variable_creator_scope
Stay organized with collections
Save and categorize content based on your preferences.
Scope which defines a variable creation function to be used by variable().
@tf_contextlib.contextmanager
tf.compat.v1.variable_creator_scope(
variable_creator
)
variable_creator is expected to be a function with the following signature:
def variable_creator(next_creator, **kwargs)
The creator is supposed to eventually call the next_creator to create a
variable if it does want to create a variable and not call Variable or
ResourceVariable directly. This helps make creators composable. A creator may
choose to create multiple variables, return already existing variables, or
simply register that a variable was created and defer to the next creators in
line. Creators can also modify the keyword arguments seen by the next
creators.
Custom getters in the variable scope will eventually resolve down to these
custom creators when they do create variables.
The valid keyword arguments in kwds are:
- initial_value: A
Tensor
, or Python object convertible to a Tensor
,
which is the initial value for the Variable. The initial value must have
a shape specified unless validate_shape
is set to False. Can also be a
callable with no argument that returns the initial value when called. In
that case, dtype
must be specified. (Note that initializer functions
from init_ops.py must first be bound to a shape before being used here.)
- trainable: If
True
, the default, also adds the variable to the graph
collection GraphKeys.TRAINABLE_VARIABLES
. This collection is used as
the default list of variables to use by the Optimizer
classes.
trainable
defaults to True
, unless synchronization
is
set to ON_READ
, in which case it defaults to False
.
- collections: List of graph collections keys. The new variable is added to
these collections. Defaults to
[GraphKeys.GLOBAL_VARIABLES]
.
- validate_shape: If
False
, allows the variable to be initialized with a
value of unknown shape. If True
, the default, the shape of
initial_value
must be known.
- caching_device: Optional device string describing where the Variable
should be cached for reading. Defaults to the Variable's device.
If not
None
, caches on another device. Typical use is to cache
on the device where the Ops using the Variable reside, to deduplicate
copying through Switch
and other conditional statements.
- name: Optional name for the variable. Defaults to
'Variable'
and gets
uniquified automatically.
- dtype: If set, initial_value will be converted to the given type.
If
None
, either the datatype will be kept (if initial_value
is
a Tensor), or convert_to_tensor
will decide.
- constraint: A constraint function to be applied to the variable after
updates by some algorithms.
- use_resource: if True, a ResourceVariable is always created.
- synchronization: Indicates when a distributed a variable will be
aggregated. Accepted values are constants defined in the class
tf.VariableSynchronization
. By default the synchronization is set to
AUTO
and the current DistributionStrategy
chooses
when to synchronize.
- aggregation: Indicates how a distributed variable will be aggregated.
Accepted values are constants defined in the class
tf.VariableAggregation
.
This set may grow over time, so it's important the signature of creators is as
mentioned above.
Args |
variable_creator
|
the passed creator
|
Yields |
A scope in which the creator is active
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.variable_creator_scope\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/variable_scope.py#L2643-L2715) |\n\nScope which defines a variable creation function to be used by variable(). \n\n @tf_contextlib.contextmanager\n tf.compat.v1.variable_creator_scope(\n variable_creator\n )\n\nvariable_creator is expected to be a function with the following signature: \n\n def variable_creator(next_creator, **kwargs)\n\nThe creator is supposed to eventually call the next_creator to create a\nvariable if it does want to create a variable and not call Variable or\nResourceVariable directly. This helps make creators composable. A creator may\nchoose to create multiple variables, return already existing variables, or\nsimply register that a variable was created and defer to the next creators in\nline. Creators can also modify the keyword arguments seen by the next\ncreators.\n\nCustom getters in the variable scope will eventually resolve down to these\ncustom creators when they do create variables.\n\nThe valid keyword arguments in kwds are:\n\n- initial_value: A `Tensor`, or Python object convertible to a `Tensor`, which is the initial value for the Variable. The initial value must have a shape specified unless `validate_shape` is set to False. Can also be a callable with no argument that returns the initial value when called. In that case, `dtype` must be specified. (Note that initializer functions from init_ops.py must first be bound to a shape before being used here.)\n- trainable: If `True`, the default, also adds the variable to the graph collection `GraphKeys.TRAINABLE_VARIABLES`. This collection is used as the default list of variables to use by the `Optimizer` classes. `trainable` defaults to `True`, unless `synchronization` is set to `ON_READ`, in which case it defaults to `False`.\n- collections: List of graph collections keys. The new variable is added to these collections. Defaults to `[GraphKeys.GLOBAL_VARIABLES]`.\n- validate_shape: If `False`, allows the variable to be initialized with a value of unknown shape. If `True`, the default, the shape of `initial_value` must be known.\n- caching_device: Optional device string describing where the Variable should be cached for reading. Defaults to the Variable's device. If not `None`, caches on another device. Typical use is to cache on the device where the Ops using the Variable reside, to deduplicate copying through `Switch` and other conditional statements.\n- name: Optional name for the variable. Defaults to `'Variable'` and gets uniquified automatically.\n- dtype: If set, initial_value will be converted to the given type. If `None`, either the datatype will be kept (if `initial_value` is a Tensor), or `convert_to_tensor` will decide.\n- constraint: A constraint function to be applied to the variable after updates by some algorithms.\n- use_resource: if True, a ResourceVariable is always created.\n- synchronization: Indicates when a distributed a variable will be aggregated. Accepted values are constants defined in the class [`tf.VariableSynchronization`](../../../tf/VariableSynchronization). By default the synchronization is set to `AUTO` and the current `DistributionStrategy` chooses when to synchronize.\n- aggregation: Indicates how a distributed variable will be aggregated. Accepted values are constants defined in the class [`tf.VariableAggregation`](../../../tf/VariableAggregation).\n\nThis set may grow over time, so it's important the signature of creators is as\nmentioned above.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------|--------------------|\n| `variable_creator` | the passed creator |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Yields ------ ||\n|---|---|\n| A scope in which the creator is active ||\n\n\u003cbr /\u003e"]]