import tensorflow as tf
from tensorflow import keras
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
xi=pd.read_csv(r'/content/ckd_full.csv')
xi=xi.drop(columns=['sg','su','pc','pcc','pcv','rbcc','wbcc'])
y=xi[['class']]
y['class']=y['class'].replace(to_replace=(r'ckd',r'notckd'), value=(1,0))
x=xi.drop(columns=['class'])
x['rbc']=x['rbc'].replace(to_replace=(r'normal',r'abnormal'), value=(1,0))
x['ba']=x['ba'].replace(to_replace=(r'present',r'notpresent'), value=(1,0))
x['htn']=x['htn'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['dm']=x['dm'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['cad']=x['cad'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['pe']=x['pe'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['ane']=x['ane'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['appet']=x['appet'].replace(to_replace=(r'good',r'poor'), value=(1,0))
x[x=="?"]=np.nan
d=['age', 'bp', 'al', 'rbc', 'ba', 'bgr', 'bu', 'sc', 'sod', 'pot', 'hemo', 'htn', 'dm','cad', 'appet', 'pe', 'ane']
for i in d:
x[i] = x[i].astype(float)
x.fillna(x.median(),inplace=True)
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.025)
#begin the model
model=keras.models.Sequential()
model.add(keras.layers.Dense(150,input_dim = 17, activation=tf.nn.relu))
#model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(100,input_dim = 17, activation=tf.nn.relu))
model.add(keras.layers.Dense(50,input_dim = 17, activation=tf.nn.relu))
model.add(keras.layers.Dense(10,input_dim = 17, activation=tf.nn.relu))
model.add(keras.layers.Dense(5,input_dim = 17, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # specifiying hyperparameters
xtrain_tensor = tf.convert_to_tensor(xtrain, dtype=tf.float32)
ytrain_tensor = tf.convert_to_tensor(ytrain, dtype=tf.float32)
model.fit(xtrain_tensor , ytrain_tensor , epochs=100, batch_size=128, validation_split = 0.15, shuffle=True, verbose=2) # load the model
#es = tf.keras.callbacks.EarlyStopping(monitor='accuracy', mode='min', verbose=1,patience=5)
model.save('NephrologistLite') # save the model with a unique name
myModel=tf.keras.models.load_model('NephrologistLite') # make an object of the model
I made this classification neural network for predictiong chronic kidney disease. The losses, while epochs are coming as NaN, though I dont have any NaN values in my dataset
My dataset:- https://drive.google.com/file/d/1iDOc5RUBq_zUOHPfspPDBMxvKIKfRIsr/view?usp=sharing
You can edit my code here:- https://colab.research.google.com/drive/1lXB7QoowiF3WaZ2LJV68r3A-a616v6hO?usp=sharing
You have 1 record in row #403 of your dataset (on the 2nd cell) which is causing the NaN gradient problem. Just delete it from your dataset and you will be good to go.
In addition: I removed the input_dims from the hidden layers as it isn't necessary. Tried to keep the code as close as possible to your code.
import tensorflow as tf
from tensorflow import keras
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
xi=pd.read_csv('ckd_full.csv')
xi=xi.drop(columns=['sg','su','pc','pcc','pcv','rbcc','wbcc'])
y=xi[['class']]
y['class']=y['class'].replace(to_replace=(r'ckd',r'notckd'), value=(1,0))
x=xi.drop(columns=['class'])
x['rbc']=x['rbc'].replace(to_replace=(r'normal',r'abnormal'), value=(1,0))
x['ba']=x['ba'].replace(to_replace=(r'present',r'notpresent'), value=(1,0))
x['htn']=x['htn'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['dm']=x['dm'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['cad']=x['cad'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['pe']=x['pe'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['ane']=x['ane'].replace(to_replace=(r'yes',r'no'), value=(1,0))
x['appet']=x['appet'].replace(to_replace=(r'good',r'poor'), value=(1,0))
x[x=="?"]=np.nan
d=['age', 'bp', 'al', 'rbc', 'ba', 'bgr', 'bu', 'sc', 'sod', 'pot', 'hemo', 'htn', 'dm','cad', 'appet', 'pe', 'ane']
for i in d:
x[i] = x[i].astype(float)
x.fillna(x.median(),inplace=True)
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.15)
#begin the model
model=keras.models.Sequential()
model.add(keras.layers.Dense(150,input_dim = 17, activation=tf.nn.relu))
#model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(100, activation=tf.nn.relu))
model.add(keras.layers.Dense(50, activation=tf.nn.relu))
model.add(keras.layers.Dense(10, activation=tf.nn.relu))
model.add(keras.layers.Dense(5, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # specifiying hyperparameters
xtrain_tensor = tf.convert_to_tensor(xtrain, dtype=tf.float32)
ytrain_tensor = tf.convert_to_tensor(ytrain, dtype=tf.float32)
model.fit(xtrain_tensor , ytrain_tensor , epochs=100, batch_size=128, validation_split = 0.15, shuffle=True, verbose=2) # load the model
model.evaluate(xtest, ytest)
#es = tf.keras.callbacks.EarlyStopping(monitor='accuracy', mode='min', verbose=1,patience=5)
#model.save('NephrologistLite') # save the model with a unique name
#myModel=tf.keras.models.load_model('NephrologistLite') # make an object of the model
These are the results I received:
Epoch 99/100
3/3 - 0s - loss: 0.2782 - accuracy: 0.8893 - val_loss: 0.2868 - val_accuracy: 1.0000
Epoch 100/100
3/3 - 0s - loss: 0.2706 - accuracy: 0.9343 - val_loss: 0.2926 - val_accuracy: 1.0000
Despite the fact that your model is doing pretty well with 100% validation accuracy, I'd suggest to remove some layers and involve a tanh layer in order to deal with the dummy variables. I hope that solves your problem :)