-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvae.py
150 lines (121 loc) · 6.17 KB
/
vae.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from models.common_networks import Encoder, Generator
from models.common_layers import LayerNormalizedGatedConv1d, LayerNormalizedGatedTransposeConv1d, LayerNormalizedLReLUConv1d, LayerNormalizedLReLUTransposeConv1d
class VAE(nn.Module):
def __init__(self, input_shape, encoder_arch, generator_arch, latent_dim,
num_speakers, speaker_dim, use_gated_convolutions=False):
super(VAE, self).__init__()
self.input_shape = input_shape
self.encoder_arch = encoder_arch
self.latent_dim = latent_dim
self.generator_arch = generator_arch
self.num_speakers = num_speakers
self.speaker_dim = speaker_dim
self.use_gated_convolutions = use_gated_convolutions
self.speaker_dict = nn.Embedding(num_embeddings=self.num_speakers,
embedding_dim=self.speaker_dim)
self.build_module()
def build_module(self):
print('Building VAE.')
x = torch.zeros((self.input_shape))
if self.use_gated_convolutions:
self.encoder = Encoder(input_shape=self.input_shape,
kernel_sizes=self.encoder_arch.kernel_sizes,
strides=self.encoder_arch.strides,
num_output_channels=self.encoder_arch.num_output_channels,
paddings=self.encoder_arch.paddings,
dilations=self.encoder_arch.dilations,
convolution_layer=LayerNormalizedGatedConv1d)
else:
self.encoder = Encoder(input_shape=self.input_shape,
kernel_sizes=self.encoder_arch.kernel_sizes,
strides=self.encoder_arch.strides,
num_output_channels=self.encoder_arch.num_output_channels,
paddings=self.encoder_arch.paddings,
dilations=self.encoder_arch.dilations,
convolution_layer=LayerNormalizedLReLUConv1d)
z_e = self.encoder(x)
self.z_e_shape = z_e.shape
# Flatten z_e
z_e = z_e.view(z_e.shape[0], -1)
# Mean of the posterior
self.mean_dense = nn.Linear(in_features=z_e.shape[1], out_features=self.latent_dim)
mean = self.mean_dense(z_e)
print('mean shape: {}'.format(mean.shape))
# Log-variance of the posterior
self.logvar_dense = nn.Linear(in_features=z_e.shape[1], out_features=self.latent_dim)
logvar = self.logvar_dense(z_e)
print('logvar shape: {}'.format(logvar.shape))
# Sample latent from the posterior
z = self.sample_latent(mean, logvar)
print('latent shape: {}'.format(z.shape))
self.latent_dense = nn.Linear(in_features=z.shape[1], out_features=self.z_e_shape[1]*self.z_e_shape[2])
z = self.latent_dense(z)
print('latent_out shape: {}'.format(z.shape))
# Speaker conditioning
y = torch.zeros((self.input_shape[0]), dtype=torch.long)
y = self.speaker_dict(y)
self.speaker_dense = nn.Linear(in_features=y.shape[1], out_features=self.z_e_shape[1]*self.z_e_shape[2])
y = self.speaker_dense(y)
print('speaker_out shape: {}'.format(y.shape))
# Add speaker embedding to the latent
z = z+y
# reshape back to 3D tensor
z = z.view(self.z_e_shape)
print('latent_out reshaped: {}'.format(z.shape))
if self.use_gated_convolutions:
self.generator = Generator(input_shape=z.shape,
kernel_sizes=self.generator_arch.kernel_sizes,
strides=self.generator_arch.strides,
dilations=self.generator_arch.dilations,
paddings=self.generator_arch.paddings,
out_paddings=self.generator_arch.out_paddings,
num_output_channels=self.generator_arch.num_output_channels,
convolution_layer=LayerNormalizedGatedTransposeConv1d)
else:
self.generator = Generator(input_shape=z.shape,
kernel_sizes=self.generator_arch.kernel_sizes,
strides=self.generator_arch.strides,
dilations=self.generator_arch.dilations,
paddings=self.generator_arch.paddings,
out_paddings=self.generator_arch.out_paddings,
num_output_channels=self.generator_arch.num_output_channels,
convolution_layer=LayerNormalizedLReLUTransposeConv1d)
x_hat = self.generator(z)
def forward(self, input, speaker):
z_e = self.encoder(input)
# Flatten z_e
z_e = z_e.view(z_e.shape[0], -1)
# Get variational parameters
mean = self.mean_dense(z_e)
logvar = self.logvar_dense(z_e)
# Sample from posterior if during training, otherwise just pass the variational mean
if self.training:
z = self.sample_latent(mean, logvar)
else:
z = mean
# Form latent output, add speaker embeddings
z = self.latent_dense(z)
y = self.speaker_dense(self.speaker_dict(speaker))
z = z + y
# reshape back to 3D tensor
z = z.view(-1, self.z_e_shape[1], self.z_e_shape[2])
x_hat = self.generator(z)
return x_hat, mean, logvar
def reset_parameters(self):
"""
Re-initializes the networks parameters
"""
self.encoder.reset_parameters()
self.generator.reset_parameters()
self.mean_dense.reset_parameters()
self.logvar_dense.reset_parameters()
self.latent_dense.reset_parameters()
self.speaker_dense.reset_parameters()
self.speaker_dict.reset_parameters()
def sample_latent(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std