Epochs 损失将以 NaN 的形式出现

Epochs loss is coming as NaN

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

我做了这个分类神经网络来预测慢性肾病。损失,虽然时代以 NaN 出现,但我的数据集中没有任何 NaN 值

我的数据集:- https://drive.google.com/file/d/1iDOc5RUBq_zUOHPfspPDBMxvKIKfRIsr/view?usp=sharing

您可以在这里编辑我的代码:- https://colab.research.google.com/drive/1lXB7QoowiF3WaZ2LJV68r3A-a616v6hO?usp=sharing

您的数据集的第 403 行(第 2 个单元格)中有 1 条记录导致了 NaN 梯度问题。只需将其从您的数据集中删除即可。

另外: 我从隐藏层中删除了 input_dims,因为它不是必需的。尝试使代码尽可能接近您的代码。

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       

这些是我收到的结果:

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

尽管您的模型在 100% 的验证准确率方面做得很好,但我建议删除一些层并使用 tanh 层来处理虚拟变量。 我希望这能解决您的问题:)