pytorch LSTM模型无法学习

时间:2020-09-20 06:52:50

标签: python deep-learning pytorch artificial-intelligence

我创建了一个简单的LSTM模型来预测Uniqlo的收盘价。问题是,我的模型似乎什么也没学。这是指向我的notebook

的链接

这是我的模型创建类(我之前尝试过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

任何人都可以指出我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

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

您不必缩放目标。直接使用它或应用日志功能或使用退货。

关于隐藏状态和单元内存,为什么要保持跟踪渐变并将其分离呢?喂lstm层时,您不必分离隐藏状态和单元存储器,因为它参与了反向传播。

如果我了解您的操作,则可以使用最近10个未平仓价格和交易量来预测下一个收盘价。我认为使用此配置无法获得良好的结果。您应该更好地将问题形式化。