Variable helper functions

TensorFlow provides a set of functions to help manage the set of variables collected in the graph.

tf.all_variables()

Returns all variables that must be saved/restored.

The Variable() constructor automatically adds new variables to the graph collection GraphKeys.VARIABLES. This convenience function returns the contents of that collection.

Returns:

A list of Variable objects.


tf.trainable_variables()

Returns all variables created with trainable=True.

When passed trainable=True, the Variable() constructor automatically adds new variables to the graph collection GraphKeys.TRAINABLE_VARIABLES. This convenience function returns the contents of that collection.

Returns:

A list of Variable objects.


tf.local_variables()

Returns all variables created with collection=[LOCAL_VARIABLES].

Returns:

A list of local Variable objects.


tf.model_variables()

Returns all variables in the MODEL_VARIABLES collection.

Returns:

A list of local Variable objects.


tf.moving_average_variables()

Returns all variables that maintain their moving averages.

If an ExponentialMovingAverage object is created and the apply() method is called on a list of variables, these variables will be added to the GraphKeys.MOVING_AVERAGE_VARIABLES collection. This convenience function returns the contents of that collection.

Returns:

A list of Variable objects.


tf.initialize_all_variables()

Returns an Op that initializes all variables.

This is just a shortcut for initialize_variables(all_variables())

Returns:

An Op that initializes all variables in the graph.


tf.initialize_variables(var_list, name='init')

Returns an Op that initializes a list of variables.

After you launch the graph in a session, you can run the returned Op to initialize all the variables in var_list. This Op runs all the initializers of the variables in var_list in parallel.

Calling initialize_variables() is equivalent to passing the list of initializers to Group().

If var_list is empty, however, the function still returns an Op that can be run. That Op just has no effect.

Args:
  • var_list: List of Variable objects to initialize.
  • name: Optional name for the returned operation.
Returns:

An Op that run the initializers of all the specified variables.


tf.initialize_local_variables()

Returns an Op that initializes all local variables.

This is just a shortcut for initialize_variables(local_variables())

Returns:

An Op that initializes all local variables in the graph.


tf.is_variable_initialized(variable)

Tests if a variable has been initialized.

Args:
  • variable: A Variable.
Returns:

Returns a scalar boolean Tensor, True if the variable has been initialized, False otherwise.


tf.report_uninitialized_variables(var_list=None, name='report_uninitialized_variables')

Adds ops to list the names of uninitialized variables.

When run, it returns a 1-D tensor containing the names of uninitialized variables if there are any, or an empty array if there are none.

Args:
  • var_list: List of Variable objects to check. Defaults to the value of all_variables() + local_variables()
  • name: Optional name of the Operation.
Returns:

A 1-D tensor containing names of the uninitialized variables, or an empty 1-D tensor if there are no variables or no uninitialized variables.


tf.assert_variables_initialized(var_list=None)

Returns an Op to check if variables are initialized.

NOTE: This function is obsolete and will be removed in 6 months. Please change your implementation to use report_uninitialized_variables().

When run, the returned Op will raise the exception FailedPreconditionError if any of the variables has not yet been initialized.

Args:
  • var_list: List of Variable objects to check. Defaults to the value of all_variables().
Returns:

An Op, or None if there are no variables.


tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)

Update 'ref' by assigning 'value' to it.

This operation outputs "ref" after the assignment is done. This makes it easier to chain operations that need to use the reset value.

Args:
  • ref: A mutable Tensor. Should be from a Variable node. May be uninitialized.
  • value: A Tensor. Must have the same type as ref. The value to be assigned to the variable.
  • validate_shape: An optional bool. Defaults to True. If true, the operation will validate that the shape of 'value' matches the shape of the Tensor being assigned to. If false, 'ref' will take on the shape of 'value'.
  • use_locking: An optional bool. Defaults to True. If True, the assignment will be protected by a lock; otherwise the behavior is undefined, but may exhibit less contention.
  • name: A name for the operation (optional).
Returns:

Same as "ref". Returned as a convenience for operations that want to use the new value after the variable has been reset.


tf.assign_add(ref, value, use_locking=None, name=None)

Update 'ref' by adding 'value' to it.

This operation outputs "ref" after the update is done. This makes it easier to chain operations that need to use the reset value.

Args:
  • ref: A mutable Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half. Should be from a Variable node.
  • value: A Tensor. Must have the same type as ref. The value to be added to the variable.
  • use_locking: An optional bool. Defaults to False. If True, the addition will be protected by a lock; otherwise the behavior is undefined, but may exhibit less contention.
  • name: A name for the operation (optional).
Returns:

Same as "ref". Returned as a convenience for operations that want to use the new value after the variable has been updated.


tf.assign_sub(ref, value, use_locking=None, name=None)

Update 'ref' by subtracting 'value' from it.

This operation outputs "ref" after the update is done. This makes it easier to chain operations that need to use the reset value.

Args:
  • ref: A mutable Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half. Should be from a Variable node.
  • value: A Tensor. Must have the same type as ref. The value to be subtracted to the variable.
  • use_locking: An optional bool. Defaults to False. If True, the subtraction will be protected by a lock; otherwise the behavior is undefined, but may exhibit less contention.
  • name: A name for the operation (optional).
Returns:

Same as "ref". Returned as a convenience for operations that want to use the new value after the variable has been updated.