generating images with variational autoencoders layers conv ( padding='same'activation='relu')(input_imgx layers conv ( padding='same'activation='relu'strides=( ))(xx layers conv ( padding='same'activation='relu')(xx layers conv ( padding='same'activation='relu')(xshape_before_flattening int_shape(xx layers flatten()(xx layers dense( activation='relu')(xz_mean layers dense(latent_dim)(xz_log_var layers dense(latent_dim)(xthe input image ends up being encoded into these two parameters next is the code for using z_mean and z_log_varthe parameters of the statistical distribution assumed to have produced input_imgto generate latent space point hereyou wrap some arbitrary code (built on top of keras backend primitivesinto lambda layer in keraseverything needs to be layerso code that isn' part of builtin layer should be wrapped in lambda (or in custom layerlisting lat ent -space-sampling funct ion def sampling(args)z_meanz_log_var args epsilon random_normal(shape=( shape(z_mean)[ ]latent_dim)mean= stddev= return z_mean exp(z_log_varepsilon layers lambda(sampling)([z_meanz_log_var]the following listing shows the decoder implementation you reshape the vector to the dimensions of an image and then use few convolution layers to obtain final image output that has the same dimensions as the original input_img listing vae decoder net workmapping lat ent space point images decoder_input layers input( int_shape( )[ :]input where you'll feed layers dense(np prod(shape_before_flattening[ :])activation='relu')(decoder_inputupsamples the input layers reshape(shape_before_flattening[ :])(xx layers conv dtranspose( padding='same'activation='relu'strides=( ))(xx layers conv ( padding='same'activation='sigmoid')(xreshapes into feature map of the same shape as the feature map just before the last flatten layer in the encoder model licensed to uses conv dtranspose layer and conv layer to decode into feature map the same size as the original image input