Variables

tf.contrib.framework.add_model_variable(var)

Adds a variable to the GraphKeys.MODEL_VARIABLES collection.

Args:
  • var: a variable.

tf.contrib.framework.assert_global_step(global_step_tensor)

Asserts global_step_tensor is a scalar int Variable or Tensor.

Args:
  • global_step_tensor: Tensor to test.

tf.contrib.framework.assert_or_get_global_step(graph=None, global_step_tensor=None)

Verifies that a global step tensor is valid or gets one if None is given.

If global_step_tensor is not None, check that it is a valid global step tensor (using assert_global_step). Otherwise find a global step tensor using get_global_step and return it.

Args:
  • graph: The graph to find the global step tensor for.
  • global_step_tensor: The tensor to check for suitability as a global step. If None is given (the default), find a global step tensor.
Returns:

A tensor suitable as a global step, or None if none was provided and none was found.


tf.contrib.framework.assign_from_checkpoint(model_path, var_list)

Creates an operation to assign specific variables from a checkpoint.

Args:
  • model_path: The full path to the model checkpoint. To get latest checkpoint use model_path = tf.train.latest_checkpoint(checkpoint_dir)
  • var_list: A list of Variable objects or a dictionary mapping names in the checkpoint to the correspoing variables to initialize. If empty or None, it would return no_op(), None.
Returns:

the restore_op and the feed_dict that need to be run to restore var_list.

Raises:
  • ValueError: If the checkpoint specified at model_path is missing one of the variables in var_list.

tf.contrib.framework.assign_from_checkpoint_fn(model_path, var_list, ignore_missing_vars=False, reshape_variables=False)

Returns a function that assigns specific variables from a checkpoint.

Args:
  • model_path: The full path to the model checkpoint. To get latest checkpoint use model_path = tf.train.latest_checkpoint(checkpoint_dir)
  • var_list: A list of Variable objects or a dictionary mapping names in the checkpoint to the correspoing variables to initialize. If empty or None, it would return no_op(), None.
  • ignore_missing_vars: Boolean, if True it would ignore variables missing in the checkpoint with a warning instead of failing.
  • reshape_variables: Boolean, if True it would automatically reshape variables which are of different shape then the ones stored in the checkpoint but which have the same number of elements.
Returns:

A function that takes a single argument, a tf.Session, that applies the assignment operation.

Raises:
  • ValueError: If the checkpoint specified at model_path is missing one of the variables in var_list.

tf.contrib.framework.assign_from_values(var_names_to_values)

Creates an assignment operation from a given mapping.

This function provides a mechanism for performing assignment of variables to values in a way that does not fill the graph with large assignment values.

Args:
  • var_names_to_values: A map from variable names to values.
Returns:
  • assign_op: An Operation that assigns each of the given variables to the requested values.
  • feed_dict: The feed dictionary to use when evaluating assign_op.
Raises:
  • ValueError: if any of the given variable names were not found.

tf.contrib.framework.assign_from_values_fn(var_names_to_values)

Returns a function that assigns specific variables from the given values.

This function provides a mechanism for performing assignment of variables to values in a way that does not fill the graph with large assignment values.

Args:
  • var_names_to_values: A map from variable names to values.
Returns:

A function that takes a single argument, a tf.Session, that applies the assignment operation.

Raises:
  • ValueError: if any of the given variable names were not found.

tf.contrib.framework.create_global_step(graph=None)

Create global step tensor in graph.

Args:
  • graph: The graph in which to create the global step. If missing, use default graph.
Returns:

Global step tensor.

Raises:
  • ValueError: if global step key is already defined.

tf.contrib.framework.get_global_step(graph=None)

Get the global step tensor.

The global step tensor must be an integer variable. We first try to find it in the collection GLOBAL_STEP, or by name global_step:0.

Args:
  • graph: The graph to find the global step in. If missing, use default graph.
Returns:

The global step variable, or None if none was found.

Raises:
  • TypeError: If the global step tensor has a non-integer type, or if it is not a Variable.

tf.contrib.framework.get_or_create_global_step(graph=None)

Returns and create (if necessary) the global step variable.

Args:
  • graph: The graph in which to create the global step. If missing, use default graph.
Returns:

the tensor representing the global step variable.


tf.contrib.framework.get_local_variables(scope=None, suffix=None)

Gets the list of model variables, filtered by scope and/or suffix.

Args:
  • scope: an optional scope for filtering the variables to return.
  • suffix: an optional suffix for filtering the variables to return.
Returns:

a list of variables in collection with scope and suffix.


tf.contrib.framework.get_model_variables(scope=None, suffix=None)

Gets the list of model variables, filtered by scope and/or suffix.

Args:
  • scope: an optional scope for filtering the variables to return.
  • suffix: an optional suffix for filtering the variables to return.
Returns:

a list of variables in collection with scope and suffix.


tf.contrib.framework.get_unique_variable(var_op_name)

Gets the variable uniquely identified by that var_op_name.

Args:
  • var_op_name: the full name of the variable op, including the scope.
Returns:

a tensorflow variable.

Raises:
  • ValueError: if no variable uniquely identified by the name exists.

tf.contrib.framework.get_variables_by_name(given_name, scope=None)

Gets the list of variables that were given that name.

Args:
  • given_name: name given to the variable without any scope.
  • scope: an optional scope for filtering the variables to return.
Returns:

a copied list of variables with the given name and scope.


tf.contrib.framework.get_variables_by_suffix(suffix, scope=None)

Gets the list of variables that end with the given suffix.

Args:
  • suffix: suffix for filtering the variables to return.
  • scope: an optional scope for filtering the variables to return.
Returns:

a copied list of variables with the given name and prefix.


tf.contrib.framework.get_variables_to_restore(include=None, exclude=None)

Gets the list of the variables to restore.

Args:
  • include: an optional list/tuple of scope strings for filtering which variables from the VARIABLES collection to include. None would include all the variables.
  • exclude: an optional list/tuple of scope strings for filtering which variables from the VARIABLES collection to exclude. None it would not exclude any.
Returns:

a list of variables to restore.

Raises:
  • TypeError: include or exclude is provided but is not a list or a tuple.

tf.contrib.framework.get_variables(scope=None, suffix=None, collection='variables')

Gets the list of variables, filtered by scope and/or suffix.

Args:
  • scope: an optional scope for filtering the variables to return.
  • suffix: an optional suffix for filtering the variables to return.
  • collection: in which collection search for. Defaults to GraphKeys.VARIABLES.
Returns:

a list of variables in collection with scope and suffix.


tf.contrib.framework.local_variable(initial_value, validate_shape=True, name=None)

Create variable and add it to GraphKeys.LOCAL_VARIABLES collection.

Args:
  • initial_value: See variables.Variable.init.
  • validate_shape: See variables.Variable.init.
  • name: See variables.Variable.init.
Returns:

New variable.


tf.contrib.framework.model_variable(*args, **kwargs)

Gets an existing model 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 if one is created.
  • regularizer: a (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection GraphKeys.REGULARIZATION_LOSSES and can be used for regularization.
  • trainable: If True also add the variable to the graph collection GraphKeys.TRAINABLE_VARIABLES (see tf.Variable).
  • collections: A list of collection names to which the Variable will be added. Note that the variable is always also added to the GraphKeys.VARIABLES and GraphKeys.MODEL_VARIABLES collections.
  • caching_device: Optional device string or function describing where the Variable should be cached for reading. Defaults to the Variable's device.
  • device: Optional device to place the variable. It can be an string or a function that is called to get the device for the variable.
Returns:

The created or existing variable.


tf.contrib.framework.variable(*args, **kwargs)

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 if one is created.
  • regularizer: a (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection GraphKeys.REGULARIZATION_LOSSES and can be used for regularization.
  • trainable: If True also add the variable to the graph collection GraphKeys.TRAINABLE_VARIABLES (see tf.Variable).
  • collections: A list of collection names to which the Variable will be added. If None it would default to tf.GraphKeys.VARIABLES.
  • caching_device: Optional device string or function describing where the Variable should be cached for reading. Defaults to the Variable's device.
  • device: Optional device to place the variable. It can be an string or a function that is called to get the device for the variable.
Returns:

The created or existing variable.


class tf.contrib.framework.VariableDeviceChooser

Device chooser for variables.

When using a parameter server it will assign them in a round-robin fashion. When not using a parameter server it allows GPU or CPU placement.


tf.contrib.framework.VariableDeviceChooser.__call__(op) {:#VariableDeviceChooser.call}


tf.contrib.framework.VariableDeviceChooser.__init__(num_tasks=0, job_name='ps', device_type='CPU', device_index=0) {:#VariableDeviceChooser.init}

Initialize VariableDeviceChooser.

Usage:

To use with 2 parameter servers: VariableDeviceChooser(2)

To use without parameter servers: VariableDeviceChooser() VariableDeviceChooser(device_type='GPU') # For GPU placement

Args:
  • num_tasks: number of tasks.
  • job_name: String, a name for the parameter server job.
  • device_type: Optional device type string (e.g. "CPU" or "GPU")
  • device_index: int. Optional device index. If left unspecified, device represents 'any' device_index.