3

When I read TensorFlow codes, I see people specify placeholders for the input arguments of the functions and then feed the input data in a session.run. A trivial example can be like:

def sigmoid(z):

    x = tf.placeholder(tf.float32, name='x')

    sigmoid = tf.sigmoid(x)

    with tf.Session() as session:
        result = session.run(sigmoid, feed_dict={x:z})

    return result

I wonder why don't they directly feed the z into the tf.sigmoid(z) and get rid of the placeholder x?

If this is a best practice, what is the reason behind it?

mohaghighat
  • 1,293
  • 17
  • 29

1 Answers1

2

In your example method sigmoid, you basically built a small computation graph (see below) and run it with session.run (in the same method). Yes, it does not add any benefit to use a place-holder in your case.

enter image description here

However, usually people just built the computation graph (and execute the graph with data later). But at the time of building the graph, the data is not needed. That's why we use a place-holder to hold the place of data. Or in other words, it allows us to create our computing operations without needing any data.

Also this should explain why we want to use tf.placehoder instead of tf.Variable for holding training data. In short:

  • tf.Variable is for trainable parameters of the model.
  • tf.placeholder is for training data which does not change as model trains.
  • No initial values are needed for placeholders.
  • The first dimension of data through feeding could be None thus supporting any batch_size.
greeness
  • 15,956
  • 5
  • 50
  • 80
  • Thank you for your reply. What I meant was not using a tf.Variable, but just the numpy.ndarray, z, which is the input argument in the function definition. Since the function definition does not need the data, can't it be used at the time of building the graph? – mohaghighat Oct 26 '17 at 16:45
  • No actually each tensor is a tensor node in the graph. When we are building the graph, imaging that you have some building blocks and one of them is this placeholder. Numpy array cannot be used at that time. After execution, a tensor node outputs a numpy array though. – greeness Oct 26 '17 at 17:07