完整工程链接: 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()
预测值与真实值对比
|