-
Notifications
You must be signed in to change notification settings - Fork 0
/
architectures.py
54 lines (44 loc) · 1.93 KB
/
architectures.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
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, num_hidden = 128, num_layers = 1, context = 5, features = 9, dropout = 0.1) -> None:
super().__init__()
self.rnn = nn.GRU(features, num_hidden, num_layers, batch_first = True)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.dropout(x)
output, state = self.rnn(x)
return output, state
class PlainDecoder(nn.Module):
def __init__(self, num_hidden = 128, num_layers = 1, context = 5, features = 9, dropout = 0.1) -> None:
super().__init__()
self.rnn = nn.GRU(features, num_hidden, num_layers, batch_first = True)
self.dense = nn.Linear(num_hidden, 1)
self.dropout = nn.Dropout(dropout)
def forward(self, x, state):
# x = self.dropout(x)
output, state = self.rnn(x, state)
output = self.dense(output)
return output
class Seq2Seq(nn.Module):
def __init__(self, num_hidden = 128, num_layers = 1, context = 5, features = 9, dropout = 0.1) -> None:
super().__init__()
self.encoder = Encoder()
self.decoder = PlainDecoder()
def forward(self, x):
# print(x.shape)
encoder_output, encoder_hidden = self.encoder(x)# hidden --> layer, batch, hidden dim
most_recent_day = x[:,-1,:].unsqueeze(1) #batch, days, features
output = self.decoder(most_recent_day, encoder_hidden).squeeze(2)
return output
class PlainModel(nn.Module):
def __init__(self, num_hidden = 128, num_layers = 1, context = 5, features = 9, dropout = 0.1) -> None:
super().__init__()
self.rnn = nn.GRU(features, num_hidden, num_layers, batch_first = True)
self.dense = nn.Linear(num_hidden, 1)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.dropout(x)
output, state = self.rnn(x)
output = self.dense(output)
return output[:,-1]