我正在尝试在 64 个随机生成的数据点上训练单个感知器(1000 个输入单元,1 个输出,无隐藏层)。我正在使用使用 Adam 优化器的 Pytorch:
import torch
from torch.autograd import Variable
torch.manual_seed(545345)
N, D_in, D_out = 64, 1000, 1
x = Variable(torch.randn(N, D_in))
y = Variable(torch.randn(N, D_out))
model = torch.nn.Linear(D_in, D_out)
loss_fn = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.Adam(model.parameters())
for t in xrange(5000):
y_pred = model(x)
loss = loss_fn(y_pred, y)
print(t, loss.data[0])
optimizer.zero_grad()
loss.backward()
optimizer.step()
最初,损失迅速减少,正如预期的那样:
(0, 91.74887084960938)
(1, 76.85824584960938)
(2, 63.434078216552734)
(3, 51.46927261352539)
(4, 40.942893981933594)
(5, 31.819372177124023)
大约 300 次迭代,误差接近于零:
(300, 2.1734419819452455e-12)
(301, 1.90354676465887e-12)
(302, 2.3347573874232808e-12)
这持续了几千次迭代。但是,训练时间过长,误差又开始增加:
(4997, 0.002102422062307596)
(4998, 0.0020302983466535807)
(4999, 0.0017039275262504816)
为什么会这样?