Conditional_VAE
Conditional_VAE copied to clipboard
A question about the losses executing
Thanks about your good work! I just have a question: in the code of CVAE, you build losses as the following methods:
def vae_loss(y_true, y_pred): # E[log P(X|z)] recon = K.sum(K.binary_crossentropy(y_pred, y_true), axis=1) # D_KL(Q(z|X) || P(z|X)) kl = 0.5 * K.sum(K.exp(z_log_var) + K.square(z_mean) - 1. - z_log_var, axis=1) return recon + kl
def KL_loss(y_true, y_pred): return(0.5 * K.sum(K.exp(z_log_var) + K.square(z_mean) - 1. - z_log_var, axis=1))
def recon_loss(y_true, y_pred): return(K.sum(K.binary_crossentropy(y_pred, y_true), axis=1))
and the model fit like this:
compile and fit
cvae.compile(optimizer=optim, loss=vae_loss, metrics = [KL_loss,recon_loss])#, recon_loss]) cvae_hist = cvae.fit([X_train, y_train], X_train, batch_size=m, epochs=n_epoch, validation_data = ([X_test, y_test], X_test), callbacks = [EarlyStopping(patience = 5)])
What I wonder is : what are the labels for the recon_loss??And what is the use of second X_train in the fit()?? For the reonstruction_loss and KL_loss just used different information, I am puzzled about this. Could you help me?? Thank you very much!
For recon_loss, the target (y_true) is original X_train data. The model's output is reconstructed data. So recon_loss punishes the network if it produces different output than the original inputs. This is the main idea behind autoencoders. So the labels are original inputs itself!
In fit function, therefore, [X_train, y_train] are inputs, and X_train is also the model's target.