当前位置:   article > 正文

用于股票分析的双向lstm(Bi-LSTM)(pytorch)_csdn bilstm在金数k线图中的应用

csdn bilstm在金数k线图中的应用

完整工程链接: 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

预测值与真实值对比

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/913901
推荐阅读
相关标签
  

闽ICP备14008679号