1
$\begingroup$

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!)


optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
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)
$\endgroup$
1
$\begingroup$

The way you calculate the 'error' function is wrong. You aggregated error value whenever you train the model. Therefore, the error value increases over time and reaches infinity (you can print the error value at each epoch to check that). The error function should be computed as follows.

error = tf.reduce_mean((y_hat - y_label)**2)

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. enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.