This is my code:
import tensorflow as tf
import numpy as np
from sympy import *
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1])
])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.0001), loss='mean_squared_error')
x_lst = []
y_lst = []
for i in range(1000):
x_lst.append(float(i))
for i in range(len(x_lst)):
y_lst.append(float(nextprime(i)))
xs = np.array(x_lst, dtype=float)
ys = np.array(y_lst, dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([1100.0]))
I'm trying to predict the next prime number here. The last line prints out [[nan]]
to the console. I've read somewhere that when this happens, it means stochastic gradient descent has 'exploded' or 'overloaded'. What do I do to fix this issue?
Edit:
When I try adding two neurons to the first Dense
layer, making my code look like this:
import tensorflow as tf
import numpy as np
from sympy import *
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1])
])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.0001), loss='mean_squared_error')
x_lst = []
y_lst = []
for i in range(1000):
x_lst.append(float(i))
for i in range(len(x_lst)):
y_lst.append(float(nextprime(i)))
xs = np.array(x_lst, dtype=float)
ys = np.array(y_lst, dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([1100.0]))
This is the output: [[ -747.6198 -1281.0985]]
None of these numbers are the correct answer, which is 1103
.