0

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.

Robo
  • 660
  • 5
  • 22
  • 1
    Your model is not even a proper neural network (it is actually equivalent to simple linear regression); and regarding extrapolating (i.e. what you are trying to do here), please see [Is deep learning bad at fitting simple non linear functions outside training scope (extrapolating)?](https://stackoverflow.com/questions/53795142/is-deep-learning-bad-at-fitting-simple-non-linear-functions-outside-training-sco) – desertnaut Nov 19 '21 at 13:46
  • Your 2nd snippet is identical to the first one - please edit & update accordingly; and the idea is not to add a 2nd neuron (hence getting 2 results for a problem where you clearly need only 1) - it is to add more *layers*. – desertnaut Nov 19 '21 at 14:08

0 Answers0