# 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.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

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 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 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 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 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 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
• 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 at 2:07