You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于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:

  • x is just a typo for the placeholder they defined: a
  • y is 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 a here) 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:27:20