Tanguy
Tanguy

Reputation: 3304

TensorFlow: why not use a function instead of a placeholder?

I am starting to use TensorFlow (with Python) and was wondering: when using a placeholder in a function, why not have an argument in my function which would feed a TensorFlow constant rather than the placeholder?

Here is an example (the difference is in x):

def sigmoid(z):
    x = tf.constant(z, dtype=tf.float32, name = "x")
    sigmoid = tf.sigmoid(x)
    with tf.Session() as sess: 
        result = sess.run(sigmoid)
    return result

instead of:

def sigmoid(z):
    x = tf.placeholder(tf.float32, name = "...")
    sigmoid = tf.sigmoid(x)
    with tf.Session() as sess: 
        result = sess.run(sigmoid, feed_dict={x:z})
    return result

Upvotes: 0

Views: 64

Answers (1)

Aaron
Aaron

Reputation: 2364

The idea with Tensorflow is that you will repeat the same calculation on lots of data. when you write the code you are setting up a computational graph that later you will execute on the data. In your first example, you have hard-coded the data to a constant. This is not a typical tensorflow use case. The second example is better because it allows you to reuse the same computational graph with different data.

Upvotes: 3

Related Questions