forked from Dapwner/CVAE-Tacotron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmvae.py
195 lines (156 loc) · 6.22 KB
/
gmvae.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
187
188
189
190
191
192
193
194
195
# adapted from https://github.com/jariasf/GMVAE/blob/master/pytorch/networks/Networks.py
# MIT License
# Copyright (c) 2019 Jhosimar George Arias Figueroa
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import torch
import torch.nn.init as init
from torch import nn
from torch.nn import functional as F
# Flatten layer
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
# Reshape layer
class Reshape(nn.Module):
def __init__(self, outer_shape):
super(Reshape, self).__init__()
self.outer_shape = outer_shape
def forward(self, x):
return x.view(x.size(0), *self.outer_shape)
# Sample from the Gumbel-Softmax distribution and optionally discretize.
class GumbelSoftmax(nn.Module):
def __init__(self, f_dim, c_dim):
super(GumbelSoftmax, self).__init__()
self.logits = nn.Linear(f_dim, c_dim)
self.f_dim = f_dim
self.c_dim = c_dim
def sample_gumbel(self, shape, is_cuda=False, eps=1e-20):
# https://zhuanlan.zhihu.com/p/50065712
U = torch.rand(shape) # sample from uniform [0,1]
if is_cuda:
U = U.cuda()
return -torch.log(-torch.log(U + eps) + eps)
def gumbel_softmax_sample(self, logits, temperature):
y = logits + self.sample_gumbel(logits.size(), logits.is_cuda)
return F.softmax(y / temperature, dim=-1)
def gumbel_softmax(self, logits, temperature, hard=False):
"""
ST-gumple-softmax
input: [*, n_class]
return: flatten --> [*, n_class] an one-hot vector
"""
# categorical_dim = 10
y = self.gumbel_softmax_sample(logits, temperature)
if not hard:
return y
shape = y.size()
_, ind = y.max(dim=-1)
y_hard = torch.zeros_like(y).view(-1, shape[-1])
y_hard.scatter_(1, ind.view(-1, 1), 1)
y_hard = y_hard.view(*shape)
# Set gradients w.r.t. y_hard gradients w.r.t. y
y_hard = (y_hard - y).detach() + y
return y_hard
def forward(self, x, temperature=1.0, hard=False):
logits = self.logits(x).view(-1, self.c_dim)
prob = F.softmax(logits, dim=-1)
y = self.gumbel_softmax(logits, temperature, hard)
return logits, prob, y
# Sample from a Gaussian distribution
class Gaussian(nn.Module):
def __init__(self, in_dim, z_dim):
super(Gaussian, self).__init__()
self.mu = nn.Linear(in_dim, z_dim)
self.var = nn.Linear(in_dim, z_dim)
def reparameterize(self, mu, var):
std = torch.sqrt(var + 1e-10)
noise = torch.randn_like(std)
z = mu + noise * std
return z
def forward(self, x):
mu = self.mu(x)
var = F.softplus(self.var(x))
z = self.reparameterize(mu, var)
return mu, var, z
# Inference Network
class InferenceNet(nn.Module):
def __init__(self, x_dim, z_dim, y_dim):
super(InferenceNet, self).__init__()
# p(z|y)
self.y_mu = nn.Linear(y_dim, z_dim)
self.y_var = nn.Linear(y_dim, z_dim)
# q(y|x)
self.inference_qyx = torch.nn.ModuleList([
nn.Linear(x_dim, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
GumbelSoftmax(512, y_dim)
])
# q(z|y,x)
self.inference_qzyx = torch.nn.ModuleList([
nn.Linear(x_dim + y_dim, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
Gaussian(512, z_dim)
])
# q(y|x)
def qyx(self, x, temperature, hard):
num_layers = len(self.inference_qyx)
for i, layer in enumerate(self.inference_qyx):
if i == num_layers - 1:
# last layer is gumbel softmax
x = layer(x, temperature, hard)
else:
x = layer(x)
return x
def pzy(self, y):
y_mu = self.y_mu(y)
y_var = F.softplus(self.y_var(y))
return y_mu, y_var
# q(z|x,y)
def qzxy(self, x, y):
concat = torch.cat((x, y), dim=1)
for layer in self.inference_qzyx:
# print(concat.shape)
concat = layer(concat)
return concat
def forward(self, x, temperature=1.0, hard=0):
# x = Flatten(x)
# q(y|x)
logits, prob, y = self.qyx(x, temperature, hard)
# q(z|x,y)
mu, var, z = self.qzxy(x, y)
# p(z|y)
y_mu, y_var = self.pzy(y)
return (z, (z, mu, var, y_mu, y_var, prob, logits))
# GMVAE Network
class GMVAENet(nn.Module):
def __init__(self, model_config):
super(GMVAENet, self).__init__()
self.inference = InferenceNet(model_config["reference_encoder"]["ref_enc_gru_size"], model_config["encoder"]["encoder_embedding_dim"],model_config["accent_encoder"]["n_accent_classes"])
# weight initialization
for m in self.modules():
if type(m) == nn.Linear or type(m) == nn.Conv2d or type(m) == nn.ConvTranspose2d:
torch.nn.init.xavier_normal_(m.weight)
if m.bias.data is not None:
init.constant_(m.bias, 0)
def forward(self, x, temperature=1.0, hard=0):
x = x.view(x.size(0), -1)
out_inf = self.inference(x, temperature, hard)
return out_inf