Code

Autoencoder Encoder Sample Code

# <1> Define the input to the encoder (the image). encoder_input = Input(shape=self.input_dim, name='encoder_input') x = encoder_input for i in range(self.n_layers_encoder): conv_layer = Conv2D( filters=self.encoder_conv_filters[i], kernel_size=self.encoder_conv_kernel_size[i], strides=self.encoder_conv_strides[i], padding='same', name='encoder_conv_' + str(i) ) # <2> Stack convolutional layers sequentially on top of each other. x = conv_layer(x) x = LeakyReLU()(x) if self.use_batch_norm: x = BatchNormalization()(x) if self.use_dropout: x = Dropout(rate=0.25)(x) shape_before_flattening = K.int_shape(x)[1:] x = Flatten()(x) # <3> Dense layer that connects this vector to the 2D latent space. encoder_output = Dense(self.z_dim, name='encoder_output')(x) # <4> The Keras model that defines the encoder -- a model that takes an # input image and encodes it into the 2D latent space. self.encoder = Model(encoder_input, encoder_output)

0

1

Updated 2026-07-10

Tags

Data Science