【问题标题】:Tensorflow: NameError: name 'x_train' is not definedTensorflow:NameError:名称'x_train'未定义
【发布时间】:2017-11-15 08:43:56
【问题描述】:

我是张量流的新手。我收到此错误...

Traceback(最近一次调用最后一次): 文件“C:\Users\s\Desktop\linear.py”,第 36 行,在 sess.run(火车,{x:x_train,y:y_train}) NameError: name 'x_train' 没有定义

我的代码:

import tensorflow as tf

# y = mx + b

#Model input and output
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

# Model parameter
m = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)

linear_model = m * x + b

# Loss function
loss = tf.reduce_sum(tf.square(linear_model - y))

#optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

#training data 
x_data = [1,2,3,4]
y_data = [0,-1,-2,-3]

# Run session
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

#tTensorboard
writer = tf.summary.FileWriter('./graphs', sess.graph)

# highest value for range is 25,000
for i in range(1000):
    sess.run(train, {x: x_train, y: y_train})
    # evaluation
    curr_m, curr_b, curr_loss = sess.run([m,b,loss], {x: x_train, y: y_train})
    # print 
    print("%s, %s, %s"%(curr_m, curr_b, curr_loss))

writer.close()

This is the screenshot

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    你没有变量/张量x_train:这就是你得到错误的原因

    NameError: name 'x_train' is not defined
    

    你是说x_data吗?

    【讨论】:

    • 没有注意到变量。谢谢!
    【解决方案2】:

    我猜你的意思是 x_data 而不是 x_train

    但是,总的来说,这不是 tensorflow 的问题。这是您的代码中的一个缺陷。下面的代码会产生同样的错误,并且它不使用 tensorflow

    p = "rr"
    print x
    

    很明显,我在定义变量 x 之前使用它。

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多