Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between a placeholder and variable when not building a model?

I am trying to understand the difference between a placeholder and a variable in TensorFlow:

X = tf.placeholder("float")
W = tf.Variable(rng.randn(), name="weight")

I also read the Stack Overflow's question below. I understand the difference when they are the input of a model.

InvalidArgumentError: You must feed a value for placeholder tensor Placeholder

However, in general, if we are not building a model, is there still a difference between tf.placeholder() and tf.Variable()?

like image 562
Edamame Avatar asked Dec 23 '16 01:12

Edamame


People also ask

What is the difference between a placeholder and a variable?

Variable is with initial value and it can change in the computation; (so good for parameters) Placeholder is without initial value and it won't change in the computation.

Is a placeholder a variable?

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data.

What are placeholder variables in Python?

In Python, Placeholder is a word, characters or a string of characters to hold a temporary place. The placeholder behaves as a dynamic place holder in such a way that you can pass a particular value for that placeholder.

What is the difference between a variable and a placeholder?

You use a Variable if you need to store state. You use a placeholder if you need to input external data. If you are not building a model, you should still use tf.placeholder if you want to insert external data that you don't necessarily have while you're defining the graph.

What is the difference between placeholders and variables in TensorFlow?

I'm not an expert in Tensorflow, so I can only speculate as to why the design has both. A big difference between placeholders and variables is that placeholders can have variable size, but the shape of a tf.Variable must be specified while constructing the graph.

What is the difference between TF variable and TF placeholder?

In short, you use tf.Variable for trainable variables such as weights (W) and biases (B) for your model. tf.placeholder is used to feed actual training examples. This is how you feed the training examples during the training: Your tf.variables will be trained (modified) as the result of this training.

When to use a placeholder in a graph?

You use a placeholder if you need to input external data. If you are not building a model, you should still use tf.placeholder if you want to insert external data that you don't necessarily have while you're defining the graph.


1 Answers

Placeholder

A placeholder is used for feeding external data into a Tensorflow computation (stuff outside the graph). Here's some documentation: (https://www.tensorflow.org/versions/r0.10/how_tos/reading_data/#feeding)

TensorFlow's feed mechanism lets you inject data into any Tensor in a computation graph. A python computation can thus feed data directly into the graph.

I personally would draw an analogy from placeholders to reading from standard input.

x = raw_input()
X = tf.placeholder("float")

When you read from standard input, you need to "inject data" from an external source. Same with a placeholder. It lets you "inject data" that's external to the computation graph.

If you're training a learning algorithm, the clear use case of placeholder is to feed in your training data. The training data isn't stored in the computation graph. How are you going to get it into the graph? By injecting it through a placeholder. A placeholder is basically you telling the graph "I don't have this for you yet. But I'll have it for you when I ask you to run."

Variable

A variable is used to store state in your graph. It requires an initial value. One use case could be representing weights of a neural network or something similar. Here's documentation: (https://www.tensorflow.org/api_docs/python/tf/Variable)

A variable maintains state in the graph across calls to run(). You add a variable to the graph by constructing an instance of the class Variable.

The Variable() constructor requires an initial value for the variable, which can be a Tensor of any type and shape. The initial value defines the type and shape of the variable. After construction, the type and shape of the variable are fixed. The value can be changed using one of the assign methods.

I personally would draw an analogy between Tensorflow Variables and assigning a variable in Python to anything that is not dependent on external stuff. For example,

# Tensorflow:
W = tf.Variable(rng.randn(), name="weight")

# Standard python:
w = 5
w = "hello"
w = [1, 2, 3, 4, 5]

W represents some sort of result of your computation. Just like how you must initialize all your variables in Python (you can't just run a command x you have to say x = ...something...), you have to initialize all Variable objects in Tensorflow.

Variable vs. Placeholder

There's not much related between tf.Variable and tf.placeholder in my opinion. You use a Variable if you need to store state. You use a placeholder if you need to input external data.

If you are not building a model, you should still use tf.placeholder if you want to insert external data that you don't necessarily have while you're defining the graph. If you are not building a model, you still need tf.Variable if you want to store some kind of result of your computation while the graph is being run.

Why have both?

I'm not an expert in Tensorflow, so I can only speculate as to why the design has both.

A big difference between placeholders and variables is that placeholders can have variable size, but the shape of a tf.Variable must be specified while constructing the graph.

Variable size placeholders sense: maybe I only want to input a training batch of size 5 right now, but maybe I want to increase the batch size later on. Maybe I don't know ahead of time how many training examples I'm going to get.

Variable size variables don't make sense: tf.Variable holds the learned parameters of your model, and the number of parameters shouldn't change. Furthermore, Tensorflow extends to distributed computation. If you had Variables whose shape changed throughout the computation, it would be very difficult to keep it properly distributed among 1000 computers.

Usually, you build a model and all parameters are known ahead of time, so that's what tf.Variable is probably used to represent. tf.placeholder is probably for everything else outside of your model (or computation graph) and so that can be more flexible.

like image 153
user2570465 Avatar answered Sep 23 '22 21:09

user2570465