Tensorflow:NameError:name' x_train'没有定义

时间:2017-06-13 13:59:07

标签: python tensorflow

我是张量流的新手。我收到了这个错误......

  

追踪(最近一次通话):   文件" C:\ Users \ s \ Desktop \ linear.py",第36行,in   sess.run(train,{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

2 个答案:

答案 0 :(得分:1)

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

NameError: name 'x_train' is not defined

您的意思是x_data吗?

答案 1 :(得分:1)

我想你的意思是... if "%hoi%" == "hallo" goto idk :: Note the `goto` must be on the same line as the `if` :: Note also that if the above test fails, batch will simply continue :: executing commands, so the next command to be executed :: will be the "xcopy" following :idk xcopy /s "E:\Windows.old\Program Files\WindowsApps\Microsoft.3DBuilder_11.1.9.0_x64__8wekyb3d8bbwe\test" "D:\" :: :: PAUSE takes no parameters. The `()` will cause the `pause` not :: to be recognised - `cmd` will attempt to execute a command "pause()" :: and fail. pause ... 而不是x_data

然而,一般来说,这不是tensorflow的问题。这是你代码中的一个缺陷。此代码将产生相同的错误,并且不使用tensorflow

x_train

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