-
Notifications
You must be signed in to change notification settings - Fork 14
/
recurrent_models.py
260 lines (181 loc) · 7.86 KB
/
recurrent_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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
##########################################################
# pytorch-qnn v1.0
# Titouan Parcollet
# LIA, Université d'Avignon et des Pays du Vaucluse
# ORKIS, Aix-en-provence
# October 2018
##########################################################
import torch
import torch.nn as nn
from torch.nn import Parameter
from torch.nn import functional as F
import torch.optim
from torch import autograd
from torch.autograd import Variable
from quaternion_layers import *
#
# QRNN, RNN, QLSTM, and LSTM models are intended to work on copy_task.py
# Please use quaternion_layers.py for building custom architectures
#
class QRNN(nn.Module):
def __init__(self, feat_size, hidden_size, CUDA):
super(QRNN, self).__init__()
# Reading options:
self.input_dim=feat_size
self.hidden_dim=hidden_size
self.num_classes=feat_size
self.CUDA=CUDA
# List initialization (Not used, but could be if multiple layers)
self.wx = QuaternionLinearAutograd(self.input_dim, self.hidden_dim)
self.uh = QuaternionLinearAutograd(self.hidden_dim, self.hidden_dim)
# Output layer initialization
self.fco = nn.Linear(curr_dim, self.num_classes)
# Optimizer
self.adam = torch.optim.Adam(self.parameters(), lr=0.005)
def forward(self, x):
# Init hidden states
h_init = Variable(torch.zeros(x.shape[1],self. hidden_dim))
if self.CUDA:
x = x.cuda()
h_init = h_init.cuda()
# Compute W * X in parallel
wx_out=self.wx(x)
h=h_init
out = []
# Navigate trough timesteps
for k in range(x.shape[0]):
at=wx_out[k]+self.uh(h)
h=at
output = nn.Tanh()(self.fco(h))
out.append(output.unsqueeze(0))
return torch.cat(out,0)
class QLSTM(nn.Module):
def __init__(self, feat_size, hidden_size, CUDA):
super(QLSTM, self).__init__()
# Reading options:
self.act=nn.Tanh()
self.act_gate=nn.Sigmoid()
self.input_dim=feat_size
self.hidden_dim=hidden_size
self.CUDA=CUDA
# +1 because feat_size = the number on the sequence, and the output one hot will also have
# a blank dimension so FEAT_SIZE + 1 BLANK
self.num_classes=feat_size + 1
# Gates initialization
self.wfx = QuaternionLinearAutograd(self.input_dim, self.hidden_dim) # Forget
self.ufh = QuaternionLinearAutograd(self.hidden_dim, self.hidden_dim, bias=False) # Forget
self.wix = QuaternionLinearAutograd(self.input_dim, self.hidden_dim) # Input
self.uih = QuaternionLinearAutograd(self.hidden_dim, self.hidden_dim, bias=False) # Input
self.wox = QuaternionLinearAutograd(self.input_dim, self.hidden_dim) # Output
self.uoh = QuaternionLinearAutograd(self.hidden_dim, self.hidden_dim, bias=False) # Output
self.wcx = QuaternionLinearAutograd(self.input_dim, self.hidden_dim) # Cell
self.uch = QuaternionLinearAutograd(self.hidden_dim, self.hidden_dim, bias=False) # Cell
# Output layer initialization
self.fco = nn.Linear(self.hidden_dim, self.num_classes)
# Optimizer
self.adam = torch.optim.Adam(self.parameters(), lr=0.005)
def forward(self, x):
h_init = Variable(torch.zeros(x.shape[1],self. hidden_dim))
if self.CUDA:
x=x.cuda()
h_init=h_init.cuda()
# Feed-forward affine transformation (done in parallel)
wfx_out=self.wfx(x)
wix_out=self.wix(x)
wox_out=self.wox(x)
wcx_out=self.wcx(x)
# Processing time steps
out = []
c=h_init
h=h_init
for k in range(x.shape[0]):
ft=self.act_gate(wfx_out[k]+self.ufh(h))
it=self.act_gate(wix_out[k]+self.uih(h))
ot=self.act_gate(wox_out[k]+self.uoh(h))
at=wcx_out[k]+self.uch(h)
c=it*self.act(at)+ft*c
h=ot*self.act(c)
output = self.fco(h)
out.append(output.unsqueeze(0))
return torch.cat(out,0)
class RNN(nn.Module):
def __init__(self, feat_size, hidden_size, CUDA):
super(RNN, self).__init__()
# Reading options:
self.input_dim=feat_size
self.hidden_dim=hidden_size
self.num_classes=feat_size
self.CUDA=CUDA
# List initialization (Not used, but could be if multiple layers)
self.wx = nn.Linear(self.input_dim, self.hidden_dim)
self.uh = nn.Linear(self.hidden_dim, self.hidden_dim)
# Output layer initialization
self.fco = nn.Linear(self.hidden_dim, self.num_classes)
# Optimizer
self.adam = torch.optim.Adam(self.parameters(), lr=0.0002)
def forward(self, x):
# Init hidden states
h_init = Variable(torch.zeros(x.shape[1],self. hidden_dim))
if self.CUDA:
x = x.cuda()
h_init = h_init.cuda()
# Compute W * X in parallel
wx_out=self.wx(x)
h=h_init
out = []
# Navigate trough timesteps
for k in range(x.shape[0]):
at=wx_out[k]+self.uh(h)
h=at
output = nn.Tanh()(self.fco(h))
out.append(output.unsqueeze(0))
return torch.cat(out,0)
class LSTM(nn.Module):
def __init__(self, feat_size, hidden_size, CUDA):
super(LSTM, self).__init__()
# Reading options:
self.act=nn.Tanh()
self.act_gate=nn.Sigmoid()
self.input_dim=feat_size
self.hidden_dim=hidden_size
self.CUDA=CUDA
# +1 because feat_size = the number on the sequence, and the output one hot will also have
# a blank dimension so FEAT_SIZE + 1 BLANK
self.num_classes=feat_size + 1
# Gates initialization
self.wfx = nn.Linear(self.input_dim, self.hidden_dim) # Forget
self.ufh = nn.Linear(self.hidden_dim, self.hidden_dim, bias=False) # Forget
self.wix = nn.Linear(self.input_dim, self.hidden_dim) # Input
self.uih = nn.Linear(self.hidden_dim, self.hidden_dim, bias=False) # Input
self.wox = nn.Linear(self.input_dim, self.hidden_dim) # Output
self.uoh = nn.Linear(self.hidden_dim, self.hidden_dim, bias=False) # Output
self.wcx = nn.Linear(self.input_dim, self.hidden_dim) # Cell
self.uch = nn.Linear(self.hidden_dim, self.hidden_dim, bias=False) # Cell
# Output layer initialization
self.fco = nn.Linear(self.hidden_dim, self.num_classes)
# Optimizer
self.adam = torch.optim.Adam(self.parameters(), lr=0.005)
def forward(self, x):
h_init = Variable(torch.zeros(x.shape[1],self. hidden_dim))
if self.CUDA:
x=x.cuda()
h_init=h_init.cuda()
# Feed-forward affine transformation (done in parallel)
wfx_out=self.wfx(x)
wix_out=self.wix(x)
wox_out=self.wox(x)
wcx_out=self.wcx(x)
# Processing time steps
out = []
c=h_init
h=h_init
for k in range(x.shape[0]):
ft=self.act_gate(wfx_out[k]+self.ufh(h))
it=self.act_gate(wix_out[k]+self.uih(h))
ot=self.act_gate(wox_out[k]+self.uoh(h))
at=wcx_out[k]+self.uch(h)
c=it*self.act(at)+ft*c
h=ot*self.act(c)
output = self.fco(h)
out.append(output.unsqueeze(0))
return torch.cat(out,0)