LostTech.TensorFlow : API Documentation

Type Variable

Namespace tensorflow

Parent Variable

Interfaces IVariable

See the [Variables Guide](https://tensorflow.org/guide/variables).

A variable maintains state in the graph across calls to `run()`. You add a variable to the graph by constructing an instance of the class `Variable`.

The `Variable()` constructor requires an initial value for the variable, which can be a `Tensor` of any type and shape. The initial value defines the type and shape of the variable. After construction, the type and shape of the variable are fixed. The value can be changed using one of the assign methods.

If you want to change the shape of a variable later you have to use an `assign` Op with `validate_shape=False`.

Just like any `Tensor`, variables created with `Variable()` can be used as inputs for other Ops in the graph. Additionally, all the operators overloaded for the `Tensor` class are carried over to variables, so you can also add nodes to the graph by just doing arithmetic on variables. When you launch the graph, variables have to be explicitly initialized before you can run Ops that use their value. You can initialize a variable by running its *initializer op*, restoring the variable from a save file, or simply running an `assign` Op that assigns a value to the variable. In fact, the variable *initializer op* is just an `assign` Op that assigns the variable's initial value to the variable itself. The most common initialization pattern is to use the convenience function `global_variables_initializer()` to add an Op to the graph that initializes all the variables. You then run that Op after launching the graph. If you need to create a variable with an initial value dependent on another variable, use the other variable's `initialized_value()`. This ensures that variables are initialized in the right order.

All variables are automatically collected in the graph where they are created. By default, the constructor adds the new variable to the graph collection `GraphKeys.GLOBAL_VARIABLES`. The convenience function `global_variables()` returns the contents of that collection.

When building a machine learning model it is often convenient to distinguish between variables holding the trainable model parameters and other variables such as a `global step` variable used to count training steps. To make this easier, the variable constructor supports a `trainable=` parameter. If `True`, the new variable is also added to the graph collection `GraphKeys.TRAINABLE_VARIABLES`. The convenience function `trainable_variables()` returns the contents of this collection. The various `Optimizer` classes use this collection as the default list of variables to optimize.

WARNING: tf.Variable objects by default have a non-intuitive memory model. A Variable is represented internally as a mutable Tensor which can non-deterministically alias other Tensors in a graph. The set of operations which consume a Variable and can lead to aliasing is undetermined and can change across TensorFlow versions. Avoid writing code which relies on the value of a Variable either changing or not changing as other operations happen. For example, using Variable objects or simple functions thereof as predicates in a tf.cond is dangerous and error-prone:

``` v = tf.Variable(True) tf.cond(v, lambda: v.assign(False), my_false_fn) # Note: this is broken. ```

Here, adding `use_resource=True` when constructing the variable will fix any nondeterminism issues: ``` v = tf.Variable(True, use_resource=True) tf.cond(v, lambda: v.assign(False), my_false_fn) ```

To use the replacement for variables which does not have these issues:

* Add `use_resource=True` when constructing tf.Variable; * Call `tf.compat.v1.get_variable_scope().set_use_resource(True)` inside a `tf.compat.v1.variable_scope` before the `tf.compat.v1.get_variable()` call.
Show Example
import tensorflow as tf 

# Create a variable. w = tf.Variable(, name=)

# Use the variable in the graph like any Tensor. y = tf.matmul(w,...another variable or tensor...)

# The overloaded operators are available too. z = tf.sigmoid(w + y)

# Assign a new value to the variable with `assign()` or a related method. w.assign(w + 1.0) w.assign_add(1.0)

Methods

Properties

Public instance methods

Tensor assign(IGraphNodeBase value, Nullable<bool> use_locking, string name, bool read_value)

Tensor assign_add(IGraphNodeBase delta, Nullable<bool> use_locking, string name, bool read_value)

object assign_add_dyn(object delta, ImplicitContainer<T> use_locking, object name, ImplicitContainer<T> read_value)

object assign_dyn(object value, ImplicitContainer<T> use_locking, object name, ImplicitContainer<T> read_value)

Tensor assign_sub(IGraphNodeBase delta, Nullable<bool> use_locking, string name, bool read_value)

object assign_sub_dyn(object delta, ImplicitContainer<T> use_locking, object name, ImplicitContainer<T> read_value)

Tensor batch_scatter_update(IndexedSlices sparse_delta, bool use_locking, string name)

object batch_scatter_update_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor count_up_to(Nullable<int> limit)

object count_up_to_dyn(object limit)

object eval(BaseSession session)

object eval(IDictionary<object, object> session)

object eval_dyn(object session)

Evaluates the value of a variable.
Returns
object
A Numpy array.

Examples: ```python >>> from keras import backend as K >>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32') >>> K.eval(kvar) array([[ 1., 2.], [ 3., 4.]], dtype=float32) ```

Tensor gather_nd(IGraphNodeBase indices, string name)

object gather_nd_dyn(object indices, object name)

Tensor initialized_value()

object initialized_value_dyn()

void load(ndarray value, object session)

void load(int value, object session)

object load_dyn(object value, object session)

Tensor read_value()

object read_value_dyn()

Tensor scatter_add(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_add_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor scatter_div(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_div_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor scatter_max(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_max_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor scatter_min(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_min_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor scatter_mul(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_mul_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor scatter_nd_add(object indices, object updates, string name)

object scatter_nd_add_dyn(object indices, object updates, object name)

Tensor scatter_nd_sub(object indices, object updates, string name)

object scatter_nd_sub_dyn(object indices, object updates, object name)

Tensor scatter_nd_update(object indices, object updates, string name)

object scatter_nd_update_dyn(object indices, object updates, object name)

Tensor scatter_sub(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_sub_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

Tensor scatter_update(IndexedSlices sparse_delta, bool use_locking, string name)

object scatter_update_dyn(object sparse_delta, ImplicitContainer<T> use_locking, object name)

object set_shape_dyn_(object shape)

Tensor sparse_read(object indices, string name)

object sparse_read_dyn(object indices, object name)

Tensor value()

object value_dyn()

Public static methods

Variable NewDyn(object initial_value, object trainable, object collections, ImplicitContainer<T> validate_shape, object caching_device, object name, object variable_def, object dtype, object expected_shape, object import_scope, object constraint, object use_resource, ImplicitContainer<T> synchronization, ImplicitContainer<T> aggregation, object shape)

Creates a new variable with value `initial_value`.

The new variable is added to the graph collections listed in `collections`, which defaults to `[GraphKeys.GLOBAL_VARIABLES]`.

If `trainable` is `True` the variable is also added to the graph collection `GraphKeys.TRAINABLE_VARIABLES`.

This constructor creates both a `variable` Op and an `assign` Op to set the variable to its initial value.
Parameters
object 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.)
object trainable
If `True`, 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. Defaults to `True`, unless `synchronization` is set to `ON_READ`, in which case it defaults to `False`.
object collections
List of graph collections keys. The new variable is added to these collections. Defaults to `[GraphKeys.GLOBAL_VARIABLES]`.
ImplicitContainer<T> 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.
object 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.
object name
Optional name for the variable. Defaults to `'Variable'` and gets uniquified automatically.
object variable_def
`VariableDef` protocol buffer. If not `None`, recreates the Variable object with its contents, referencing the variable's nodes in the graph, which must already exist. The graph is not changed. `variable_def` and the other arguments are mutually exclusive.
object 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.
object expected_shape
A TensorShape. If set, initial_value is expected to have this shape.
object import_scope
Optional `string`. Name scope to add to the `Variable.` Only used when initializing from protocol buffer.
object constraint
An optional projection function to be applied to the variable after being updated by an `Optimizer` (e.g. used to implement norm constraints or value constraints for layer weights). The function must take as input the unprojected Tensor representing the value of the variable and return the Tensor for the projected value (which must have the same shape). Constraints are not safe to use when doing asynchronous distributed training.
object use_resource
whether to use resource variables.
ImplicitContainer<T> 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.
ImplicitContainer<T> aggregation
Indicates how a distributed variable will be aggregated. Accepted values are constants defined in the class tf.VariableAggregation.
object shape
(optional) The shape of this variable. If None, the shape of `initial_value` will be used. When setting this argument to `tf.TensorShape(None)` (representing an unspecified shape), the variable can be assigned with values of different shapes.

Public properties

object aggregation get;

object aggregation_dyn get;

object constraint get;

object constraint_dyn get;

object device get;

object device_dyn get;

object dtype get;

object dtype_dyn get;

object graph get;

object graph_dyn get;

Tensor initial_value get;

object initial_value_dyn get;

object initializer get;

object initializer_dyn get;

object name get;

object name_dyn get;

object op get;

object op_dyn get;

object PythonObject get;

object SaveSliceInfo_dyn get; set;

Information on how to save this Variable as a slice.

Provides internal support for saving variables as slices of a larger variable. This API is not public and is subject to change.

Available properties:

* full_name * full_shape * var_offset * var_shape

TensorShape shape get;

object shape_dyn get;

object synchronization get;

object synchronization_dyn get;

Nullable<bool> trainable get;

object trainable_dyn get;