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
uploaded = files.upload()

import io
dset = pd.read_csv(io.BytesIO(uploaded['1-210.csv']))

y= dset.Readernumber
x=dset.drop('Readername',axis=1)

#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.
model.compile(optimizer=Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy']) 

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

Topic keras tensorflow deep-learning machine-learning

Category Data Science


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,..)


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.