Created
July 2, 2021 10:40
-
-
Save Akash-Rawat/8877cd993faf46eb9101e5937d1e9ebb to your computer and use it in GitHub Desktop.
Defining loss_function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_loss(reconstructed, caption_prob, images, captions_transformed, mean, log_std): | |
size = captions_transformed.shape[0] | |
reconstruction_error = criterion(reconstructed, images) | |
likelihoods = torch.stack([ | |
caption_prob[i, np.arange(MAX_CAPTION_LEN), captions_transformed[i]] for i in range(size)]) | |
log_likelihoods = -torch.log(likelihoods).sum() | |
KL_divergence = - (1 - mean.pow(2) - torch.exp(2 * log_std) + (2 *log_std)).sum() | |
return reconstruction_error + (log_likelihoods) + KL_divergence, log_likelihoods | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment