python - pytorch LSTM 不会过拟合单个样本
问题描述
我尝试过拟合单个时间序列。意思是,我尝试(X,Y)
一遍又一遍地对一对进行训练。我这样做是为了对超参数的功能有个印象。但它不收敛。这是显示大约 800 次迭代后每次迭代的 MSE 的损失图:
我希望错误完全消失,但在我写这篇文章时,它卡在了高原上,它仍然存在。时间序列具有长度29600
,RNN 将单个值映射到另一个单个值。它由一个带有1
输入的 LSTM 单元、200
隐藏单元和一个完全连接的层映射到单个值
我的感觉告诉我,模型可能不够复杂,无法适应样本。但在我尝试增加 RNN 的复杂性之前,我必须确保我的训练实施是正确的。也许我没有正确使用 autograd 。由于这是第一次尝试训练神经网络,我不知道需要多长时间。
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
# the class containing the LSTM
class Sequence(nn.Module):
def __init__(self):
self.hidden_state_len = 200
super(Sequence, self).__init__()
self.lstm1 = nn.LSTMCell(1, self.hidden_state_len)
self.linear = nn.Linear(self.hidden_state_len, 1)
h_t = torch
def forward(self, input):
outputs = []
h_t = torch.zeros(input.size(0), self.hidden_state_len, dtype=torch.double).cuda()
c_t = torch.zeros(input.size(0), self.hidden_state_len, dtype=torch.double).cuda()
for i, input_t in enumerate(input.chunk(input.size(1), dim=1)):
h_t, c_t = self.lstm1(input_t, (h_t, c_t))
output = self.linear(h_t)
outputs += [output]
return torch.cat(outputs, dim=1)
x1 = torch.load("/floyd/input/wav/x1.pt").double().cuda()[0][7400:37000].reshape(1,-1)
y1 = torch.load("/floyd/input/wav/y1.pt").double().cuda()[0][7400:37000].reshape(1,-1)
seq = Sequence()
seq.double()
criterion = nn.MSELoss()
seq = seq.cuda()
device = torch.device("cuda:0")
seq = seq.to(device)
optimizer = optim.Adam(seq.parameters())
starttime = datetime.datetime.now()
i = -1
# training for 4 hours on cloud GPU
while((datetime.datetime.now() - starttime).total_seconds() < 60*60*4) :
i+=1
optimizer.zero_grad()
input = Variable(x1)
target = Variable(y1)
out = seq(input)
loss = criterion(out, target)
loss.backward()
optimizer.step()
解决方案
推荐阅读
- c++ - 在字符串中搜索单词的频率(C++)
- openedge - 你好我怎么能联合表进行中4gl?并显示
- javascript - 如何显示使用 Formik 和 yup 的多个错误行?
- javafx - 在 JavaFx 中的指定开始时间连续播放声音剪辑
- ios - 应该使用哪个缓存策略?
- python - 无法在 Kaggle 内核中使用“kaggle.competitions”
- elasticsearch - 询问关于本体和graphdb的问题
- docker - Google 是否有用于其 OCR/读取 API 的 On Premise Docker 容器,类似于 MSAzure 认知服务
- ruby-on-rails - 有什么方法可以在 ROR 中导出(生成)可编辑的 PDF?
- go - 是否有任何配置不要在特定环境中使用跟踪