为什么我的损失函数随着每个时期而增加?

问题描述 投票:0回答:2

我是机器学习新手,所以如果这是任何人都可以弄清楚的愚蠢问题,我很抱歉。我在这里使用 TensorFlow 和 Keras。

这是我的代码:

import tensorflow as tf
import numpy as np
from tensorflow import keras
model = keras.Sequential([
    keras.layers.Dense(units=1, input_shape=[1])
])
model.compile(optimizer="sgd", loss="mean_squared_error")
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0], dtype=float)
ys = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0], dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([25.0]))

我得到这个作为输出[我没有显示全部 500 行,只显示 20 个时期:

Epoch 1/500
1/1 [==============================] - 0s 210ms/step - loss: 450.9794
Epoch 2/500
1/1 [==============================] - 0s 4ms/step - loss: 1603.0852
Epoch 3/500
1/1 [==============================] - 0s 10ms/step - loss: 5698.4731
Epoch 4/500
1/1 [==============================] - 0s 7ms/step - loss: 20256.3398
Epoch 5/500
1/1 [==============================] - 0s 10ms/step - loss: 72005.1719
Epoch 6/500
1/1 [==============================] - 0s 4ms/step - loss: 255956.5938
Epoch 7/500
1/1 [==============================] - 0s 3ms/step - loss: 909848.5000
Epoch 8/500
1/1 [==============================] - 0s 5ms/step - loss: 3234236.0000
Epoch 9/500
1/1 [==============================] - 0s 3ms/step - loss: 11496730.0000
Epoch 10/500
1/1 [==============================] - 0s 3ms/step - loss: 40867392.0000
Epoch 11/500
1/1 [==============================] - 0s 3ms/step - loss: 145271264.0000
Epoch 12/500
1/1 [==============================] - 0s 3ms/step - loss: 516395584.0000
Epoch 13/500
1/1 [==============================] - 0s 4ms/step - loss: 1835629312.0000
Epoch 14/500
1/1 [==============================] - 0s 3ms/step - loss: 6525110272.0000
Epoch 15/500
1/1 [==============================] - 0s 3ms/step - loss: 23194802176.0000
Epoch 16/500
1/1 [==============================] - 0s 3ms/step - loss: 82450513920.0000
Epoch 17/500
1/1 [==============================] - 0s 3ms/step - loss: 293086593024.0000
Epoch 18/500
1/1 [==============================] - 0s 5ms/step - loss: 1041834835968.0000
Epoch 19/500
1/1 [==============================] - 0s 3ms/step - loss: 3703408164864.0000
Epoch 20/500
1/1 [==============================] - 0s 3ms/step - loss: 13164500484096.0000

如您所见,它正在呈指数级增长。很快(在第 64 个纪元),这些数字变成了

inf
。然后,从无穷大开始,它做了一些事情并变成了
NaN
(不是数字)。我认为随着时间的推移,模型会更好地找出模式,这是怎么回事?

我注意到一件事,如果我将

xs
ys
的长度从 20 减少到 10,损失就会减少并变成
7.9193e-05
。当我将两个 numpy 数组的长度增加到
18
后,它开始不受控制地增加,否则就没有问题。我给出了 20 个值,因为我认为如果我提供更多数据,模型会更好,这就是为什么我给出了 20 个值。

python tensorflow keras artificial-intelligence loss-function
2个回答
3
投票

你的阿尔法/学习率似乎太大了。

尝试使用较低的学习率,如下所示:

import tensorflow as tf
import numpy as np
from tensorflow import keras
model = keras.Sequential([
    keras.layers.Dense(units=1, input_shape=[1])
])
# manually set the optimizer, default learning_rate=0.01
opt = keras.optimizers.SGD(learning_rate=0.0001)

model.compile(optimizer=opt, loss="mean_squared_error")
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0], dtype=float)
ys = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0], dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([25.0]))

...将会收敛。

ADAM 效果更好的原因之一可能是因为它自适应地估计学习率 - 我认为 ADAM 中的 A 代表自适应。

编辑:确实如此!

来自https://arxiv.org/pdf/1412.6980.pdf

该方法计算个体自适应学习率 梯度一阶矩和二阶矩估计的不同参数; 亚当这个名字 来自自适应矩估计

Epoch 1/500
1/1 [==============================] - 0s 129ms/step - loss: 1.2133
Epoch 2/500
1/1 [==============================] - 0s 990us/step - loss: 1.1442
Epoch 3/500
1/1 [==============================] - 0s 0s/step - loss: 1.0792
Epoch 4/500
1/1 [==============================] - 0s 1ms/step - loss: 1.0178
Epoch 5/500
1/1 [==============================] - 0s 1ms/step - loss: 0.9599
Epoch 6/500
1/1 [==============================] - 0s 1ms/step - loss: 0.9053
Epoch 7/500
1/1 [==============================] - 0s 0s/step - loss: 0.8538
Epoch 8/500
1/1 [==============================] - 0s 1ms/step - loss: 0.8053
Epoch 9/500
1/1 [==============================] - 0s 999us/step - loss: 0.7595
Epoch 10/500
1/1 [==============================] - 0s 1ms/step - loss: 0.7163
...
Epoch 499/500
1/1 [==============================] - 0s 1ms/step - loss: 9.9431e-06
Epoch 500/500
1/1 [==============================] - 0s 999us/step - loss: 9.9420e-06

编辑2:

使用真正的/“普通”梯度下降(相对于随机 GD),您应该看到每一步都收敛。如果你开始出现分歧,通常是因为 alpha/学习率/步长太大。这意味着搜索在一个、多个或所有维度上“超出”。

考虑一个损失函数,其偏导数/梯度在一维或多维上具有非常窄的谷值。 “迈得太远”可能意味着突然出现大错误。


1
投票

优化器 SGD 似乎在您的数据集上表现不佳。 如果您将优化器替换为“adam”,您应该会得到您期望的结果。

model.compile(optimizer="adam", loss="mean_squared_error")

预测应该是你所期望的

print(model.predict([25.0]))
# [[12.487587]]

我并不是 100% 明白为什么 SGD 优化器效果这么差。

编辑:

@MortenJensen(如下)很好地解释了为什么 adam 优化器做得更好。 总结:sgd 做得不好的原因是它需要较小的学习率。然而,Adam 具有自适应学习率。

© www.soinside.com 2019 - 2024. All rights reserved.