0

When I run model.predict(##), I get the result nan. I also get this for calculating the loss function. This is what my code looks like:

import tensorflow as tf
import numpy as np
from tensorflow import keras

print(tf.__version__)

# Build a simple Sequential model
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])

# Compile the model
model.compile(optimizer='sgd', loss='mean_squared_error')

# Declare model inputs and outputs for training
x_train = np.loadtxt("combined_data.txt")
y_train = np.loadtxt("admit_data.txt")

# Train the model
model.fit(x_train, y_train, epochs=500)

prediction = np.array([[160, 160]])
print(np.shape(prediction))
# Make a prediction
print(model.predict(prediction))


# Evaluate the model
loss = model.evaluate(x_train, y_train)
print(f'Loss: {loss}')

And this is what some of the training data looks like: x_train:

163  164
152  161
159  158
164  146
163  154
163  131
152  150
141  155
147  157
142  168
139  156
151  151
144  160
134  157
158  143
167  133

And then my y_train values are either 0, 50, or 100, and cannot be anything else. I would expect a prediction somewhere from 0 to 100, but instead I get NAN (not a number)

1
  • Normalize your data. Neural networks can't work with unnormalized values, you get exploding gradients (and with that, nan values).
    – mhenning
    Commented Nov 7, 2023 at 13:32

1 Answer 1

0

Did your try with Adam optimizer? Instead of SGD?

By keeping your code and the information on the input/output of your database here is a code that runs without NAN

import tensorflow as tf
import numpy as np
from tensorflow import keras
print(tf.__version__)

# Build a simple Sequential model
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Declare model inputs and outputs for training
x_train = []
x_train.append([163,  164])
x_train.append([152,  161])
x_train.append([159,  158])
x_train.append([164,  146])
x_train.append([163,  154])
x_train.append([163,  131])
x_train.append([152,  150])
x_train.append([141,  155])
x_train.append([147,  157])
x_train.append([142,  168])
x_train.append([139,  156])
x_train.append([151,  151])
x_train.append([144,  160])
x_train.append([134,  157])
x_train.append([158,  143])
x_train.append([167,  133])
x_train = tf.convert_to_tensor(np.array(x_train).astype(np.float32))

y_train = tf.convert_to_tensor(np.random.choice([0, 50, 100], size=len(x_train)).reshape([len(x_train),1]).astype(np.float32))

# Train the model
model.fit(x_train, y_train, epochs=500)

prediction = np.array([[160, 160]])
print(np.shape(prediction))
# Make a prediction
print(model.predict(prediction))


# Evaluate the model
loss = model.evaluate(x_train, y_train)
print(f'Loss: {loss}')

Also as mentionned by @mhenning, there are some good pratices you need to try.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.