Q:

Getting nan value in Output in Tensorflow

Can you please help me in below code ?
Getting nan value in Output in Tensorflow, when trying to get the values
of w and b in tensorflow session
import numpy as np
import tensorflow as tf
import numpy.random as rand

trainX = np.array([[2.5,5.6,7.8,8.9]],dtype=np.float32)

trainY = np.array([[6.7,6.7,8.9,5.4]],dtype=np.float32)
num_samples = trainX.shape[0]
num_samples2 = trainY.shape[0]
X = tf.placeholder(dtype=tf.float32)
Y = tf.placeholder(dtype=tf.float32)
type(X)
 w = tf.Variable(rand.randn(1),dtype=tf.float32)
b = tf.Variable(rand.randn(1),dtype=tf.float32)
type(w)
tf.shape(w)
tf.shape(b)
w = tf.convert_to_tensor(w,dtype=tf.float32)
b = tf.convert_to_tensor(b,dtype=tf.float32)
num_iter = 10000 
learning_rate = 0.01
def model(X,w,b):
    return tf.multiply(X,w) + b
type(X)
type(w)
pred = model(X,w,b)
cost = tf.square(Y-pred)
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(cost)
text_file = open("Output.txt", "w")
model = tf.global_variables_initializer()
with tf.Session() as session:
    session.run(model)
    for i in range(num_iter):
        session.run(train, feed_dict={X: trainX , Y: trainY})
    print(session.run(w))
    print(session.run(b))

Thanks and Regards,
Subho

A:

It is "diverged".
Change learning rate lower.
#learning_rate = 0.01
learning_rate = 0.001

I confirmed below result.
[0.00044938]
[6.922184]

