用于股票分析的双向lstm(Bi-LSTM)(pytorch)

完整工程链接: link.

bilstm原理图:

在这里插入图片描述

股票预测:

通过使用单层的Bi-LSTM来实现股票收盘价的预测,数据集采用的是平安银行的数据

代码:

下面展示一些 model代码。

import torch
from torch import nn

time_step = 20
input_size = 10
learning_rate = 0.001
hidden_size = 64
num_layers = 1
end_lenth=6500
class Bi_LSTM(nn.Module):
    """搭建LSTM"""
    def __init__(self):
        super(Bi_LSTM, self).__init__()
        # LSTM层
        self.lstm = nn.LSTM(input_size=input_size,      # 输入单元个数
                            hidden_size=hidden_size,    # 隐藏单元个数
                            num_layers=num_layers,      # 隐藏层数
                            batch_first=True,           # True:[batch, time_step, input_size] False:[time_step, batch, input_size]
                            bidirectional=True)

        # 输出层
        self.output_layers = nn.Linear(in_features=hidden_size*2,    # 输入特征个数
                                       out_features=1)  # 输出特征个数

    def forward(self, x):
        lstm_out, (h_n, h_c) = self.lstm(x, None)   #
        # print(lstm_out.shape)
        output = self.output_layers(lstm_out[:, -1, :])    # 选择最后一个时刻的LSTM作为输出
        return output

# lstm=Bi_LSTM()

预测值与真实值对比

在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值