-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcVAE.py
232 lines (212 loc) · 6.91 KB
/
cVAE.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from typing import Tuple
import torch
import torch.nn as nn
# Encoder class
class cEncoder(nn.Module):
def __init__(self, input_dim:int, latent_dim:int, num_classes:int)->torch.Tensor:
super().__init__()
"""
Parameters
----------
input_dim : int
Dimension of input data, e.g. number of features (28*28=784 for MNIST)
latent_dim : int
Dimension of latent space (z)
num_classes : int
Number of classes in dataset, e.g. 10 for MNIST
"""
self.embed = nn.Embedding(num_classes, num_classes)
self.linear = nn.Sequential(
nn.Linear(input_dim+num_classes, 1024),
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU()
)
self.linear2 = nn.Linear(128, latent_dim)
self.linear3 = nn.Linear(128, latent_dim)
def forward(self, x:torch.Tensor, y:torch.Tensor)->torch.Tensor:
"""
Parameters
----------
x : torch.Tensor
Input data
y : torch.Tensor
Labels
"""
y = self.embed(y)
x = torch.cat((x, y), dim=1)
hidden = self.linear(x)
mu = self.linear2(hidden)
log_var = self.linear3(hidden)
return mu, log_var
# Decoder class
class cDecoder(nn.Module):
def __init__(self, latent_dim:int, output_dim:int, num_classes:int)->torch.Tensor:
super().__init__()
"""
Parameters
----------
latent_dim : int
Dimension of latent space (z)
output_dim : int
Dimension of output data, e.g. number of features (28*28=784 for MNIST)
num_classes : int
Number of classes in dataset, e.g. 10 for MNIST
"""
self.embed = nn.Embedding(num_classes, num_classes)
self.linear = nn.Sequential(
nn.Linear(latent_dim+num_classes, 256),
nn.ReLU(),
nn.Linear(256, 512),
nn.ReLU(),
nn.Linear(512, 1024),
nn.ReLU(),
nn.Linear(1024, output_dim),
nn.Tanh()
)
def forward(self, z:torch.Tensor, y:torch.Tensor)->torch.Tensor:
"""
Parameters
----------
z : torch.Tensor
Latent space
y : torch.Tensor
Labels
"""
y = self.embed(y)
z = torch.cat((z, y), dim=1)
return self.linear(z)
# cVAE class
class cVAE(nn.Module):
def __init__(self, input_dim:int, latent_dim:int, output_dim:int, num_classes:int)->torch.Tensor:
super().__init__()
"""
Parameters
----------
input_dim : int
Dimension of input data, e.g. number of features (28*28=784 for MNIST)
latent_dim : int
Dimension of latent space (z)
output_dim : int
Dimension of output data, e.g. number of features (28*28=784 for MNIST)
num_classes : int
Number of classes in dataset, e.g. 10 for MNIST
"""
self.latent_dim = latent_dim
self.encoder = cEncoder(input_dim, latent_dim, num_classes)
self.decoder = cDecoder(latent_dim, output_dim, num_classes)
def forward(self, x:torch.Tensor, y:torch.Tensor)->Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Parameters
----------
x : torch.Tensor
Input data
y : torch.Tensor
Labels
Returns
-------
reconstructed_x : torch.Tensor
Reconstructed input data
mu : torch.Tensor
Mean of latent space
log_var : torch.Tensor
Log variance of latent space
"""
mu : torch.Tensor
mu, log_var = self.encoder(x, y)
z = self.reparameterize(mu, log_var)
output = self.decoder(z, y)
return output, mu, log_var
def reparameterize(self, mu:torch.Tensor, log_var:torch.Tensor)->torch.Tensor:
"""
Parameters
----------
mu : torch.Tensor
Mean of the latent Gaussian distribution
log_var : torch.Tensor
Log variance of the latent Gaussian distribution
Returns
-------
z : torch.Tensor
Latent space
"""
std = torch.exp(0.5*log_var)
eps = torch.randn_like(std)
z = mu + eps*std
return z
def loss_function(self, x:torch.Tensor, output:torch.Tensor, mu:torch.Tensor, log_var:torch.Tensor)->torch.Tensor:
"""
Parameters
----------
x : torch.Tensor
Input data
output : torch.Tensor
Output data
mu : torch.Tensor
Mean of the latent Gaussian distribution
log_var : torch.Tensor
Log variance of the latent Gaussian distribution
Returns
-------
loss : torch.Tensor
Loss value
"""
# MSE loss
mse_loss = nn.MSELoss(reduction='sum')
# Reconstruction loss
recon_loss = mse_loss(output, x)
# KL divergence loss
KLD = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
# Total loss
loss = recon_loss + KLD
return loss
def sample(self, num_samples:int, y:torch.Tensor)->torch.Tensor:
"""
Parameters
----------
num_samples : int
Number of samples to generate
y : torch.Tensor
Labels
Returns
-------
samples : torch.Tensor
Generated samples
"""
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
z = torch.randn(num_samples, self.latent_dim).to(device)
samples = self.decoder(z, y)
return samples
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
input_dim = 784
latent_dim = 20
num_classes = 10
output_dim = 784
# Test encoder
x = torch.randn((64, input_dim)).to(device)
y = torch.randint(0, 10, (64,)).to(device)
encoder = cEncoder(input_dim, latent_dim, num_classes).to(device)
mu, log_var = encoder(x, y)
print("mu.shape:", mu.shape)
# Test decoder
decoder = cDecoder(latent_dim, input_dim, num_classes).to(device)
z = torch.randn((64, latent_dim)).to(device)
output = decoder(z, y)
print("output.shape:", output.shape)
# Test cVAE
cvae = cVAE(input_dim, latent_dim, output_dim, num_classes).to(device)
output, mu, log_var = cvae(x, y)
print("output.shape:", output.shape)
print("mu.shape:", mu.shape)
print("log_var.shape:", log_var.shape)
# Test loss function
loss = cvae.loss_function(x, output, mu, log_var)
print("loss:", loss.item())
# Test sample
samples = cvae.sample(64, y)
print("samples.shape:", samples.shape)