简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM