generative deep learning decoder model(decoder_inputxz_decoded decoder(zapplies it to to recover the decoded instantiates the decoder modelwhich turns decoder_inputinto the decoded image the dual loss of vae doesn' fit the traditional expectation of sample-wise function of the form loss(inputtargetthusyou'll set up the loss by writing custom layer that internally uses the built-in add_loss layer method to create an arbitrary loss listing cust om layer used comput he vae loss class customvariationallayer(keras layers layer)def vae_loss(selfxz_decoded) flatten(xz_decoded flatten(z_decodedxent_loss keras metrics binary_crossentropy(xz_decodedkl_loss - - mean z_log_var square(z_meank exp(z_log_var)axis=- return mean(xent_loss kl_lossyou don' use this outputbut the layer must return something def call(selfinputs)you implement custom layers inputs[ by writing call method z_decoded inputs[ loss self vae_loss(xz_decodedcalls the custom layer on self add_loss(lossinputs=inputsthe input and the return customvariationallayer()([input_imgz_decoded]decoded output to obtain the final model output finallyyou're ready to instantiate and train the model because the loss is taken care of in the custom layeryou don' specify an external loss at compile time (loss=none)which in turn means you won' pass target data during training (as you can seeyou only pass x_train to the model in fitlisting training he vae from keras datasets import mnist vae model(input_imgyvae compile(optimizer='rmsprop'loss=nonevae summary((x_train_)(x_testy_testmnist load_data(x_train x_train astype('float ' x_train x_train reshape(x_train shape ( ,)x_test x_test astype('float ' x_test x_test reshape(x_test shape ( ,)vae fit( =x_trainy=noneshuffle=trueepochs= batch_size=batch_sizevalidation_data=(x_testnone)licensed to