0
votes

I'm new into tensorflow. I'm receiving this error...

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

My Code:

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

2 Answers

1
votes

You do not have a variable/tensor x_train: this is why you get an error

NameError: name 'x_train' is not defined

Did you mean x_data?

1
votes

I guess you meant to have x_data not x_train

However, in general, this is not a problem from tensorflow. It is a flaw in your code. This code bellow will produce the same error and it does not use tensorflow

p = "rr"
print x

As it is clear that I am using the variable x before defining it.