使用自定义映射损失时的keras nan损失

时间:2020-08-03 08:42:26

标签: python tensorflow keras

我建立了一个简单的lstm网络,并使用了costom mape损失,如下所示:

def custom_mape(y_true, y_pred):
    mapes = K.switch(K.equal(y_true, 0), y_true, 100*K.abs(y_true - y_pred)/y_true)
    return K.mean(mapes, axis=-1)

损失从一开始就变成了微不足道:

Model: "sequential_93"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_163 (LSTM)              (None, 14, 1)             296       
=================================================================
Total params: 296
Trainable params: 296
Non-trainable params: 0
_________________________________________________________________
Epoch 1/50
410/410 [==============================] - 3s 7ms/step - loss: nan - val_loss: nan
Epoch 2/50
410/410 [==============================] - 2s 6ms/step - loss: nan - val_loss: nan
Epoch 3/50
410/410 [==============================] - 2s 6ms/step - loss: nan - val_loss: nan
Epoch 4/50
410/410 [==============================] - 2s 6ms/step - loss: nan - val_loss: nan
Epoch 5/50
410/410 [==============================] - 2s 6ms/step - loss: nan - val_loss: nan
Epoch 6/50
410/410 [==============================] - 2s 5ms/step - loss: nan - val_loss: nan
Epoch 7/50
410/410 [==============================] - 3s 6ms/step - loss: nan - val_loss: nan
Epoch 8/50
410/410 [==============================] - 2s 5ms/step - loss: nan - val_loss: nan
Epoch 9/50
410/410 [==============================] - 2s 5ms/step - loss: nan - val_loss: nan
Epoch 10/50
410/410 [==============================] - 2s 5ms/step - loss: nan - val_loss: nan

以下是我尝试过的一些方法:

  1. 当我将K.abs(y_true-y_pred)/ y_true更改为K.abs(y_true-y_pred)时,网络正常工作。
  2. 要弄清楚是否是梯度爆炸,我分别尝试了clipvalue = 1,lr = 0和batchsize = 1。损失依旧。

此外,我对y使用了最小-最大归一化,并且y的示例如下:

[[1.84368752e-05],
[9.86574098e-04],
[8.09853832e-04]]

1 个答案:

答案 0 :(得分:0)

K.abs(y_true - y_pred)/y_true

在这里,如果y_true0,您将得到nan,因为您正尝试除以0