python - Keras 网络产生逆向预测
问题描述
我有一个时间序列数据集,我正在尝试训练一个网络以使其过拟合(显然,这只是第一步,然后我将与过拟合作斗争)。
该网络有两层:LSTM(32 个神经元)和 Dense(1 个神经元,无激活)
训练/模型具有以下参数:
epochs: 20
、steps_per_epoch: 100
、loss: "mse"
、optimizer: "rmsprop"
。
TimeseriesGenerator
产生输入序列:length: 1
, sampling_rate: 1
, batch_size: 1
.
我希望网络只会记住这么小的数据集(我尝试了更复杂的网络但无济于事),训练数据集的损失几乎为零。事实并非如此,当我像这样可视化训练集上的结果时:
y_pred = model.predict_generator(gen)
plot_points = 40
epochs = range(1, plot_points + 1)
pred_points = numpy.resize(y_pred[:plot_points], (plot_points,))
target_points = gen.targets[:plot_points]
plt.plot(epochs, pred_points, 'b', label='Predictions')
plt.plot(epochs, target_points, 'r', label='Targets')
plt.legend()
plt.show()
我得到:
预测的幅度稍小,但与目标正好相反。顺便提一句。这不是记忆的,即使对于算法根本没有训练过的测试数据集,它们也会被反转。似乎我的网络没有记住数据集,而是学会了否定输入值并稍微缩小它。知道为什么会这样吗?优化器似乎不是应该收敛的解决方案(损失很大)。
编辑(我的代码的一些相关部分):
train_gen = keras.preprocessing.sequence.TimeseriesGenerator(
x,
y,
length=1,
sampling_rate=1,
batch_size=1,
shuffle=False
)
model = Sequential()
model.add(LSTM(32, input_shape=(1, 1), return_sequences=False))
model.add(Dense(1, input_shape=(1, 1)))
model.compile(
loss="mse",
optimizer="rmsprop",
metrics=[keras.metrics.mean_squared_error]
)
history = model.fit_generator(
train_gen,
epochs=20,
steps_per_epoch=100
)
编辑(不同的,随机生成的数据集):
我不得不将 LSTM 神经元的数量增加到 256 个,使用之前的设置(32 个神经元),蓝线非常平坦。然而,随着增加,出现相同的模式——幅度稍小的逆预测。
编辑(目标移动+1):
与预测相比,将目标移动 1 并不会产生更好的拟合。注意图中突出显示的部分不仅是交替的,而且在那里更明显。
编辑(将长度增加到 2 ... TimeseriesGenerator(length=2, ...)
):
随着length=2
预测停止如此密切地跟踪目标,但反转的整体模式仍然存在。
解决方案
编辑:经过作者的评论,我不相信这是正确的答案,但我会为后代保留它。
很好的问题和答案是由于 Time_generator 的工作原理!显然,它不是抓取具有相同索引的 x,y 对(例如输入x[0]
到输出目标y[0]
),而是抓取偏移量x[0]
为1 的目标(因此y[1]
)。
因此,用偏移量 1 绘制 y 将产生所需的拟合。
模拟代码:
import keras
import matplotlib.pyplot as plt
x=np.random.uniform(0,10,size=41).reshape(-1,1)
x[::2]*=-1
y=x[1:]
x=x[:-1]
train_gen = keras.preprocessing.sequence.TimeseriesGenerator(
x,
y,
length=1,
sampling_rate=1,
batch_size=1,
shuffle=False
)
model = keras.models.Sequential()
model.add(keras.layers.LSTM(100, input_shape=(1, 1), return_sequences=False))
model.add(keras.layers.Dense(1))
model.compile(
loss="mse",
optimizer="rmsprop",
metrics=[keras.metrics.mean_squared_error]
)
model.optimizer.lr/=.1
history = model.fit_generator(
train_gen,
epochs=20,
steps_per_epoch=100
)
正确的绘图:
y_pred = model.predict_generator(train_gen)
plot_points = 39
epochs = range(1, plot_points + 1)
pred_points = np.resize(y_pred[:plot_points], (plot_points,))
target_points = train_gen.targets[1:plot_points+1] #NOTICE DIFFERENT INDEXING HERE
plt.plot(epochs, pred_points, 'b', label='Predictions')
plt.plot(epochs, target_points, 'r', label='Targets')
plt.legend()
plt.show()
输出,请注意拟合不再反转并且非常准确:
这是偏移不正确时的样子:
推荐阅读
- ubuntu - 如何直接在当前目录中提取 zip 中的文件?
- flask - 生产就绪服务器是什么意思?
- python - 静态媒体图像未在 Django 中显示
- c# - 如何编写状态机,这样我就不必用 99% 相似的代码重复状态?
- python - 将垂直滚动条添加到 nbsphinx 中的 Jupyter 笔记本代码/输出单元格
- azure - AD 连接问题(设备连接类型问题)
- angular - Angular `font:inherit` 会破坏字体
- python-3.x - 如何使用 Python 将 pdf 文件发送到多个电子邮件?
- python - 在 Azure Synapse 笔记本 ValueError 中运行 nltk.download:对已关闭文件的 I/O 操作
- python - 重新采样多边形坐标以均匀分布它们