-
Notifications
You must be signed in to change notification settings - Fork 1
/
lstm_oneshot_multistep.py
160 lines (129 loc) · 5.51 KB
/
lstm_oneshot_multistep.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Import relevant libraries
import torch
import torch.nn as nn
class MultiStepLSTMSingleLayer(nn.Module):
"""
A PyTorch neural network model using an LSTM for multi-step time series forecasting.
Attributes:
n_variates (int): Number of input variables (features).
hidden_size (int): Number of features in the hidden state of the LSTM.
n_layers (int): Number of recurrent layers in the LSTM.
output_size (int): Number of features in the output/forecasted values.
device (str): Device on which the model is being run (e.g., 'cuda' or 'cpu').
Methods:
forward(x):
Performs a forward pass through the LSTM layer.
Example:
model = MultiStepLstmSingleLayer(n_variates, hidden_size, n_layers, output_size, device)
"""
def __init__(self, n_variates, hidden_size, n_layers, output_size, device):
"""
Initializes the MultiStepLSTM model.
Parameters:
- n_variates (int): Number of input variables (features).
- hidden_size (int): Number of features in the hidden state of the LSTM.
- n_layers (int): Number of recurrent layers in the LSTM.
- output_size (int): Number of features in the output/forecasted values.
- device (str): Device on which the model is being run (e.g., 'cuda' or 'cpu').
"""
super().__init__()
# Set model attributes
self.n_variates = n_variates
self.hidden_size = hidden_size
self.n_layers = n_layers
self.output_size = output_size
self.device = device
# LSTM layer with specified input size, hidden size, and batch_first
self.lstm = nn.LSTM(
input_size=self.n_variates,
hidden_size=self.hidden_size,
num_layers=self.n_layers,
batch_first=True,
)
# Linear layer mapping the LSTM output to the forecasted values
self.linear = nn.Linear(self.hidden_size, self.output_size)
def forward(self, x):
"""
Performs a forward pass through the LSTM layer.
Parameters:
- x (torch.Tensor): Input data tensor with shape (batch_size, seq_length, n_variates).
Returns:
- torch.Tensor: Output tensor with shape (batch_size, output_size).
"""
# Initialize hidden state and cell state
h0 = torch.zeros(self.n_layers, x.size(0), self.hidden_size).to(
self.device
)
c0 = torch.zeros(self.n_layers, x.size(0), self.hidden_size).to(
self.device
)
# LSTM layer
lstm_out, _ = self.lstm(x, (h0, c0))
# Extract the last time step output from the LSTM output
lstm_out = lstm_out[:, -1, :]
# Linear layer for the final output (forecasted values)
output = self.linear(lstm_out)
return output
class MultiStepLSTMMultiLayer(nn.Module):
"""
A PyTorch neural network model using an LSTM for multi-step time series forecasting.
Attributes:
n_variates (int): Number of input variables (features).
hidden_size (int): Number of features in the hidden state of the LSTM.
n_layers (int): Number of recurrent layers in the LSTM.
output_size (int): Number of features in the output/forecasted values.
device (str): Device on which the model is being run (e.g., 'cuda' or 'cpu').
Methods:
forward(x):
Performs a forward pass through the LSTM layer.
Example:
model = MultiStepLstmMultiLayer(n_variates, hidden_size, n_layers, output_size, device)
"""
def __init__(self, n_variates, hidden_size, n_layers, output_size, device):
"""
Initializes the MultiStepLSTM model.
Parameters:
- n_variates (int): Number of input variables (features).
- hidden_size (int): Number of features in the hidden state of the LSTM.
- n_layers (int): Number of recurrent layers in the LSTM.
- output_size (int): Number of features in the output/forecasted values.
- device (str): Device on which the model is being run (e.g., 'cuda' or 'cpu').
"""
super().__init__()
# Set model attributes
self.n_variates = n_variates
self.hidden_size = hidden_size
self.n_layers = n_layers
self.output_size = output_size
self.device = device
# LSTM layer with specified input size, hidden size, and batch_first
self.lstm = nn.LSTM(
input_size=self.n_variates,
hidden_size=self.hidden_size,
num_layers=self.n_layers,
batch_first=True,
)
self.fc1 = nn.Linear(n_layers * hidden_size, 128)
self.fc2 = nn.Linear(128, output_size)
self.relu = nn.ReLU6()
def forward(self, x):
"""
Performs a forward pass through the LSTM layer.
Parameters:
- x (torch.Tensor): Input data tensor with shape (batch_size, seq_length, n_variates).
Returns:
- torch.Tensor: Output tensor with shape (batch_size, output_size).
"""
h0 = torch.zeros(self.n_layers, x.size(0), self.hidden_size).to(
x.device
)
c0 = torch.zeros(self.n_layers, x.size(0), self.hidden_size).to(
x.device
)
_, (hn, cn) = self.lstm(x, (h0, c0))
hn = hn.view(x.size(0), self.n_layers * self.hidden_size)
out = self.relu(hn)
out = self.fc1(out)
out = self.relu(out)
out = self.fc2(out)
return out