首页 > 解决方案 > RNN 的 Tensorflow NAN 损失结果

问题描述

我在 Windows 10 中使用 Anaconda 环境。我使用的是 python 3.6.13、numpy 1.19.5 和 tensorflow 2.1.0

我正在尝试为时间序列预测创建一个 RNN,但我一直将 nan 作为损失值。我使用以下函数作为损失函数:

warmup_steps = 50
def loss_mse_warmup(y_true, y_pred):
    """
    Calculate the Mean Squared Error between y_true and y_pred,
    but ignore the beginning "warmup" part of the sequences.
    
    y_true is the desired output.
    y_pred is the model's output.
    """

    # The shape of both input tensors are:
    # [batch_size, sequence_length, num_y_signals].

    # Ignore the "warmup" parts of the sequences
    # by taking slices of the tensors.
    y_true_slice = y_true[:, warmup_steps:, :]
    y_pred_slice = y_pred[:, warmup_steps:, :]

    # These sliced tensors both have this shape:
    # [batch_size, sequence_length - warmup_steps, num_y_signals]

    # Calculat the Mean Squared Error and use it as loss.
    mse = mean(square(y_true_slice - y_pred_slice))
    
    return mse

此外,在格式化数据时,我删除了所有带有 NAN 的列df.dropna()

这是我的模型

model = Sequential()

model.add(GRU(units=128, return_sequences=True, input_shape=(None, num_x_signals)))

model.add(Dense(num_y_signals, activation='sigmoid')) 

optimizer = Adam(clipvalue=1)

model.compile(loss=loss_mse_warmup, optimizer=optimizer)

我的模型只返回 nan 作为损失结果,有谁知道为什么会发生这种情况以及我该如何解决?

标签: pythontensorflow

解决方案


推荐阅读