Gets an existing variable with these parameters or creates a new one. Args: name: the name of the new or existing variable. shape: shape of the new or existing variable. dtype: type of the new or existing variable (defaults to `DT_FLOAT`). initializer: initializer for the variable
(name,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
trainable=True,
collections=None,
caching_device=None,
device=None,
partitioner=None,
custom_getter=None,
use_resource=None,
synchronization=variables.VariableSynchronization.AUTO,
aggregation=variables.VariableAggregation.NONE)
| 208 | |
| 209 | @contrib_add_arg_scope |
| 210 | def variable(name, |
| 211 | shape=None, |
| 212 | dtype=None, |
| 213 | initializer=None, |
| 214 | regularizer=None, |
| 215 | trainable=True, |
| 216 | collections=None, |
| 217 | caching_device=None, |
| 218 | device=None, |
| 219 | partitioner=None, |
| 220 | custom_getter=None, |
| 221 | use_resource=None, |
| 222 | synchronization=variables.VariableSynchronization.AUTO, |
| 223 | aggregation=variables.VariableAggregation.NONE): |
| 224 | """Gets an existing variable with these parameters or creates a new one. |
| 225 | |
| 226 | Args: |
| 227 | name: the name of the new or existing variable. |
| 228 | shape: shape of the new or existing variable. |
| 229 | dtype: type of the new or existing variable (defaults to `DT_FLOAT`). |
| 230 | initializer: initializer for the variable if one is created. |
| 231 | regularizer: a (Tensor -> Tensor or None) function; the result of applying |
| 232 | it on a newly created variable will be added to the collection |
| 233 | GraphKeys.REGULARIZATION_LOSSES and can be used for regularization. |
| 234 | trainable: If `True` also add the variable to the graph collection |
| 235 | `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`). |
| 236 | collections: A list of collection names to which the Variable will be added. |
| 237 | If None it would default to `tf.GraphKeys.GLOBAL_VARIABLES`. |
| 238 | caching_device: Optional device string or function describing where the |
| 239 | Variable should be cached for reading. Defaults to the Variable's device. |
| 240 | device: Optional device to place the variable. It can be an string or a |
| 241 | function that is called to get the device for the variable. |
| 242 | partitioner: Optional callable that accepts a fully defined `TensorShape` |
| 243 | and dtype of the `Variable` to be created, and returns a list of |
| 244 | partitions for each axis (currently only one axis can be partitioned). |
| 245 | custom_getter: Callable that allows overwriting the internal get_variable |
| 246 | method and has to have the same signature. |
| 247 | use_resource: If `True` use a ResourceVariable instead of a Variable. |
| 248 | synchronization: Indicates when a distributed a variable will be aggregated. |
| 249 | Accepted values are constants defined in the class |
| 250 | `tf.VariableSynchronization`. By default the synchronization is set to |
| 251 | `AUTO` and the current `DistributionStrategy` chooses when to synchronize. |
| 252 | aggregation: Indicates how a distributed variable will be aggregated. |
| 253 | Accepted values are constants defined in the class |
| 254 | `tf.VariableAggregation`. |
| 255 | |
| 256 | Returns: |
| 257 | The created or existing variable. |
| 258 | """ |
| 259 | collections = list(collections if collections is not None else |
| 260 | [ops.GraphKeys.GLOBAL_VARIABLES]) |
| 261 | |
| 262 | # Remove duplicates |
| 263 | collections = list(set(collections)) |
| 264 | getter = variable_scope.get_variable |
| 265 | if custom_getter is not None: |
| 266 | getter = functools.partial( |
| 267 | custom_getter, reuse=variable_scope.get_variable_scope().reuse) |
no test coverage detected