python - 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 作为损失结果,有谁知道为什么会发生这种情况以及我该如何解决?
解决方案
推荐阅读
- javascript - 如何在 React Native 中设置 Android 屏幕、分辨率、纵横比和大小?
- c# - 如何从 C# 中的树视图中删除选定的父节点
- matlab-figure - 带有高程数据的真实极坐标 3D 图
- android - 有没有办法在运行时获取布局文件中的所有视图
- bash - 错误!冲突的操作语句:命令、调试
- flutter - Flutter 发布版本崩溃
- mongodb - Mongo 聚合匹配 + 总和组 - 如果没有匹配则返回 0
- rxandroidble - RXAndroidBLE 无法扫描设备和读取特征
- javascript - 如何使用带有续集的节点中的关系的软删除
- django - 系统管理员的 REST api