Tensorflow:名称错误:未定义名称“x_train”

2024-05-26 11:11:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的tensorflow。我收到这个错误。。。

Traceback (most recent call last): File "C:\Users\s\Desktop\linear.py", line 36, in sess.run(train, {x: x_train, y: y_train}) NameError: name 'x_train' is not defined

我的代码:

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


Tags: runindatamodelistftensorflowtrain
2条回答

我想你应该有x_data而不是x_train

然而,总的来说,这不是来自tensorflow的问题。这是你的代码中的一个缺陷。下面的代码将产生相同的错误,它不使用tensorflow

p = "rr"
print x

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

您没有变量/张量x_train:这就是为什么您会得到一个错误

NameError: name 'x_train' is not defined

你是说x_data

相关问题 更多 >