pytorch LSTM 模型不学习

pytorch LSTM model not learning

我创建了一个简单的 LSTM 模型来预测优衣库的收盘价。问题是,我的模型似乎没有学到任何东西。这是我 notebook

的 link

这是我创建的模型class(之前试过relu激活函数,结果一样):

class lstm(torch.nn.Module):
  def __init__(self,hidden_layers):
    super(lstm,self).__init__()
    self.hidden_layers = hidden_layers
    self.lstm = torch.nn.LSTM(input_size = 2,hidden_size = 100,num_layers = self.hidden_layers,batch_first=True)
    self.hidden1 = torch.nn.Linear(100,80)
    self.dropout1 = torch.nn.Dropout(0.1)
    self.hidden2 = torch.nn.Linear(80,60)
    self.dropout2 = torch.nn.Dropout(0.1)
    self.output = torch.nn.Linear(60,1)

  def forward(self,x):
    batch = len(x)
    h = torch.randn(self.hidden_layers,batch,100).requires_grad_().cuda()
    c = torch.randn(self.hidden_layers,batch,100).requires_grad_().cuda()

    x,(ho,co)= self.lstm(x.view(batch,10,2),(h.detach(),c.detach()))
    x = torch.reshape(x[:,-1,:],(batch,-1))
    x = self.hidden1(x)
    x = torch.nn.functional.tanh(x)
    x = self.dropout1(x)
    x = self.hidden2(x)
    x = torch.nn.functional.tanh(x)
    x = self.dropout2(x)
    x = self.output(x)
    return x

model = lstm(10)

这是我的训练损失图: training loss

这是我的验证损失图: validation loss

这是我的基本事实(蓝色)与预测(橙色): ground truth vs prediction

谁能指出我做错了什么?

你用整个数据训练定标器。这不是一个好的策略。您应该只使用训练数据。

您不必缩放目标。直接使用或者套用log函数或者使用returns.

关于隐藏状态和单元内存,为什么要跟踪梯度并在之后分离它们?在输入 lstm 层时,您不必分离隐藏状态和单元内存,因为它参与了反向传播。

如果我明白你做了什么,你会使用最近 10 个开盘价和交易量来预测下一个收盘价。我不认为你可以用这个配置得到好的结果。你应该更好地形式化问题。