python keras prediction returning nan

  • Last Update :
  • Techknowledgy :

Source code :

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 5)

# create model
model = Sequential()
model.add(Dense(950, input_shape = (425, ), activation = 'relu'))
model.add(Dense(425, activation = 'relu'))
model.add(Dense(200, activation = 'relu'))
model.add(Dense(50, activation = 'relu'))
model.add(Dense(1, activation = 'sigmoid'))

# Compile model
sgd = optimizers.SGD(lr = 0.1, decay = 1e-6, momentum = 0.9, nesterov = True)
model.compile(loss = 'mean_squared_error', optimizer = 'sgd')

# Fit the model
model.fit(X_train, y_train, epochs = 20, batch_size = 1, verbose = 1)

#evaluate the model
y_pred = model.predict(X_test)

score = model.evaluate(X_test, y_test, verbose = 1)
print(score)

# calculate predictions
predictions = model.predict(X_pred)

y_train and y_test look like :

array([1.17899644, 1.46080518, 0.9662137, ..., 2.40157461,
   0.53870386, 1.3192718
])

Suggestion : 2

Also it seems that the network is not anycodings_tensorflow actually learning for reasons that i cant anycodings_tensorflow figure out.,I am having problems with understanding how anycodings_tensorflow Keras works with data and why my model is anycodings_tensorflow not working accordingly. I am trying to anycodings_tensorflow build small model that could predict cities anycodings_tensorflow based on input of longitude and latitude.,What i would like to see is when i make a anycodings_tensorflow prediction, for example, the first index of anycodings_tensorflow cities array i would like to see the output anycodings_tensorflow array index zero to have largest activation anycodings_tensorflow value.,Data The latitude and longitude data is anycodings_tensorflow normalized between 0 / 1

Data The latitude and longitude data is anycodings_tensorflow normalized between 0 / 1

cities = [];

cities.append([60.1695213, 24.9354496]);
#1
cities.append([60.2052002,24.6522007]); # 2
cities.append([61.4991112, 23.7871208]);
#3
cities.append([64.222176,27.72785]);    # 4
cities.append([60.4514809, 22.2686901]);
#5
cities.append([65.0123596,25.4681606]); # 6
cities.append([60.9826698, 25.6615105]);
#7
cities.append([62.8923798,27.6770306]); # 8
cities.append([62.2414703, 25.7208805]);
#9
cities.append([61.4833298,21.7833309]); # 10
cities.append([61.0587082, 28.1887093]);
#11
cities.append([63.0960007,21.6157703]); # 12
cities.append([60.4664001, 26.9458199]);
#13
cities.append([62.601181,29.7631607]); # 14
cities.append([60.9959602, 24.4643402]);
#15
cities.append([60.3923302,25.6650696]); # 16
cities.append([61.6885681, 27.2722702]);
#17
cities.append([65.579287,24.196943]); # 18
cities.append([65.986503, 28.692848]);
#19
cities.append([61.1272392,21.5112705]); # 20

train_cities = np.array(cities);

for i in train_cities:
   i[0] = normalize(i[0], 65.986503, 60.1695213, 0.99, 0.01)
i[1] = normalize(i[1], 29.7631607, 21.5112705, 0.99, 0.01)

train_labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

Normalized longitude/latitude

[
   [0.01168472 0.41784541]
   [0.01769563 0.38420658]
   [0.23568373 0.28146911]
   [0.69444458 0.74947275]
   [0.05918709 0.10113927]
   [0.82756859 0.48111052]
   [0.14867768 0.50407289]
   [0.47041082 0.7434374]
   [0.36075063 0.51112371]
   [0.233025 0.04349768]
   [0.16148804 0.80420471]
   [0.50471529 0.02359807]
   [0.06170056 0.65659833]
   [0.42135191 0.99118761]
   [0.15091674 0.36189614]
   [0.04922184 0.50449557]
   [0.26760196 0.69536778]
   [0.92308013 0.33013987]
   [0.99168472 0.86407655]
   [0.17303361 0.01118761]
]

Model

model = keras.Sequential([
   keras.layers.Dense(10, activation = tf.nn.relu, input_shape = (2, )),
   keras.layers.Dense(20, activation = tf.nn.softmax)
]);

model.compile(optimizer = 'adam',
   loss = 'sparse_categorical_crossentropy',
   metrics = ['accuracy'])

model.fit(train_cities, train_labels, epochs = 50)

I am getting a output array of nan indexes

array([
   [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
      nan, nan, nan, nan, nan, nan, nan
   ]
], dtype = float32)

Also it seems that the network is not anycodings_tensorflow actually learning for reasons that i cant anycodings_tensorflow figure out.

Epoch 50 / 50
20 / 20[ === === === === === === === === === === ] - 0 s 200 us / step - loss: nan - acc: 0.0000e+00

Suggestion : 3

I am having problems with understanding how Keras works with data and why my model is not working accordingly. I am trying to build small model that could predict cities based on input of longitude and latitude.,kerasneural-networkpythontensorflow,Also it seems that the network is not actually learning for reasons that i cant figure out.,What i would like to do with this data is simply input one of the cities index array to the network and get the corresponding label for it.

Data
The latitude and longitude data is normalized between 0 / 1

cities = [];

cities.append([60.1695213, 24.9354496]);
#1
cities.append([60.2052002,24.6522007]); # 2
cities.append([61.4991112, 23.7871208]);
#3
cities.append([64.222176,27.72785]);    # 4
cities.append([60.4514809, 22.2686901]);
#5
cities.append([65.0123596,25.4681606]); # 6
cities.append([60.9826698, 25.6615105]);
#7
cities.append([62.8923798,27.6770306]); # 8
cities.append([62.2414703, 25.7208805]);
#9
cities.append([61.4833298,21.7833309]); # 10
cities.append([61.0587082, 28.1887093]);
#11
cities.append([63.0960007,21.6157703]); # 12
cities.append([60.4664001, 26.9458199]);
#13
cities.append([62.601181,29.7631607]); # 14
cities.append([60.9959602, 24.4643402]);
#15
cities.append([60.3923302,25.6650696]); # 16
cities.append([61.6885681, 27.2722702]);
#17
cities.append([65.579287,24.196943]); # 18
cities.append([65.986503, 28.692848]);
#19
cities.append([61.1272392,21.5112705]); # 20

train_cities = np.array(cities);

for i in train_cities:
   i[0] = normalize(i[0], 65.986503, 60.1695213, 0.99, 0.01)
i[1] = normalize(i[1], 29.7631607, 21.5112705, 0.99, 0.01)

train_labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

Normalized longitude/latitude

[
   [0.01168472 0.41784541]
   [0.01769563 0.38420658]
   [0.23568373 0.28146911]
   [0.69444458 0.74947275]
   [0.05918709 0.10113927]
   [0.82756859 0.48111052]
   [0.14867768 0.50407289]
   [0.47041082 0.7434374]
   [0.36075063 0.51112371]
   [0.233025 0.04349768]
   [0.16148804 0.80420471]
   [0.50471529 0.02359807]
   [0.06170056 0.65659833]
   [0.42135191 0.99118761]
   [0.15091674 0.36189614]
   [0.04922184 0.50449557]
   [0.26760196 0.69536778]
   [0.92308013 0.33013987]
   [0.99168472 0.86407655]
   [0.17303361 0.01118761]
]

Model

model = keras.Sequential([
   keras.layers.Dense(10, activation = tf.nn.relu, input_shape = (2, )),
   keras.layers.Dense(20, activation = tf.nn.softmax)
]);

model.compile(optimizer = 'adam',
   loss = 'sparse_categorical_crossentropy',
   metrics = ['accuracy'])

model.fit(train_cities, train_labels, epochs = 50)

I am getting a output array of nan indexes

array([
   [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
      nan, nan, nan, nan, nan, nan, nan
   ]
], dtype = float32)

Also it seems that the network is not actually learning for reasons that i cant figure out.

Epoch 50 / 50
20 / 20[ === === === === === === === === === === ] - 0 s 200 us / step - loss: nan - acc: 0.0000e+00

Suggestion : 4

 November 09, 2021     tensorflow, tensorflow.js     No comments   

And I tried to code it:

const model = tf.sequential();
model.add(tf.layers.dense({
   units: 2,
   inputShape: [2]
}));
model.add(tf.layers.dense({
   units: 64,
   inputShape: [2]
}));
model.add(tf.layers.dense({
   units: 1,
   inputShape: [64]
}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({
   loss: 'meanSquaredError',
   optimizer: 'sgd'
});

// Generate some synthetic data for training.
const xs = tf.tensor([
   [1, 5],
   [2, 10],
   [3, 15],
   [4, 20],
   [5, 25],
   [6, 30],
   [7, 35],
   [8, 40]
], [8, 2]);
const ys = tf.tensor([1, 2, 3, 4, 5, 6, 7, 8], [8, 1]);

// Train the model using the data.
model.fit(xs, ys, {
   epochs: 100
}).then(() => {
   // Use the model to do inference on a data point the model hasn't seen before:
   // Open the browser devtools to see the output
   model.predict(tf.tensor([10, 50], [1, 2])).print();
});

The first layer takes the shape of the input data

model.add(tf.layers.dense({
   units: 2,
   inputShape: [2]
}))

The inputShape is [2], which means that your input x is of shape [2]. The last layer unit value gives the dimension of the output y.

 model.add(tf.layers.dense({
    units: 1,
    inputShape: [64]
 }));