-
Notifications
You must be signed in to change notification settings - Fork 1
/
vrnn.py
186 lines (139 loc) · 4.56 KB
/
vrnn.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
import math
import torch
import torch.nn as nn
import torch.utils
import torch.utils.data
from torchvision import datasets, transforms
from torch.autograd import Variable
import matplotlib.pyplot as plt
"""implementation of the Variational Recurrent
Neural Network (VRNN) from https://arxiv.org/abs/1506.02216
using unimodal isotropic gaussian distributions for
inference, prior, and generating models."""
REGULATOR = 1e-6
class VRNN(nn.Module):
def __init__(self, x_dim, h_dim, z_dim, n_layers, bias=False):
super(VRNN, self).__init__()
self.x_dim = x_dim
self.h_dim = h_dim
self.z_dim = z_dim
self.n_layers = n_layers
#feature-extracting transformations
self.phi_x = nn.Sequential(
nn.Linear(x_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU())
self.phi_z = nn.Sequential(
nn.Linear(z_dim, h_dim),
nn.ReLU())
#encoder
self.enc = nn.Sequential(
nn.Linear(h_dim + h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU())
self.enc_mean = nn.Linear(h_dim, z_dim)
self.enc_std = nn.Sequential(
nn.Linear(h_dim, z_dim),
nn.Softplus())
#prior
self.prior = nn.Sequential(
nn.Linear(h_dim, h_dim),
nn.ReLU())
self.prior_mean = nn.Linear(h_dim, z_dim)
self.prior_std = nn.Sequential(
nn.Linear(h_dim, z_dim),
nn.Softplus())
#decoder
self.dec = nn.Sequential(
nn.Linear(h_dim + h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU())
self.dec_std = nn.Sequential(
nn.Linear(h_dim, x_dim),
nn.Softplus())
#self.dec_mean = nn.Linear(h_dim, x_dim)
self.dec_mean = nn.Sequential(
nn.Linear(h_dim, x_dim),
nn.Sigmoid())
#recurrence
self.rnn = nn.GRU(h_dim + h_dim, h_dim, n_layers, bias)
def forward(self, x):
all_enc_mean, all_enc_std = [], []
all_dec_mean, all_dec_std = [], []
kld_loss = 0
nll_loss = 0
h = Variable(torch.zeros(self.n_layers, x.size(1), self.h_dim))
for t in range(x.size(0)):
phi_x_t = self.phi_x(x[t])
#encoder
enc_t = self.enc(torch.cat([phi_x_t, h[-1]], 1))
enc_mean_t = self.enc_mean(enc_t)
enc_std_t = self.enc_std(enc_t)
#prior
prior_t = self.prior(h[-1])
prior_mean_t = self.prior_mean(prior_t)
prior_std_t = self.prior_std(prior_t)
#sampling and reparameterization
z_t = self._reparameterized_sample(enc_mean_t, enc_std_t)
phi_z_t = self.phi_z(z_t)
#decoder
dec_t = self.dec(torch.cat([phi_z_t, h[-1]], 1))
dec_mean_t = self.dec_mean(dec_t)
dec_std_t = self.dec_std(dec_t)
#recurrence
_, h = self.rnn(torch.cat([phi_x_t, phi_z_t], 1).unsqueeze(0), h)
#computing losses
kld_loss += self._kld_gauss(enc_mean_t, enc_std_t, prior_mean_t, prior_std_t)
#nll_loss += self._nll_gauss(dec_mean_t, dec_std_t, x[t])
nll_loss += self._nll_bernoulli(dec_mean_t, x[t])
all_enc_std.append(enc_std_t)
all_enc_mean.append(enc_mean_t)
all_dec_mean.append(dec_mean_t)
all_dec_std.append(dec_std_t)
return kld_loss, nll_loss, \
(all_enc_mean, all_enc_std), \
(all_dec_mean, all_dec_std)
def sample(self, seq_len):
sample = torch.zeros(seq_len, self.x_dim)
h = Variable(torch.zeros(self.n_layers, 1, self.h_dim))
for t in range(seq_len):
#prior
prior_t = self.prior(h[-1])
prior_mean_t = self.prior_mean(prior_t)
prior_std_t = self.prior_std(prior_t)
#sampling and reparameterization
z_t = self._reparameterized_sample(prior_mean_t, prior_std_t)
phi_z_t = self.phi_z(z_t)
#decoder
dec_t = self.dec(torch.cat([phi_z_t, h[-1]], 1))
dec_mean_t = self.dec_mean(dec_t)
#dec_std_t = self.dec_std(dec_t)
phi_x_t = self.phi_x(dec_mean_t)
#recurrence
_, h = self.rnn(torch.cat([phi_x_t, phi_z_t], 1).unsqueeze(0), h)
sample[t] = dec_mean_t.data
return sample
def reset_parameters(self, stdv=1e-1):
for weight in self.parameters():
weight.data.normal_(0, stdv)
def _init_weights(self, stdv):
pass
def _reparameterized_sample(self, mean, std):
"""using std to sample"""
eps = torch.FloatTensor(std.size()).normal_()
eps = Variable(eps)
return eps.mul(std).add_(mean)
def _kld_gauss(self, mean_1, std_1, mean_2, std_2):
"""Using std to compute KLD"""
kld_element = (2 * torch.log(std_2) - 2 * torch.log(std_1) +
(std_1.pow(2) + (mean_1 - mean_2).pow(2)) /
std_2.pow(2) - 1)
return 0.5 * torch.sum(kld_element)
def _nll_bernoulli(self, theta, x):
return - torch.sum(x*torch.log(theta) + (1-x)*torch.log(1-theta))
def _nll_gauss(self, mean, std, x):
tmp = Variable( torch.ones(x.size(0), x.size(1))* np.log(2* np.pi) ) + 2 * torch.log(std)
return 0.5 * torch.sum(tmp + 1.0/std.pow(2) * (x-mean).pow(2))