# I am getting (loss: nan - accuracy: 0.0000e+00) for all epochs after training the model

I made a simple model to train my data set which consists of (210 samples and each sample consists of a numpy array of 22 values) and x_trian and y_trian look like:

and this is my simple code:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffler

from google.colab import files

import io

#the split ratio of 80:20. The 20% testing data set is represented by the 0.2 at the end.
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)

x_train= np.asarray(x_train).astype('float32')
y_train = np.asarray(y_train).astype('float32')
y_train, x_train = shuffle(y_train, x_train)

#create the model #input_shape=(23,)
model = Sequential([
Dense(units=4,input_shape=(22,), activation='relu'),
Dense(units=16, activation='relu'),
Dense(units=10, activation='softmax')
])

#get the model ready for training is call the compile() function on it.

#train it using the fit() function.
model.fit(x_train, y_train, epochs=5)


And this is what I'm getting for all the epochs :

I will be grateful to anyone who can help me!

• The reason must be there: y_train, x_train = shuffle(y_train, x_train)when doing this you are simply breaking the relationship between your features set and your target. Actually I do not see where you define x_train nor y_train Aug 25, 2021 at 22:26
• i forgot this line sorry and i undated it above x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2) Aug 25, 2021 at 22:39
• great! so any reason for this line y_train, x_train = shuffle(y_train, x_train? from my understanding this is causing your model not to be able to learn anything since it adds literally pure randomness to your X, y mapping Aug 25, 2021 at 22:40
• thank you julio, unfortunately the code give me another error and i don't know why,how ever it was working will, i gives me "could not convert string to float: '9 0.559884" for this line "x_train= np.asarray(x_train).astype('float32')" Aug 25, 2021 at 22:47
• It seems like you have mixed data types in your x matrix, all the inputs should be numerical and in here you have a value that could not be converted into float '9 0.559884, it might be because it seem there are a space between the first 9 and the consecutive 0, try to clean this. Interestings this error was not shown before Aug 25, 2021 at 22:53

I can't comment -- where this would be more applicable -- but your y_train is class encoded (e.g., this sample's label is class 1), which is a single output. When your data are fed into the model w/ 10 output nodes, the model doesn't know what to do considering your y_train has 1 output for each sample.

A solution would be to one-hot encode your outputs (e.g., if your sample's label is class 1, it would be represented as [0,1,0,0,0,0,0,0,0,0]). Sklearn has a convinient OneHotEncoder to make the preprocessing simple. Now you have 10 outputs for each sample and the model can understand what's going on. Hopefully this helps

• did you mean that i should encode the y_trian before i feed it to the fit function ? using this way : enc = preprocessing.OneHotEncoder() enc.fit(y_train) onehotlabels = enc.transform(y_train).toarray() , but it always gives me this error Expected 2D array, got 1D array instead: Regards! Robert Aug 26, 2021 at 11:41
• Yes. You need to reshape your arrays. For each sample, it should initially be something like (,10), but it needs to either be reshaped to (1,10) or (10,1), I can't remember off the top of my head right now. Either way, it can be accomplished using the array's reshape command Aug 27, 2021 at 2:07
• i tried reshape(10,1) and reshape(1,10) it always gives me " ValueError: cannot reshape array of size 189 into shape (1,10)" then i tried " reshape(189,10) still gives me " ValueError: cannot reshape array of size 189 into shape (189,10) " Aug 27, 2021 at 3:33
• That's for each sample. Reshape(189,1,10) or reshape(189,10,1) for all your data Aug 27, 2021 at 3:49
• I'm sorry for bothering you Robert, but it still gives me "cannot reshape array of size 189 into shape (189,10,1)" or "cannot reshape array of size 189 into shape (189,1,10)" Aug 27, 2021 at 3:54

you should one_hot target(y) before model.fit & give it in training in such a form

y= tf.one_hot(y,10,)


you've got ok, just display results of each batch training:

history= model.fit(x_train, y_train, epochs=100, batch_size=32)
print(history.history['accuracy'])
print(history.history['loss'])


and can evaluate your model

test_loss, test_acc = model.evaluate(x_test, y_test,  verbose=2)
print('\nTest accuracy:', test_acc)


and at the end when predicting - choose from probabilities the best

predicted = model.predict(x)
Y2 = predicted.argmax( axis = 1)
print(Y2)


p.s. your 1st Dense better use Dense(units=64,..)