-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
111 lines (94 loc) · 3.4 KB
/
model.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
from typing import TypeAlias
import torch
import torch.nn as nn
from torch import Tensor
class LSTMModel(nn.Module):
def __init__(
self,
input_size: int,
hidden_size: int,
output_size: int,
device: torch.device,
num_layers: int = 1,
) -> None:
super().__init__()
# num_features, hidden_size, num_layers
# features means the no. of features per sample
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.linear = nn.Linear(hidden_size, output_size)
self.num_layers = num_layers
self.hidden_size = hidden_size
self.device = device
def forward(self, seq: Tensor):
h0, c0 = self.hidden
lstm_out, self.hidden = self.lstm(seq, (h0, c0))
pred = self.linear(lstm_out)
return pred[:, -1]
def reset_hidden(self, batch_size: int):
self.hidden = (
torch.zeros(self.num_layers, batch_size, self.hidden_size).to(self.device),
torch.zeros(self.num_layers, batch_size, self.hidden_size).to(self.device),
)
class GRUModel(nn.Module):
def __init__(
self,
input_size: int,
hidden_size: int,
output_size: int,
device: torch.device,
num_layers: int = 1,
) -> None:
super().__init__()
# num_features, hidden_size, num_layers
# features means the no. of features per sample
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.linear = nn.Linear(hidden_size, output_size)
self.num_layers = num_layers
self.hidden_size = hidden_size
self.device = device
def forward(self, seq: Tensor):
h0, c0 = self.hidden
lstm_out, self.hidden = self.lstm(seq, (h0, c0))
pred = self.linear(lstm_out)
return pred[:, -1]
def reset_hidden(self, batch_size: int):
self.hidden = (
torch.zeros(self.num_layers, batch_size, self.hidden_size).to(self.device),
torch.zeros(self.num_layers, batch_size, self.hidden_size).to(self.device),
)
class RNNModel(nn.Module):
def __init__(
self,
n_features: int,
hidden_size: int,
output_size: int,
device: torch.device,
num_layers: int = 1,
) -> None:
super().__init__()
# num_features, hidden_size, num_layers
# features means the length of the vector representing the input
self.rnn = nn.RNN(
n_features, hidden_size, num_layers=num_layers, batch_first=True
)
self.linear = nn.Linear(hidden_size, output_size)
self.num_layers = num_layers
self.hidden_size = hidden_size
self.device = device
def forward(self, x: Tensor):
rnn_out, self.hidden = self.rnn(x, self.hidden)
pred = self.linear(rnn_out)
# only return the last prediction
return pred[:, -1]
def reset_hidden(self, batch_size: int):
self.hidden = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(
self.device
)
TimeSeriesModel: TypeAlias = LSTMModel | RNNModel | GRUModel
def get_available_device() -> torch.device:
device = torch.device("cpu")
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available(): # type: ignore
device = torch.device("mps")
return device