首页 > 解决方案 > 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()

标签: pythondeep-learninglstmpytorchrecurrent-neural-network

解决方案


推荐阅读