-
Notifications
You must be signed in to change notification settings - Fork 8
/
mog_lstm.py
30 lines (26 loc) · 1.07 KB
/
mog_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
28
29
30
import torch
import torch.nn as nn
import math
class MogrifierLSTMCell(nn.Module):
def __init__(self, input_size, hidden_size, mogrify_steps):
super(MogrifierLSTMCell, self).__init__()
self.mogrify_steps = mogrify_steps
self.lstm = nn.LSTMCell(input_size, hidden_size)
self.mogrifier_list = nn.ModuleList([nn.Linear(hidden_size, input_size)]) # start with q
for i in range(1, mogrify_steps):
if i % 2 == 0:
self.mogrifier_list.extend([nn.Linear(hidden_size, input_size)]) # q
else:
self.mogrifier_list.extend([nn.Linear(input_size, hidden_size)]) # r
def mogrify(self, x, h):
for i in range(self.mogrify_steps):
if (i+1) % 2 == 0:
h = (2*torch.sigmoid(self.mogrifier_list[i](x))) * h
else:
x = (2*torch.sigmoid(self.mogrifier_list[i](h))) * x
return x, h
def forward(self, x, states):
ht, ct = states
x, ht = self.mogrify(x, ht)
ht, ct = self.lstm(x, (ht, ct))
return ht, ct