我正在使用Keras并使用LSTM神经网络跟踪this tutorial进行时间序列预测 . 在本教程中, adam
用作优化程序, mean_square_error
用作损失函数 .
我玩了一些优化器和损失函数,只是为了开始学习更多关于NN的东西,虽然我已经研究了一下这个理论,但实践是我现在想念的 .
所以我注意到一些我无法完全解释的行为 . 我试图使用 mean_absolute_percentage_error
作为损失函数,因为这是一个经常用于时间序列预测的缩放度量,它也是我想用来评估我的系统的 . 然后我尝试用 adam
拟合网络,并得到了一些结果 . 在此过程中,显示的损失值按预期减少直到一定数量的历元,从大约100的值开始并减少到大约60.这是一个片段 .
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 99.1431
Running epoch 23
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 99.0501
Running epoch 24
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 98.9580
Running epoch 25
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 98.8667
Running epoch 26
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 98.7761
Running epoch 27
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 98.6863
然后我尝试 SGD
作为优化器,仍然使用 mean_absolute_percentage_error
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.99, nesterov=True)
我得到的损失值完全不同
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 53223.5102
Running epoch 4
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 53253.8156
Running epoch 5
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 53253.9768
Running epoch 6
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 53252.5767
Running epoch 7
Epoch 1/1
28/28 [==============================] - 0s 2ms/step - loss: 53251.0539
我用 SGD
的学习率稍微降低了一点,降低了它的单位0.0001,我得到的损失值更接近我得到的 adam
,虽然仍然从170开始然后减少 .
最重要的是,如果我切换回 mean_square_error
作为损失函数,我认为 adam
和 SGD
之间的损失值范围没有太大差异(尽管它们可能以不同的速度减小) .
现在,我对优化如何工作有一些想法,虽然我理解了这种行为上的差异,特别是为什么 SGD
只是在 mean_absolute_percentage_error
被用作损失的情况下从 adam
开始这么大的不同,而当 mean_square_error
是应用时,两个优化器的行为类似 .
PS:如果你想知道它为什么说Epoch 1/1,那是因为它来自教程 . 这是一个有状态的LSTM,我直接控制时代,必要时重置状态 .