tf.keras给出nan丢失和非验证错误

时间:2018-08-15 04:59:14

标签: python tensorflow keras deep-learning

我正在尝试使用tf.keras API编程深度神经网络,我觉得我的模型是正确的,我删除了所有的nan值,但是在训练过程中仍然得到NAN值。 我一直在使用的数据集是来自UCI here

的Wiscon Cancer数据集

我的代码:

from tensorflow import keras
import pandas as pd
import tensorflow as tf

df = pd.read_csv('breastc.csv.csv')
df.dropna()
id_ = df['ID'].tolist()
del df['ID']
labels = df['Class'].tolist()
import numpy as np
del df['Class']
column_list='Compactness'
df[column_list] = df[column_list].apply(pd.to_numeric, errors='coerce')

model = keras.Sequential()
model.add(keras.layers.Dense(64,activation='relu',input_shape = (9,)))
model.add(keras.layers.Dense(64,activation='relu'))
model.add(keras.layers.Dense(1,activation='softmax'))

model.summary()

X=df.iloc[:].values

model.compile(optimizer=tf.train.AdamOptimizer(0.01),
              loss='mse',       # mean squared error
              metrics=['mae'])
model.fit(X,labels,batch_size=32,epochs=10,validation_split=0.2)

在fit语句之后,我得到以下结果

Train on 559 samples, validate on 140 samples
Epoch 1/10
559/559 [==============================] - 0s 599us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 2/10
559/559 [==============================] - 0s 82us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 3/10
559/559 [==============================] - 0s 86us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 4/10
559/559 [==============================] - 0s 84us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 5/10
559/559 [==============================] - 0s 87us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 6/10
559/559 [==============================] - 0s 83us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 7/10
559/559 [==============================] - 0s 80us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 8/10
559/559 [==============================] - 0s 77us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 9/10
559/559 [==============================] - 0s 73us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
Epoch 10/10
559/559 [==============================] - 0s 62us/step - loss: nan - mean_absolute_error: nan - val_loss: nan - val_mean_absolute_error: nan
<tensorflow.python.keras._impl.keras.callbacks.History at 0x15c94a80cc0>

我们看到没有进行任何培训。 请引导我。

您真诚的, Vidit Shah

1 个答案:

答案 0 :(得分:0)

当您有2个或更多类别的分类问题并且只想选择其中一个类别时,通常应将最后一层的输出神经元数等于类别数,并将其激活函数设置为softmax (因此,您将在类上分配一个输出)。一旦获得输出分布,就可以将接收概率最高的输出向量(即类别)中的索引视为预测。

您应该更改的另一件事是损失函数,当您使用softmax作为输出激活函数时,您需要使用交叉熵损失来测量两个分布之间的距离(在这种情况下,网络的输出和黄金分布-全0和索引中对应正确类别的1)。如果您将黄金标签表示为正确类别的索引(在您的情况下为0或1),则可以通过将sparse_corssentropy设置为损失函数来直接使用这些索引(它将自动将索引转换为一热向量)。

要包装所有内容,可以按以下方式转换代码:

from tensorflow import keras
import pandas as pd
import tensorflow as tf

df = pd.read_csv('breastc.csv.csv')
df.dropna()
id_ = df['ID'].tolist()
del df['ID']
labels = df['Class'].tolist()
import numpy as np
del df['Class']
column_list='Compactness'
df[column_list] = df[column_list].apply(pd.to_numeric, errors='coerce')

model = keras.Sequential()
model.add(keras.layers.Dense(64,activation='relu',input_shape = (9,)))
model.add(keras.layers.Dense(64,activation='relu'))
model.add(keras.layers.Dense(2,activation='softmax'))

model.summary()

X=df.iloc[:].values

model.compile(optimizer=tf.train.AdamOptimizer(0.01),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(X,labels,batch_size=32,epochs=10,validation_split=0.2)