Skip to content

Instantly share code, notes, and snippets.

@AFAgarap
Created May 16, 2019 09:33
Show Gist options
  • Save AFAgarap/fdfe2d9c3ec3a509122be26854c84e62 to your computer and use it in GitHub Desktop.
Save AFAgarap/fdfe2d9c3ec3a509122be26854c84e62 to your computer and use it in GitHub Desktop.
TensorFlow 2.0 implementation of a variational autoencoder model.
class VariationalAutoencoder(tf.keras.Model):
def __init__(self, latent_dim, original_dim):
super(VariationalAutoencoder, self).__init__()
self.encoder = Encoder(latent_dim=latent_dim)
self.decoder = Decoder(original_dim=original_dim)
def call(self, input_features):
z_mean, z_log_var, latent_code = self.encoder(input_features)
reconstructed = self.decoder(latent_code)
kl_divergence = -5e-2 * tf.reduce_sum(tf.exp(z_log_var) + tf.square(z_mean) - 1 - z_log_var)
self.add_loss(kl_divergence)
return reconstructed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment