SSung2710
SSung2710

Reputation: 612

Tensorflow resulting in NaN

I'm trying to adapt the sample linear regression program that TensorFlow has on their Getting Started page to quadratic regression. To do this, I just added another variable and changed the function. However, this seems to result in NaN values. Here's my code:

import numpy as np
import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sess = tf.Session()
a = tf.Variable([1.], dtype=tf.float32)
b = tf.Variable([0.3], dtype=tf.float32)
c = tf.Variable([0.3], dtype = tf.float32)
x = tf.placeholder(tf.float32)
x_train = [1,2,3,4]
y_train =[1,4,9,16]
quad_model = a * x * x + b * x + c
init = tf.global_variables_initializer()
sess.run(init)
y = tf.placeholder(tf.float32) # Model Data
squared_deltas = tf.square(quad_model - y)
loss = tf.reduce_sum(squared_deltas)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
for i in range(1000):
    sess.run(train, {x:x_train, y:y_train})
    print(sess.run([a,b,c]))

print(sess.run([a, b, c]))

Any help would be appreciated!

Upvotes: 1

Views: 147

Answers (1)

Tianjin Gu
Tianjin Gu

Reputation: 784

Maybe NaN caused by over fitting, you can reduce the learning rate and training count.

Upvotes: 1

Related Questions