-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstochastic_rnn.py
182 lines (141 loc) · 6.57 KB
/
stochastic_rnn.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
from typing import Optional, Tuple
import torch
from torch import nn, Tensor
class StochasticLSTMCell(nn.Module):
def __init__(self, input_size: int, hidden_size: int, dropout: Optional[float]=None):
"""
Args:
- dropout: should be between 0 and 1
"""
super(StochasticLSTMCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
if dropout is None:
self.p_logit = nn.Parameter(torch.empty(1).normal_())
elif not 0 < dropout < 1:
raise Exception("Dropout rate should be between in (0, 1)")
else:
self.p_logit = dropout
self.Wi = nn.Linear(self.input_size, self.hidden_size)
self.Wf = nn.Linear(self.input_size, self.hidden_size)
self.Wo = nn.Linear(self.input_size, self.hidden_size)
self.Wg = nn.Linear(self.input_size, self.hidden_size)
self.Ui = nn.Linear(self.hidden_size, self.hidden_size)
self.Uf = nn.Linear(self.hidden_size, self.hidden_size)
self.Uo = nn.Linear(self.hidden_size, self.hidden_size)
self.Ug = nn.Linear(self.hidden_size, self.hidden_size)
self.init_weights()
def init_weights(self):
k = torch.tensor(self.hidden_size, dtype=torch.float32).reciprocal().sqrt()
self.Wi.weight.data.uniform_(-k,k)
self.Wi.bias.data.uniform_(-k,k)
self.Wf.weight.data.uniform_(-k,k)
self.Wf.bias.data.uniform_(-k,k)
self.Wo.weight.data.uniform_(-k,k)
self.Wo.bias.data.uniform_(-k,k)
self.Wg.weight.data.uniform_(-k,k)
self.Wg.bias.data.uniform_(-k,k)
self.Ui.weight.data.uniform_(-k,k)
self.Ui.bias.data.uniform_(-k,k)
self.Uf.weight.data.uniform_(-k,k)
self.Uf.bias.data.uniform_(-k,k)
self.Uo.weight.data.uniform_(-k,k)
self.Uo.bias.data.uniform_(-k,k)
self.Ug.weight.data.uniform_(-k,k)
self.Ug.bias.data.uniform_(-k,k)
# Note: value p_logit at infinity can cause numerical instability
def _sample_mask(self, B):
"""Dropout masks for 4 gates, scale input by 1 / (1 - p)"""
if isinstance(self.p_logit, float):
p = self.p_logit
else:
p = torch.sigmoid(self.p_logit)
GATES = 4
eps = torch.tensor(1e-7)
t = 1e-1
ux = torch.rand(GATES, B, self.input_size)
uh = torch.rand(GATES, B, self.hidden_size)
if self.input_size == 1:
zx = (1-torch.sigmoid((torch.log(eps) - torch.log(1+eps)
+ torch.log(ux+eps) - torch.log(1-ux+eps))
/ t))
else:
zx = (1-torch.sigmoid((torch.log(p+eps) - torch.log(1-p+eps)
+ torch.log(ux+eps) - torch.log(1-ux+eps))
/ t)) / (1-p)
zh = (1-torch.sigmoid((torch.log(p+eps) - torch.log(1-p+eps)
+ torch.log(uh+eps) - torch.log(1-uh+eps))
/ t)) / (1-p)
return zx, zh
def regularizer(self):
if isinstance(self.p_logit, float):
p = torch.tensor(self.p_logit)
else:
p = torch.sigmoid(self.p_logit)
# Weight
weight_sum = torch.tensor([
torch.sum(params**2) for name, params in self.named_parameters() if name.endswith("weight")
]).sum() / (1.-p)
# Bias
bias_sum = torch.tensor([
torch.sum(params**2) for name, params in self.named_parameters() if name.endswith("bias")
]).sum()
if isinstance(self.p_logit, float):
dropout_reg = torch.zeros(1)
else:
# Dropout
dropout_reg = self.input_size * (p * torch.log(p) + (1-p)*torch.log(1-p))
return weight_sum, bias_sum, 2.*dropout_reg
def forward(self, input: Tensor, hx: Optional[Tuple[Tensor, Tensor]]=None) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
"""
input shape (sequence, batch, input dimension)
output shape (sequence, batch, output dimension)
return output, (hidden_state, cell_state)
"""
T, B = input.shape[0:2]
if hx is None:
h_t = torch.zeros(B, self.hidden_size, dtype=input.dtype)
c_t = torch.zeros(B, self.hidden_size, dtype=input.dtype)
else:
h_t, c_t = hx
hn = torch.empty(T, B, self.hidden_size, dtype=input.dtype)
# Masks
zx, zh = self._sample_mask(B)
for t in range(T):
x_i, x_f, x_o, x_g = (input[t] * zx_ for zx_ in zx)
h_i, h_f, h_o, h_g = (h_t * zh_ for zh_ in zh)
i = torch.sigmoid(self.Ui(h_i) + self.Wi(x_i))
f = torch.sigmoid(self.Uf(h_f) + self.Wf(x_f))
o = torch.sigmoid(self.Uo(h_o) + self.Wo(x_o))
g = torch.tanh(self.Ug(h_g) + self.Wg(x_g))
c_t = f * c_t + i * g
h_t = o * torch.tanh(c_t)
hn[t] = h_t
return hn, (h_t, c_t)
class StochasticLSTM(nn.Module):
"""LSTM stacked layers with dropout and MCMC"""
def __init__(self, input_size: int, hidden_size: int, dropout:Optional[float]=None, num_layers: int=1):
super(StochasticLSTM, self).__init__()
self.num_layers = num_layers
self.first_layer = StochasticLSTMCell(input_size, hidden_size, dropout)
self.hidden_layers = nn.ModuleList([StochasticLSTMCell(hidden_size, hidden_size, dropout) for i in range(num_layers-1)])
def regularizer(self):
total_weight_reg, total_bias_reg, total_dropout_reg = self.first_layer.regularizer()
for l in self.hidden_layers:
weight, bias, dropout = l.regularizer()
total_weight_reg += weight
total_bias_reg += bias
total_dropout_reg += dropout
return total_weight_reg, total_bias_reg, total_dropout_reg
def forward(self, input: Tensor, hx: Optional[Tuple[Tensor, Tensor]]=None) -> Tuple[Tensor, Tuple[Tensor, Tensor]]:
B = input.shape[1]
h_n = torch.empty(self.num_layers, B, self.first_layer.hidden_size)
c_n = torch.empty(self.num_layers, B, self.first_layer.hidden_size)
outputs, (h, c) = self.first_layer(input, hx)
h_n[0] = h
c_n[0] = c
for i, layer in enumerate(self.hidden_layers):
outputs, (h, c) = layer(outputs, (h, c))
h_n[i+1] = h
c_n[i+1] = c
return outputs, (h_n, c_n)