How to ___ Variational AutoEncoder ?

Necessary Imports

import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import keras.layers as L
import keras.backend as K

Sampling class

class sampling(L.Layer):
def __init__(self, intdim, **kwargs):
self.intdim = intdim
super(sampling, self).__init__(**kwargs)

def call(self, args):
z_mean, z_log_sigma = args
batch_size = tf.shape(z_mean)[0]
epsilon = K.random_normal(shape=(batch_size, self.intdim),
mean=0., stddev=1)
return z_mean + z_log_sigma * epsilon

def compute_output_shape(self, input_shape):
return input_shape[0]

def get_config(self):
return config

Variational AutoEncoder Class

class VAutoEnc():
def __init__(self, featurelen, intdim):
self.vocab_len = vocab_len
self.featurelen = feature_len
self.intdim = intdim
self.sample = self.sampling(self.intdim)
self.model = self.RetGraph()

def RetGraph(self):
encinp = L.Input(shape=(self.featurelen,1), dtype='float64')

l = L.Dense(self.intdim)(encinp)

mean = L.Dense(self.intdim)(encinp)
log_sigma = L.Dense(self.intdim)(encinp)

z = self.sample([mean, log_sigma])

out = self.Dense(self.featurelen)(z)

meansigma = L.concatenate([mean, log_sigma], name='meansigma')

model = keras.models.Model(encinp, [z_l, meansigma])

return model

def recloss(self):
def loss1(y_true, y_pred):
reconstruct_loss = tf.keras.losses.MSE(y_pred, y_true)
return reconstruct_loss
return loss1

def vaeloss(self):
def loss2(y_true, y_pred):
mean = y_pred[:,0:self.intdim]
log_sigma = y_pred[:, self.intdim:]
kl_loss = -0.5*K.mean(1+log_sigma-K.square(mean)-K.exp(log_sigma))
return kl_loss



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store