[英]Loss is NaN using activation softmax and loss function categorical_crossentropy
I'm trying to make this model work.我正在尝试使这个 model 工作。 Initially
x.shape
is (6703, 56) and y.shape
is a binary column having shape (6703, ).最初
x.shape
是 (6703, 56) 并且y.shape
是具有形状 (6703, ) 的二元列。 Then I run然后我跑
y = y.to_numpy()
y = y.astype("float32")
y = tf.keras.utils.to_categorical(y, 2)
and now y.shape
is (6703, 2).现在
y.shape
是 (6703, 2)。 I run我跑
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=0.2, random_state=42)
and now现在
X_train
shape is (5362, 56) X_train
形状为 (5362, 56)Y_train
shape is (5362, 2) Y_train
形状为 (5362, 2)X_test
shape is (1341, 56) X_test
形状为 (1341, 56)Y_test
shape is (1341, 2) Y_test
形状为 (1341, 2) Then I build the model:然后我构建 model:
model = tf.keras.models.Sequential(name="3layers")
model.add(keras.layers.Dense(N_HIDDEN,
input_shape=(len(X_train[0]),),
name="dense1",
activation="relu"))
model.add(keras.layers.Dropout(DROPOUT))
model.add(keras.layers.Dense(N_HIDDEN,
name="dense2",
activation="relu"))
model.add(keras.layers.Dropout(DROPOUT))
model.add(keras.layers.Dense(NB_CLASSES,
name="dense3",
activation="softmax"))
model.summary()
model.compile(optimizer="SGD", #SGD adam
loss="categorical_crossentropy",
metrics=["accuracy"])
model.fit(X_train, Y_train,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
verbose=VERBOSE,
validation_split=VALIDATION_SPLIT)
test_loss, test_acc = model.evaluate(X_test, Y_test)
The summary is what I expect:总结是我所期望的:
dense1 (Dense) (None, 64) 3648
dropout_18 (Dropout) (None, 64) 0
dense2 (Dense) (None, 64) 4160
dropout_19 (Dropout) (None, 64) 0
dense3 (Dense) (None, 2) 130
but the output is但 output 是
Epoch 1/5
> 429/429 [==============================] - 1s 1ms/step - loss: nan - accuracy: 0.5141 - val_loss: nan - val_accuracy: 0.4884
Epoch 2/5
> 429/429 [==============================] - 0s 1ms/step - loss: nan - accuracy: 0.5143 - val_loss: nan - val_accuracy: 0.4884
Epoch 3/5
> 429/429 [==============================] - 0s 987us/step - loss: nan - accuracy: 0.5143 - val_loss: nan - val_accuracy: 0.4884
I've tried changing many parameters, I'm stuck.我试过改变很多参数,我被卡住了。
I found what it was.我发现它是什么。 There were some "None" values in the x matrix that caused the problem.
x 矩阵中有一些“无”值导致了问题。 Removing them it started evaluating a numeric loss.
删除它们,它开始评估数字损失。 Very poor accuracy, but this will be another problem to solve.
精度很差,但这将是另一个需要解决的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.