Skip to content

Instantly share code, notes, and snippets.

@AFAgarap
Created May 16, 2019 09:31
Show Gist options
  • Save AFAgarap/27935013850dbd881d49df180b6dec89 to your computer and use it in GitHub Desktop.
Save AFAgarap/27935013850dbd881d49df180b6dec89 to your computer and use it in GitHub Desktop.
TensorFlow 2.0 implementation of an encoder layer for a variational autoencoder.
class Encoder(tf.keras.layers.Layer):
def __init__(self, latent_dim):
super(Encoder, self).__init__()
self.input_layer = tf.keras.layers.InputLayer(input_shape=(28, 28, 1))
self.reshape = tf.keras.layers.Reshape(target_shape=(784, ))
self.hidden_layer_1 = tf.keras.layers.Dense(units=128, activation=tf.nn.relu)
self.hidden_layer_2 = tf.keras.layers.Dense(units=64, activation=tf.nn.relu)
self.hidden_layer_3 = tf.keras.layers.Dense(units=32, activation=tf.nn.relu)
self.z_mean_layer = tf.keras.layers.Dense(units=latent_dim)
self.z_log_var_layer = tf.keras.layers.Dense(units=latent_dim)
self.sampling = Sampling()
def call(self, input_features):
input_features = self.input_layer(input_features)
input_features = self.reshape(input_features)
activation_1 = self.hidden_layer_1(input_features)
activation_2 = self.hidden_layer_2(activation_1)
activation_3 = self.hidden_layer_3(activation_2)
z_mean = self.z_mean_layer(activation_3)
z_log_var = self.z_log_var_layer(activation_3)
z = self.sampling((z_mean, z_log_var))
return z_mean, z_log_var, z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment