-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSTM.py
28 lines (20 loc) · 775 Bytes
/
LSTM.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
import torch
import torch.nn as nn
import warnings
warnings.simplefilter("ignore")
# Define device
device = "cuda" if torch.cuda.is_available() else "cpu"
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, n_layers):
super().__init__()
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.lstm = nn.LSTM(input_dim, hidden_dim, n_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, 1)
def forward(self, x):
batch_size = x.size(0)
h0 = torch.zeros(self.n_layers, batch_size, self.hidden_dim).to(device)
c0 = torch.zeros(self.n_layers, batch_size, self.hidden_dim).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
return out