vikash singh
vikash singh

Reputation: 1509

Getting exception while using tf.placeholder in TensorFlow

I am new to Python and TensorFlow. Can somebody explain me why I am getting the error while executing the below simple code -

import tensorflow as tf

#Model parameters
w = tf.Variable([.4], dtype =tf.float32)
b = tf.Variable([-4], dtype = tf.float32)

x = tf.placeholder(tf.float32)

linear_model = w*x +b

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

print(sess.run(linear_model))

The error stacktrace is -

File "C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun run_metadata)

tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float [[{{node Placeholder}}]]

Upvotes: 3

Views: 79

Answers (1)

anki
anki

Reputation: 765

The error is correct. You want to evaluate linear_model. At this point you have to specify x.

import tensorflow as tf

#Model parameters
w = tf.Variable([.4], dtype =tf.float32)
b = tf.Variable([-4], dtype = tf.float32)

x = tf.placeholder(tf.float32)

linear_model = w*x +b

sess = tf.Session()
init = tf.global_variables_initializer()
### This works fine!
sess.run(init)

### Now you have to specify x...
print(sess.run(linear_model, {x: 0}))

And this gives

[-4.]

Upvotes: 2

Related Questions