-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
86 lines (64 loc) · 2.9 KB
/
models.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
import torch
import torch.nn as nn
from sru import SRU
class LSTMModel(nn.Module):
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
super(LSTMModel, self).__init__()
self.hidden_dim = hidden_dim
self.layer_dim = layer_dim
self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
h0 = h0.cuda()
c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
c0 = c0.cuda()
out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:,-1,:])
return out
class GRUModel(nn.Module):
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
super(GRUModel, self).__init__()
self.hidden_dim = hidden_dim
self.layer_dim = layer_dim
self.gru = nn.GRU(input_dim, hidden_dim, layer_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
h0 = h0.cuda()
c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
c0 = c0.cuda()
out, hn = self.gru(x, h0)
out = self.fc(out[:,-1,:])
return out
class SRUModel(nn.Module):
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
super(SRUModel, self).__init__()
self.hidden_dim = hidden_dim
self.layer_dim = layer_dim
self.sru = SRU(input_dim, hidden_dim, layer_dim, has_skip_term = False, amp_recurrence_fp16=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
h0 = h0.cuda()
c0 = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
c0 = c0.cuda()
out, c = self.sru(x)
out = self.fc(out[-1,:,:])
return out
class RNNModel(nn.Module):
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
super(RNNModel, self).__init__()
self.hidden_dim = hidden_dim
self.layer_dim = layer_dim
self.rnn = nn.RNN(input_dim, hidden_dim, layer_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
hidden = torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).requires_grad_()
hidden = hidden.cuda()
out, hidden = self.rnn(x, hidden)
out = self.fc(out[:,-1,:])
return out
def init_hidden(self, batch_size):
hidden = torch.zeros(self.layer_dim, batch_size, self.hidden_dim).requires_grad_()
return hidden