2017-06-13 1030 views
0

我是tensorflow中的新成員。我收到這個錯誤...Tensorflow:NameError:名稱'x_train'未定義

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

回答

1

你沒有一個變量/張x_train:這就是爲什麼你會得到錯誤

NameError: name 'x_train' is not defined 

您的意思是x_data

+1

沒有注意到變量。謝謝! – Transit

1

我猜你的意思是有沒有x_datax_train

然而,在一般情況下,這是不是從tensorflow問題。這是你代碼中的一個缺陷。此代碼波紋管將產生相同的錯誤,它不使用tensorflow

p = "rr" 
print x 

因爲很顯然,我使用的變量x定義它。