-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnns.py
112 lines (95 loc) · 4.23 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
"""
A simple implementation of the RNN family - RNN, LSTM, BiLSTM, BiLSTMSearch.
"""
import torch
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:
"""
x = self.embeddings(x) # (N, L) -> (N, L, H)
for cell in self.cells:
x = cell(x)
return x
class RNNCell(torch.nn.Module):
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)
: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) -> (N, 1) -> (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.
Complexity - O(L * D)
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):
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.
"""
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 BiLSTM(RNNFamily):
"""
🚧 공사중 🚧
"""
pass
class BiLSTMSearch(RNNFamily):
"""
🚧 공사중 🚧
"""
pass