-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
43 lines (30 loc) · 1.37 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
import torch
import torch.nn as nn
from torch.autograd import Variable
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers):
super(LSTM, self).__init__()
self.num_layers = num_layers
self.input_size = input_size
self.hidden_size = hidden_size
# Tensors in (batch, seq, feature)
self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,
num_layers=num_layers, batch_first=True)
self.fc1 = nn.Linear(hidden_size, 1)
self.fc2 = nn.Linear(hidden_size, 1)
# self.hidden1 = nn.Linear(hidden_size, hidden_size)
# self.hidden2 = nn.Linear(hidden_size, hidden_size)
# self.hidden3 = nn.Linear(hidden_size, hidden_size)
# self.hidden4 = nn.Linear(hidden_size, hidden_size)
def forward(self, data):
h_0 = Variable(torch.zeros(
self.num_layers, data.size(0), self.hidden_size))
c_0 = Variable(torch.zeros(
self.num_layers, data.size(0), self.hidden_size))
# Propagate input through LSTM
out, (_, _) = self.lstm(data, (h_0, c_0))
out = out[:,-1,:]
# out = self.hidden4(self.hidden3(self.hidden2(self.hidden1(out))))
out_x = self.fc1(out)
out_y = self.fc2(out)
return out_x, out_y