关于Quora上TensorFlow示例代码中x和y未定义的技术咨询
Hey there! Let's work through this error you're hitting—totally understandable when you're just starting out with TensorFlow.
Fixing the Undefined
x/y Error in That Quora Code Snippet First off, the code from that Quora page has simple typos that are causing the "x and y not defined" error. Let's break it down:
The Broken Snippet (With Typos)
Here's the original code you referenced:
a = tf.placeholder(tf.float32) b = a+5 with tf.Session() as session: result = session.run(y, feed_dict={x: [1, 2, 3]}) print(result)
What's the Context for x and y?
The author of that snippet mixed up variable names! Here's what they meant to write:
xis just a typo for the placeholder they defined:ayis a typo for the computed tensor:b
The Corrected Working Code
a = tf.placeholder(tf.float32) # This is the "x" the author referenced b = a + 5 # This is the "y" they wanted to compute with tf.Session() as session: result = session.run(b, feed_dict={a: [1, 2, 3]}) print(result) # Output will be [6. 7. 8.]
Quick Beginner Context for Placeholders vs Variables
Since you're learning the difference between these two:
- Placeholders (like
ahere) are temporary "slots" for data you feed in when you run your session. They're perfect for input data that changes each run (like training batches). - Variables are for values that stick around across session runs—think model weights that you train and update over time. This snippet doesn't use Variables, but it's a key distinction to remember as you keep learning!
内容的提问来源于stack exchange,提问作者ffejrekaburb
相关产品推荐
相关产品推荐

