-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrnns.py
132 lines (114 loc) · 5.05 KB
/
rnns.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
"""
A simple implementation of the RNN family - RNN, LSTM, BiLSTM, BiLSTMSearch.
"""
import torch # noqa
from typing import Tuple
class RNNFamily(torch.nn.Module):
def __init__(self, vocab_size: int, hidden_size: int, cells: torch.nn.ModuleList):
super().__init__()
self.embeddings = torch.nn.Embedding(num_embeddings=vocab_size, embedding_dim=hidden_size)
self.cells = cells
def forward(self, x: torch.Tensor):
"""
:param x: (N, L)
:return: memories (N, L, H)
"""
x = self.embeddings(x) # (N, L) -> (N, L, H)
for cell in self.cells:
x = cell(x)
return x
class RNNCell(torch.nn.Module):
"""
weights = 2 * H^2
"""
def __init__(self, hidden_size: int):
super().__init__()
self.W_hh = torch.nn.Linear(in_features=hidden_size, out_features=hidden_size)
self.W_xh = torch.nn.Linear(in_features=hidden_size, out_features=hidden_size)
self.register_buffer("dummy", torch.zeros(hidden_size))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
h_t = f_W(h_t-1(short), x_t(now))
h_t = tanh(W_hh * h_t-1 + W_xh * x_t)
h*h*2 = 2*h^2
:param x - (N, L, H)
:return: memories - (N, L, H)
"""
N, L, _ = x.shape
memories = list()
short = self.dummy.unsqueeze(0).expand(N, -1) # (H) -> (1, H) -> (N, H)
for time in range(L):
now = x[:, time] # (N, L, H) -> (N, H)
short = torch.tanh(self.W_hh(short) + self.W_xh(now)) # ... -> (N, H)
memories.append(short)
return torch.stack(memories, dim=1) # ... -> (N, L, H)
class RNN(RNNFamily):
"""
A vanilla multi-layer RNN.
H * H * 2 + V * H = 2*H^2 + V*H = H(2H + V)
https://medium.com/ecovisioneth/building-deep-multi-layer-recurrent-neural-networks-with-star-cell-2f01acdb73a7
"""
def __init__(self, vocab_size: int, hidden_size: int, depth: int):
super().__init__(vocab_size, hidden_size,
cells=torch.nn.ModuleList([RNNCell(hidden_size) for _ in range(depth)]))
class LSTMCell(torch.nn.Module):
"""
weights = 2 * H * H * 4 = 8 * H^2. (4 * RNNCell)
"""
def __init__(self, hidden_size: int):
super().__init__()
self.W_f = torch.nn.Linear(in_features=hidden_size*2, out_features=hidden_size)
self.W_i = torch.nn.Linear(in_features=hidden_size*2, out_features=hidden_size)
self.W_o = torch.nn.Linear(in_features=hidden_size*2, out_features=hidden_size)
self.W_h = torch.nn.Linear(in_features=hidden_size*2, out_features=hidden_size)
self.register_buffer("dummy", torch.zeros(hidden_size))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
:param x - (N, L, H)
:return: memories (N, L, H)
"""
N, L, _ = x.shape
memories = list()
long = self.dummy.unsqueeze(0).expand(N, -1) # (H) -> (1, H) -> (N, H)
short = self.dummy.unsqueeze(0).expand(N, -1) # (H) -> (1, H) -> (N, H)
for time in range(L):
now = x[:, time] # (N, L, H) -> (N, H)
short_cat_now = torch.concat([short, now], dim=-1) # (N, H), (N, H) -> (N, H * 2)
f = torch.sigmoid(self.W_f(short_cat_now)) # (N, H * 2) * (H * 2, H) -> (N, H)
i = torch.sigmoid(self.W_i(short_cat_now)) # (N, H * 2) * (H * 2, H) -> (N, H)
o = torch.sigmoid(self.W_o(short_cat_now)) # (N, H * 2) * (H * 2, H) -> (N, H)
h = self.W_h(short_cat_now) # (N, H * 2) * (H * 2, H) -> (N, H)
# forget parts of long-term memory, while adding parts of short-term memory to long-term memory
long = torch.mul(f, long) + torch.mul(i, h) # (N, H) + (N, H) -> (N, H)
# generate short-term memory from parts of long-term memory
short = torch.mul(o, torch.tanh(long)) # (N, H) + (N, H) -> (N, H)
memories.append(short)
return torch.stack(memories, dim=1) # ... -> (N, L, H)
class LSTM(RNNFamily):
"""
A simple, multi-layer LSTM.
weights = H(8H + V)
"""
def __init__(self, vocab_size: int, hidden_size: int, depth: int):
super().__init__(vocab_size, hidden_size,
cells=torch.nn.ModuleList([LSTMCell(hidden_size) for _ in range(depth)]))
class BiLSTMCell(torch.nn.Module):
"""
weights = 2 * 8 * H^2 = 16 * H^2
"""
def __init__(self, hidden_size: int):
super().__init__()
self.lr = LSTMCell(hidden_size)
self.rl = LSTMCell(hidden_size)
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor]:
memories = self.lr(x) # (N, L) -> (N, L, H)
memories = self.rl(torch.fliplr(memories)) # (N, L) -> (N, L, H)
return memories
class BiLSTM(RNNFamily):
"""
A simple, multi-layer LSTM.
weights = H(16H + V)
"""
def __init__(self, vocab_size: int, hidden_size: int, depth: int):
super().__init__(vocab_size, hidden_size,
cells=torch.nn.ModuleList([BiLSTMCell(hidden_size) for _ in range(depth)]))