简体   繁体   English

凯拉斯:损失不断增加

[英]Keras: loss keeps increasing

Code: 码:

import keras
import numpy as np

x = []
y = []

for i in range(1000):
    x.append((i/10.0))
    y.append(2.71828 ** (i/10.0))

x = np.asarray(x)
y = np.asarray(y)
x = x.T
y = y.T

model = keras.models.Sequential()
model.add(keras.layers.Dense(1, input_dim=1, activation='relu'))
model.add(keras.layers.Dense(100, activation='relu'))
model.add(keras.layers.Dense(1))

model.compile(loss='mean_squared_error', optimizer=keras.optimizers.SGD(lr=0.001))
model.fit(x, y, batch_size=1, shuffle=False)

tx = [0.0, 1.0, 10.0]
tx = np.asarray(tx)
tx = tx.T

print(model.predict(tx))

This is a very simple neural net which intends to map e^x. 这是一个非常简单的神经网络,旨在映射e ^ x。 Its my first time using keras and when I run it, the loss keeps increasing to infinity. 这是我第一次使用keras,当我运行它时,损失不断增加到无穷大。 Instead it should decrease. 相反,它应该减少。

If a loss increases to infinity it is a sign that the learning rate is too high. 如果损失增加到无穷大,则表明学习率太高。 The problem with fitting e^x, where x = 100 as in your case is that the difference in values will be very large. 拟合e ^ x的问题(在您的情况下x = 100)是值的差异将非常大。 Therefore the gradient is going to be very large and the updates are going to be just as large, depending on your learning rate. 因此,根据您的学习速度,梯度将非常大,更新也将同样大。 However if your learning rate is very small, the network will be dominated by those large updates. 但是,如果您的学习率很小,那么网络将被大量更新所支配。 That is one of the reasons why values are normalized in deep learning. 这就是为什么在深度学习中将价值规范化的原因之一。

One quick solution would be to just use the first 100 values or decrease your step size. 一种快速的解决方案是仅使用前100个值或减小步长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM