Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we name variables in Tensorflow?

In some of the places, I saw the syntax, where variables are initialized with names, sometimes without names. For example:

# With name var = tf.Variable(0, name="counter")  # Without one = tf.constant(1) 

What is the point of naming the variable var "counter"?

like image 527
randomizer Avatar asked Nov 11 '15 10:11

randomizer


People also ask

What is TensorFlow variable used for?

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.

What is name in TensorFlow?

The name TensorFlow derives from the operations that such neural networks perform on multidimensional data arrays, which are referred to as tensors. During the Google I/O Conference in June 2016, Jeff Dean stated that 1,500 repositories on GitHub mentioned TensorFlow, of which only 5 were from Google.

How do you declare a TensorFlow variable?

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. The type and form of the variable are defined by its initial value. The shape and the variables are fixed once they are created.

What is the difference between tf constant and tf variable?

In TensorFlow the differences between constants and variables are that when you declare some constant, its value can't be changed in the future (also the initialization should be with a value, not with operation). Nevertheless, when you declare a Variable, you can change its value in the future with tf.


2 Answers

The name parameter is optional (you can create variables and constants with or without it), and the variable you use in your program does not depend on it. Names can be helpful in a couple of places:

When you want to save or restore your variables (you can save them to a binary file after the computation). From docs:

By default, it uses the value of the Variable.name property for each variable

matrix_1 = tf.Variable([[1, 2], [2, 3]], name="v1") matrix_2 = tf.Variable([[3, 4], [5, 6]], name="v2") init = tf.initialize_all_variables()  saver = tf.train.Saver()  sess = tf.Session() sess.run(init) save_path = saver.save(sess, "/model.ckpt") sess.close() 

Nonetheless you have variables matrix_1, matrix_2 they are saves as v1, v2 in the file.

Also names are used in TensorBoard to nicely show names of edges. You can even group them by using the same scope:

import tensorflow as tf  with tf.name_scope('hidden') as scope:   a = tf.constant(5, name='alpha')   W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')   b = tf.Variable(tf.zeros([1]), name='biases') 
like image 163
Salvador Dali Avatar answered Sep 25 '22 10:09

Salvador Dali


You can imagine Python namespace and TensorFlow namespace as two parallel universes. Names in TensorFlow space are actually the "real" attributes belonging to any TensorFlow variables, while names in Python space are just temporary pointers pointing to TensorFlow variables during this run of your script. That is the reason why when saving and restoring variables, only TensorFlow names are used, because the Python namespace no longer exists after script being terminated, but Tensorflow namespace is still there in your saved files.

like image 28
Lifu Huang Avatar answered Sep 23 '22 10:09

Lifu Huang