LSTM时序预测loss值为nan

LSTM时序预测loss值为nan_第1张图片
当loss 显示为 nan时,首先检查训练集中是否存在nan值,可以用np.isnan()方法进行查看,如果数据集没问题再检查下损失函数会否适合当前模型,

def trainModel(train_X, train_Y):
    """
    trainX,trainY: 训练LSTM模型所需要的数据
    """
    model = Sequential()
    model.add(LSTM(units=50, activation='relu',
                   input_shape=(train_X.shape[1], train_X.shape[2]),
                   return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(units=50, activation='relu', return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(units=50, return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(units=32))
    model.add(Dropout(0.2))
    model.add(Dense(train_Y.shape[1], activation='linear'))
    model.compile(loss='mse', optimizer='adam', loss_weights=0.00001)
    model.fit(train_X, train_Y, epochs=20, batch_size=128, verbose=1)
    return model

最后还可以尝试更改学习率来查看loss值情况,通常学习率的取值有:0.1,0.01, 0.001, 0.00001

你可能感兴趣的:(知识点运用)