Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}}]]

like image 599
vikash singh Avatar asked Sep 17 '26 07:09

vikash singh


1 Answers

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.]
like image 113
anki Avatar answered Sep 18 '26 20:09

anki