LSTM代码部分(关于原理网上资料已经很多,这里只展示代码,数据集为开源ETT数据集)
1. 导入必须要的包
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import plotly.express as px
from sklearn import metrics
from torch.utils.data import Dataset, DataLoader
2. 定义LSTM网络(这里定义两个网络结构作对比)
# 定义模型
class Lstm(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, is_bidirectional, output_size, seq_length):
super(Lstm, self).__init__()
self.bidirectional = is_bidirectional
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first = True, bidirectional = self.bidirectional)
if self.bidirectional:
num_directions = 2
else:
num_directions = 1
self.fc1 = nn.Linear(hidden_size * num_directions * seq_length, output_size)
def forward(self, x):
###
# batch_first = True
# 因此input和output的第一维是batch
###
### lstm的input
# input (batch, seq_len, input_size)
# h_0 (num_layers * num_directions, batch, hidden_size) 这里存在疑问?,batch_first = True 和 False 是否一样
# c_0 (num_layers * num_directions, batch, hidden_size)
# 不输入默认初始为0
###
### lstm的output
# output (batch, seq_len, hidden_size * num_directions)
# h_n (num_layers * num_directions, batch, hidden_size)
# c_n (num_layers * num_directions, batch, hidden_size)
###
###
# 因为input和output的第一维是batch
# 对于lstm来说,想要的值应该是h_n[-1, :, :]或output[:, -1, :]
###
x, (h_n, c_n) = self.lstm(x)
batch_size, seq_length, hidden_size = x.shape
# print(x.shape)
# print(h_n.shape)
# print(c_n.shape)
x = x.contiguous()
x = x.view(-1, seq_length * hidden_size)
x = self.fc1(x)
return x
# 优化模型
class opt_Lstm(nn.Module):
def __init__(self, input_size, hidden_size, output_size, linear_size, num_layers, seq_length, is_bidirectional):