-
Notifications
You must be signed in to change notification settings - Fork 70
/
vae.py
executable file
·429 lines (323 loc) · 19.5 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import pandas as pd
import numpy as np
import math
import statistics
from sklearn.datasets import load_digits, load_iris, load_boston, load_breast_cancer
from scipy.stats import multivariate_normal as mvn
from sklearn.model_selection import train_test_split
import sklearn
from copy import deepcopy
from sklearn.metrics import pairwise_distances
np.seterr(all = "warn")
class VariationalAutoencoder():
SigmoidActivation = "sigmoid"
ReLUActivation = "relu"
LinearActivation = "linear"
LeakyReLUActivation = "lrelu"
def __init__(self,
learning_rate = 0.04,
batch_size = 32,
num_hidden_layers = None,
num_neurons_each_layer = None,
z_shape = 4,
epochs = 10):
self.learning_rate = learning_rate
self.batch_size = batch_size
self.epochs = epochs
self.num_hidden_layers = num_hidden_layers
self.num_neurons_each_layer = num_neurons_each_layer
self.z_shape = z_shape
self.activations_functions = {
self.SigmoidActivation: self._sigmoid,
self.LeakyReLUActivation: self._leaky_relu,
self.ReLUActivation: self._relu,
self.LinearActivation: self._linear
}
self.activations_derivatives = {
self.SigmoidActivation: self._sigmoid_derivative,
self.LeakyReLUActivation: self._leaky_relu_derivative,
self.ReLUActivation: self._relu_derivative,
self.LinearActivation: self._linear_derivative
}
# Activations for Encoder and Decoder
self.encoder_activations = [self.LeakyReLUActivation] * self.num_hidden_layers + [self.LinearActivation]
self.decoder_activations = [self.ReLUActivation] * self.num_hidden_layers + [self.SigmoidActivation]
self.num_neurons_each_encoder_layer = self.num_neurons_each_layer
self.num_neurons_each_decoder_layer = self.num_neurons_each_layer[::-1]
def _sigmoid(self, x):
x = np.select([x < 0, x >= 0], [np.exp(x)/(1 + np.exp(x)), 1/(1 + np.exp(-x))])
return x
def _relu(self, x):
return np.maximum(0, x)
def _leaky_relu(self, x):
return np.maximum(0, x)
def _linear(self, x):
return x
def _sigmoid_derivative(self, x):
return self._sigmoid(x) * (1 - self._sigmoid(x))
def _relu_derivative(self, x):
return (np.ones_like(x) * (x > 0))
def _leaky_relu_derivative(self, x):
return
def _linear_derivative(self, x):
return np.ones_like(x)
def _binary_cross_entropy_loss(self, y_hat, y):
loss = np.sum(-y * np.log(y_hat + 1e-15) - (1 - y) * np.log(1 - y_hat + 1e-15))
return loss
def _kl_divergence(self, mu, log_var):
return -0.5 * np.sum(1 + log_var - np.power(mu, 2) - np.exp(log_var))
def _encoder(self, X):
encoder_out = []
for curr_layer in self.encoder_layers:
encoder_out.append([])
# Get the activation for this layer and its function
activation_for_this_layer = self.encoder_activations[curr_layer]
activation_function = self.activations_functions[activation_for_this_layer]
if curr_layer == 0:
previous_layer_output = X
else:
previous_layer_output = encoder_out[curr_layer - 1].copy()
previous_layer_output = np.insert(previous_layer_output, obj = 0, values = 1, axis = 1)
if curr_layer != self.encoder_layers[-1]:
encoder_out[curr_layer] = activation_function(previous_layer_output @ self.encoder_weights[curr_layer].T)
else:
encoder_weights_last_layer = np.transpose(self.encoder_weights[curr_layer], axes = (0, 2, 1))
encoder_out[curr_layer] = activation_function(previous_layer_output @ encoder_weights_last_layer)
encoder_out = np.array(encoder_out)
mu, log_var = encoder_out[-1][0], encoder_out[-1][1]
return mu, log_var, encoder_out
def _decoder(self, z):
decoder_out = []
for curr_layer in self.decoder_layers:
decoder_out.append([])
# Get the activation for this layer and its function
activation_for_this_layer = self.decoder_activations[curr_layer]
activation_function = self.activations_functions[activation_for_this_layer]
if curr_layer == 0:
previous_layer_output = z
else:
previous_layer_output = decoder_out[curr_layer - 1].copy()
previous_layer_output = np.insert(previous_layer_output, obj = 0, values = 1, axis = 1)
decoder_out[curr_layer] = activation_function(previous_layer_output @ self.decoder_weights[curr_layer].T)
xhat_batch = decoder_out[-1]
return xhat_batch, decoder_out
def _forward(self, X):
# Encode
mu, log_var, encoder_out = self._encoder(X)
# Reparametrization trick to sample z from gaussian. First sample x from standard normal distribution.
# Then we use z = mu + sigma*x to get our latent variable.
self.rand_sample = np.random.standard_normal(size = (self.batch_size, self.z_shape))
self.sample_z = mu + np.exp(log_var * .5) * self.rand_sample
# Decode
xhat_batch, decoder_out = self._decoder(self.sample_z)
return mu, log_var, xhat_batch, encoder_out, decoder_out
def _backward_decoder(self, y, decoder_out):
decoder_output_derivatives = deepcopy(decoder_out)
decoder_weight_derivatives = deepcopy(self.decoder_weights)
# We calculate weight derivatives for each data row in the batch and average the
# derivatives at the end.
decoder_weight_derivatives = [decoder_weight_derivatives] * self.batch_size
# Compute the output derivatives
layers_reversed = self.decoder_layers[::-1]
for curr_layer in layers_reversed:
next_layer = curr_layer + 1
# For the last layer simply use the formula
if curr_layer == self.total_decoder_layers - 1:
decoder_output_derivatives[curr_layer] = -y/(decoder_out[curr_layer] + 1e-16) + \
(1 - y) * 1/(1 - decoder_out[curr_layer] + 1e-16)
continue
# Get the activation derivative function for next layer
activation_for_next_layer = self.decoder_activations[next_layer]
activation_derivative = self.activations_derivatives[activation_for_next_layer]
# The next layer output derivatives
next_layer_output_derivatives = decoder_output_derivatives[next_layer]
# Calculate the activation derivative. Add a 1 for the bias weight
current_layer_output = decoder_out[curr_layer].copy()
current_layer_output = np.insert(current_layer_output, obj = 0, values = 1, axis = 1)
next_layer_activation_derivatives = activation_derivative(current_layer_output @ self.decoder_weights[next_layer].T)
# Remove the bias from the weights. Bias output derivative is 1.
next_layer_weights_without_bias = self.decoder_weights[next_layer][:, 1:]
# Cycle through the batch of next layer activation derivatives
for batch_index, next_layer_activation_derivative in enumerate(next_layer_activation_derivatives):
next_layer_activation_derivative = next_layer_activation_derivative.reshape(-1, 1)
# Multiply each neuron's activation derivative with its weights. This is the Hadmard product
second_term = next_layer_activation_derivative * next_layer_weights_without_bias
# Sum over all the neurons in the next layer to get the output derivative for each
# neuron in the current layer. This is because each neuron contributes to all the neurons
# in the next layer.
decoder_output_derivatives[curr_layer][batch_index] = next_layer_output_derivatives[batch_index] @ second_term
# Update the weights using the output derivative calculated above
for curr_layer in layers_reversed:
# Get the activation for this layer and its derivative function
activation_for_this_layer = self.decoder_activations[curr_layer]
activation_derivative = self.activations_derivatives[activation_for_this_layer]
# If first layer then use the data as the previous layer.
if curr_layer == 0:
previous_layer_output = self.sample_z
else:
prev_layer = curr_layer - 1
previous_layer_output = decoder_out[prev_layer].copy()
previous_layer_output = np.insert(previous_layer_output, obj = 0, values = 1, axis = 1)
# Current layer output derivatives
curr_layer_output_derivatives = decoder_output_derivatives[curr_layer]
# Get current layer's activation derivatives
curr_layer_activation_derivatives = activation_derivative(previous_layer_output @ self.decoder_weights[curr_layer].T)
curr_layer_activation_derivatives = curr_layer_activation_derivatives
# Cycle through the batch of next layer activation derivatives
for batch_index, curr_layer_activation_derivative in enumerate(curr_layer_activation_derivatives):
curr_layer_activation_derivative = curr_layer_activation_derivative.reshape(-1, 1)
# For the current layer multiply each neuron's activation derivatives with all previous layer outputs.
curr_layer_weight_derivatives = curr_layer_output_derivatives[batch_index].reshape(-1, 1) * \
curr_layer_activation_derivative * previous_layer_output[batch_index]
decoder_weight_derivatives[batch_index][curr_layer] = curr_layer_weight_derivatives
# Average the gradients across batch
decoder_weight_derivatives = np.mean(decoder_weight_derivatives, axis = 0)
return decoder_weight_derivatives, decoder_output_derivatives
def _calculate_mu_derivative(self, decoder_output_derivatives):
mu_derivatives = np.zeros((self.batch_size, self.z_shape))
# Add a bias to z
z_with_bias = np.insert(self.sample_z, obj = 0, values = 1, axis = 1)
# Activation derivative function for the first layer of decoder
activation_for_decoder_first_layer = self.decoder_activations[0]
activation_derivative_func = self.activations_derivatives[activation_for_decoder_first_layer]
# Activation derivatives for the first layer of decoder.
decoder_first_layer_activation_derivatives = activation_derivative_func(z_with_bias @ self.decoder_weights[0].T)
decoder_first_layer_weights_without_bias = self.decoder_weights[0][:, 1:]
# Cycle through the batch of next layer's activation derivatives
for batch_index, next_layer_activation_derivative in enumerate(decoder_first_layer_activation_derivatives):
next_layer_activation_derivative = next_layer_activation_derivative.reshape(-1, 1)
second_term = next_layer_activation_derivative * decoder_first_layer_weights_without_bias
mu_derivatives[batch_index] = decoder_output_derivatives[0][batch_index] @ second_term
return mu_derivatives
def _calculate_log_var_derivative(self, decoder_output_derivatives, log_var):
log_var_derivatives = np.zeros((self.batch_size, self.z_shape))
# Add a bias to z
z_with_bias = np.insert(self.sample_z, obj = 0, values = 1, axis = 1)
# Activation derivative function for the first layer of decoder
activation_for_decoder_first_layer = self.decoder_activations[0]
activation_derivative_func = self.activations_derivatives[activation_for_decoder_first_layer]
# Activation derivatives for the first layer of decoder.
decoder_first_layer_activation_derivatives = activation_derivative_func(z_with_bias @ self.decoder_weights[0].T)
decoder_first_layer_weights_without_bias = self.decoder_weights[0][:, 1:]
# Cycle through the batch of next layer's activation derivatives
for batch_index, next_layer_activation_derivative in enumerate(decoder_first_layer_activation_derivatives):
next_layer_activation_derivative = next_layer_activation_derivative.reshape(-1, 1)
second_term = next_layer_activation_derivative * decoder_first_layer_weights_without_bias
log_var_derivatives[batch_index] = (decoder_output_derivatives[0][batch_index] @ second_term) * \
np.exp(log_var * .5)[batch_index] * 0.5 * self.rand_sample[batch_index]
return log_var_derivatives
def _backward_encoder_recon_loss(self, encoder_out, decoder_out, decoder_output_derivatives, log_var):
encoder_output_derivatives = deepcopy(encoder_out)
encoder_weight_derivatives = deepcopy(self.encoder_weights)
# Calculate derivatives of mu and log_var using decoder outputs and derivatives
mu_derivatives = self._calculate_mu_derivative(decoder_output_derivatives)
log_var_derivatives = self._calculate_log_var_derivative(decoder_output_derivatives, log_var)
encoder_output_derivatives_recon = deepcopy(encoder_out)
encoder_weight_derivatives_recon = deepcopy(self.encoder_weights)
# We calculate weight derivatives for each data row in the batch and average the
# derivatives at the end.
encoder_weight_derivatives_recon = [encoder_weight_derivatives_recon] * self.batch_size
print(mu_derivatives)
s
return
def _backward_encoder_kl_loss(self):
return
def _update_weights(self):
return
def _backward(self, xhat_batch, x_batch, encoder_out, decoder_out, log_var):
# Calculate decoder gradients. We use the reconstruction loss to backpropagate through decoder.
decoder_weight_derivatives, decoder_output_derivatives = self._backward_decoder(x_batch, decoder_out)
# Calculate encoder gradients. For encoder, we use both the reconstruction loss and the
# KL Divergence loss.
encoder_weight_derivatives_recon_loss = self._backward_encoder_recon_loss(encoder_out,
decoder_out,
decoder_output_derivatives,
log_var)
encoder_weight_derivatives_kl_loss = self._backward_encoder_kl_loss()
# Update weights using Adam
self._update_weights(decoder_weight_derivatives, encoder_weight_derivatives)
return
def _initialise_weights(self, input_shape):
# Encoder Layers
self.num_neurons_each_encoder_layer.append(2) # 2 for two outputs - mu and sigma
self.total_encoder_layers = self.num_hidden_layers + 1 # +1 for the last output layer
self.encoder_layers = range(self.total_encoder_layers)
# Decoder Layers
self.num_neurons_each_decoder_layer.append(input_shape) # Last layer of decoder has input shape
self.total_decoder_layers = self.num_hidden_layers + 1 # +1 for the last output layer
self.decoder_layers = range(self.total_decoder_layers)
# Empty weight arrays
self.encoder_weights = []
self.decoder_weights = []
# Initialise encoder weights
for layer in self.encoder_layers:
self.encoder_weights.append([])
number_of_neurons_in_this_layer = self.num_neurons_each_encoder_layer[layer]
if layer == 0:
fan_in = input_shape
previous_layer_shape = fan_in
else:
fan_in = self.num_neurons_each_encoder_layer[layer - 1]
previous_layer_shape = 1 + fan_in
fan_out = number_of_neurons_in_this_layer
init_bound = np.sqrt(6. / (fan_in + fan_out))
if layer != self.encoder_layers[-1]:
self.encoder_weights[layer] = np.random.uniform(low = -init_bound,
high = init_bound,
size = (number_of_neurons_in_this_layer,
previous_layer_shape))
else:
# Last layer of encoder outputs mu and sigma whose dimensions
# are of shape z_shape.
self.encoder_weights[layer] = np.random.uniform(low = -init_bound,
high = init_bound,
size = (number_of_neurons_in_this_layer,
self.z_shape,
previous_layer_shape))
# Initialise decoder weights
for layer in self.decoder_layers:
self.decoder_weights.append([])
number_of_neurons_in_this_layer = self.num_neurons_each_decoder_layer[layer]
if layer == 0:
# Input to decoder is the latent variable constructed from
# gaussian distribution
fan_in = self.z_shape
else:
fan_in = self.num_neurons_each_layer[layer - 1]
fan_out = number_of_neurons_in_this_layer
previous_layer_shape = 1 + fan_in # +1 for the bias
init_bound = np.sqrt(6. / (fan_in + fan_out))
self.decoder_weights[layer] = np.random.uniform(low = -init_bound,
high = init_bound,
size = (number_of_neurons_in_this_layer,
previous_layer_shape))
self.encoder_weights = np.array(self.encoder_weights)
self.decoder_weights = np.array(self.decoder_weights)
self.old_encoder_weights = deepcopy(self.encoder_weights)
self.old_decoder_weights = deepcopy(self.decoder_weights)
def _get_batches(self, X):
for i in range(0, X.shape[0], self.batch_size):
yield X[i: i + self.batch_size]
def fit(self, X):
# Add a bias column to X
X_new = np.column_stack((np.ones(len(X)), X))
# Initialise weights using Glorot Uniform initialiser
self._initialise_weights(X_new.shape[1])
# Get batches
batches = self._get_batches(X_new)
iterations = 0
while iterations <= self.epochs:
# Train using mini-batch SGD
for x_batch in batches:
# Forward pass
mu, log_var, xhat_batch, encoder_out, decoder_out = self._forward(x_batch)
# Reconstruction Loss - between decoded output and input data
reconstruction_loss = self._binary_cross_entropy_loss(xhat_batch, x_batch)
# Calculate KL Divergence between sampled z (Gaussian Distribution: N(mu, sigma))
# and N(0, 1)
kl_loss = self._kl_divergence(mu, log_var)
loss = reconstruction_loss + kl_loss
loss = loss / self.batch_size
# Backward pass - for every result in the batch
# calculate gradient and update the weights using Adam
self._backward(xhat_batch, x_batch, encoder_out, decoder_out, log_var)