Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a local variable in tensorflow?

Tags:

tensorflow

Tensorflow has this API defined:

tf.local_variables()

Returns all variables created with collection=[LOCAL_VARIABLES].

Returns:

A list of local Variable objects.

What exactly is a local variable in TensorFlow? Can someone give me an example?

like image 657
Tsuan Avatar asked Aug 12 '16 04:08

Tsuan


People also ask

What are local variables?

A local variable is a type of variable that can be used where the scope and extent of the variable is within the method or statement block in which it is declared. It is used as an iteration variable in the foreach statement, exception variable in the specific-catch clause and resource variable in the using statement.

What is a local variable python?

In general, a variable that is defined in a block is available in that block only. It is not accessible outside the block. Such a variable is called a local variable.

What is an example of a local variable?

Declaration of Local Variable: In this case it is executed in the same manner as if it were part of a local variable declaration statement. For example: for(int i=0;i<=5;i++){……} In above example int i=0 is a local variable declaration. Its scope is only limited to the for loop.

What is a variables in TensorFlow?

A variable is a state or value that can be modified by performing operations on it. In TensorFlow variables are created using the Variable() constructor. The Variable() constructor expects an initial value for the variable, which can be any kind or shape of Tensor.

What is a TensorFlow variable?

Was this helpful? A TensorFlow variable is the recommended way to represent shared, persistent state your program manipulates. This guide covers how to create, update, and manage instances of tf.Variable in TensorFlow. Variables are created and tracked via the tf.Variable class.

Is it possible to set the location of variables and tensors?

It's possible to set the location of a variable or tensor on one device and do the computation on another device. This will introduce delay, as data needs to be copied between the devices.

Does TensorFlow run faster on GPU or CPU?

For better performance, TensorFlow will attempt to place tensors and variables on the fastest device compatible with its dtype. This means most variables are placed on a GPU if one is available. However, you can override this. In this snippet, place a float tensor and a variable on the CPU, even if a GPU is available.

What is a tensor variable in Python?

A variable looks and acts like a tensor, and, in fact, is a data structure backed by a tf.Tensor. Like tensors, they have a dtype and a shape, and can be exported to NumPy. Shape: (2, 2) DType: <dtype: 'float32'> As NumPy: [ [1. 2.] [3. 4.]] Most tensor operations work on variables as expected, although variables cannot be reshaped.


2 Answers

Short answer: a local variable in TF is any variable which was created with collections=[tf.GraphKeys.LOCAL_VARIABLES]. For example:

e = tf.Variable(6, name='var_e', collections=[tf.GraphKeys.LOCAL_VARIABLES])

LOCAL_VARIABLES: the subset of Variable objects that are local to each machine. Usually used for temporarily variables, like counters. Note: use tf.contrib.framework.local_variable to add to this collection.

They are usually not saved/restored to checkpoint and used for temporary or intermediate values.


Long answer: this was a source of confusion for me as well. In the beginning I thought that local variables mean the same thing as local variable in almost any programming language, but it is not the same thing:

import tensorflow as tf

def some_func():
    z = tf.Variable(1, name='var_z')

a = tf.Variable(1, name='var_a')
b = tf.get_variable('var_b', 2)
with tf.name_scope('aaa'):
    c = tf.Variable(3, name='var_c')

with tf.variable_scope('bbb'):
    d = tf.Variable(3, name='var_d')

some_func()
some_func()

print [str(i.name) for i in tf.global_variables()]
print [str(i.name) for i in tf.local_variables()]

No matter what I tried, I always recieved only global variables:

['var_a:0', 'var_b:0', 'aaa/var_c:0', 'bbb/var_d:0', 'var_z:0', 'var_z_1:0']
[]

The documentation for tf.local_variables have not provided a lot of details:

Local variables - per process variables, usually not saved/restored to checkpoint and used for temporary or intermediate values. For example, they can be used as counters for metrics computation or number of epochs this machine has read data. The local_variable() automatically adds new variable to GraphKeys.LOCAL_VARIABLES. This convenience function returns the contents of that collection.


But reading docs for the init method in tf.Variable class, I found that while creating a variable, you can provide what kind of a variable do you want it to be by assigning a list of collections.

The list of possible collection elements is here. So to create a local variable you need to do something like this. You will see it in the list of local_variables:

e = tf.Variable(6, name='var_e', collections=[tf.GraphKeys.LOCAL_VARIABLES])
print [str(i.name) for i in tf.local_variables()]
like image 169
Salvador Dali Avatar answered Oct 12 '22 13:10

Salvador Dali


It's the same as regular variable, but it's in a different collection than default (GraphKeys.VARIABLES). That collection is used by saver to initialize the default list of variables to save, so having a local designation has an effect of not saving that variable by default.

I'm seeing only one place that uses it in the codebase, which is the limit_epochs

  with ops.name_scope(name, "limit_epochs", [tensor]) as name:
    zero64 = constant_op.constant(0, dtype=dtypes.int64)
    epochs = variables.Variable(
        zero64, name="epochs", trainable=False,
        collections=[ops.GraphKeys.LOCAL_VARIABLES])
like image 42
Yaroslav Bulatov Avatar answered Oct 12 '22 12:10

Yaroslav Bulatov