# tensorflow simple regression nan after >29 observations

I have code as below. If the number of data points changed to any number above 30 (example 40) then i get nan for values of final_slope , final_intercept why?

For 25 examples it runs fine. I am using a cpu version on tensorflow on my windows machine.

The number of datapoints can be changed by changing number on the line 4th line n= 40

import numpy as np
import tensorflow as tf

n= 40
x_data = np.linspace(0,10,n) + np.random.uniform(-1.5,1.5,n)
y_label = np.linspace(0,10,n) + np.random.uniform(-1.5,1.5,n)

import matplotlib.pyplot as plt
#%matplotlib inline
plt.plot(x_data,y_label,'*')

m = tf.Variable(0.39)
b = tf.Variable(0.2)

error = 0

for x,y in zip(x_data,y_label):

y_hat = m*x + b  #Our predicted value

error += (y-y_hat)**2 # The cost we want to minimize (we'll need to use an optimization function for the minimization!)

train = optimizer.minimize(error)

init = tf.global_variables_initializer()

with tf.Session() as sess:

sess.run(init)

epochs = 1000

for i in range(epochs):

sess.run(train)

# Fetch Back Results
final_slope , final_intercept = sess.run([m,b])

print (final_slope , final_intercept)


By the way, you can check the error value at each epoch. Choose the hyperparameters of the model to make sure that the error value decreases in the time span. Here is the error curve over the epoch span. 