asd*_*sdf 8 python machine-learning keras tensorflow
我有一个时间序列数据集,我正在尝试训练一个网络,以便它过度拟合(显然,这只是第一步,我将与过度拟合作斗争).
网络有两层:LSTM(32个神经元)和密集(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()
Run Code Online (Sandbox Code Playgroud)
我明白了:
预测的幅度稍微小一些,但与目标正好相反.顺便说一句.这是没有记忆的,即使对于算法没有训练过的测试数据集,它们也是反转的.似乎我的网络不是记住数据集而是学会否定输入值并稍微缩小它.知道为什么会这样吗?它似乎不是优化器应该收敛的解决方案(损失相当大).
编辑(我的代码的一些相关部分):
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
)
Run Code Online (Sandbox Code Playgroud)
编辑(不同的,随机生成的数据集):
我不得不将LSTM神经元的数量增加到256,使用之前的设置(32个神经元),蓝线几乎是平坦的.然而,随着增加,出现相同的模式 - 具有稍小幅度的反向预测.
编辑(目标移动+1):
与预测相比,将目标移动一个并不会产生更好的拟合.注意图形不仅仅是交替的突出显示的部分,它在那里更加明显.
编辑(长度增加到2 ...... TimeseriesGenerator(length=2, ...)):
随着length=2预测停止跟踪目标如此密切,但整体反转模式仍然存在.