Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between tf.placeholder and tf.Variable?

Tags:

tensorflow

I'm a newbie to TensorFlow. I'm confused about the difference between tf.placeholder and tf.Variable. In my view, tf.placeholder is used for input data, and tf.Variable is used to store the state of data. This is all what I know.

Could someone explain to me more in detail about their differences? In particular, when to use tf.Variable and when to use tf.placeholder?

like image 464
J.Doe Avatar asked Apr 18 '16 12:04

J.Doe


People also ask

What does TensorFlow placeholder do?

A placeholder is a variable in Tensorflow to which data will be assigned sometime later on. It enables us to create processes or operations without the requirement for data. Data is fed into the placeholder as the session starts, and the session is run. We can feed in data into tensorflow graphs using placeholders.

What is a placeholder variable?

Placeholder variables are nontranslatable text strings that stand in for a term or phrase that is used multiple times, or represents a term that shouldn't be translated, such as an official product name.

What is a TF variable?

A tf. Variable represents a tensor whose value can be changed by running ops on it. Specific ops allow you to read and modify the values of this tensor. Higher level libraries like tf.

What is Feed_dict in TensorFlow?

In TensorFlow, there are placeholders that are similar to variables that anyone could define, even during the runtime by using the feed_dict argument.


1 Answers

In short, you use tf.Variable for trainable variables such as weights (W) and biases (B) for your model.

weights = tf.Variable(     tf.truncated_normal([IMAGE_PIXELS, hidden1_units],                     stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), name='weights')  biases = tf.Variable(tf.zeros([hidden1_units]), name='biases') 

tf.placeholder is used to feed actual training examples.

images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_PIXELS)) labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size)) 

This is how you feed the training examples during the training:

for step in xrange(FLAGS.max_steps):     feed_dict = {        images_placeholder: images_feed,        labels_placeholder: labels_feed,      }     _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) 

Your tf.variables will be trained (modified) as the result of this training.

See more at https://www.tensorflow.org/versions/r0.7/tutorials/mnist/tf/index.html. (Examples are taken from the web page.)

like image 143
Sung Kim Avatar answered Sep 18 '22 14:09

Sung Kim