# Implementing loss function for bivariate Gaussian distribution

Hi,

I’m trying to implement a negative log likelihood loss function for a bivariate Gaussian distribution using torch MultivariateNormal.

my current implementation as follow:

import torch
from torch.distributions.multivariate_normal import MultivariateNormal as MVNormal

``````def Gaussian2DLikelihood(outputs, targets):
#mux is mean of x
#mux is mean of y
#sx,sy is std  >0
#corr is correlation -1<corr<1

batch_size = targets.shape
mux, muy, sx, sy, corr = outputs[:,0], outputs[:,1], outputs[:,2], outputs[:,3], outputs[:,4]
sx = torch.exp(sx)
sy = torch.exp(sy)
corr = torch.tanh(corr)
mux, muy, sx, sy, corr = mux.unsqueeze(-1), muy.unsqueeze(-1), sx.unsqueeze(-1), sy.unsqueeze(-1), corr.unsqueeze(-1)

mu_xy = torch.cat([mux,muy],1)
print('mu_xy.shape:',mu_xy.shape)#mu_xy.shape: torch.Size([8, 2])

cov_xy = torch.zeros(batch_size,2,2) #Covariance matrix
cov_xy[:,0,0] = (sx*sx).squeeze()
cov_xy[:,0,1] = (corr*sx*sy).squeeze()
cov_xy[:,1,0] = (corr*sx*sy).squeeze()
cov_xy[:,1,1] = (sy*sy).squeeze()
print('cov_xy.shape:',cov_xy.shape)#cov_xy.shape: torch.Size([8,2, 2])

loglik = -normal.log_prob(targets[0,:])
for d in range(1,batch_size):
loglik += -normal.log_prob(targets[d,:])

return loglik/batch_size
``````

A test :
`Gaussian2DLikelihood(torch.rand(8,5),torch.rand(8,2))`

I’m not quite sure it’s correct, I’ve seen some examples where people take the log of loglik then they use the log-sum-exp thing.

Any ideas?
{"ft_lang_label":"__label__en","ft_lang_prob":0.5602727,"math_prob":0.99555415,"size":1412,"snap":"2022-40-2023-06","text_gpt3_token_len":425,"char_repetition_ratio":0.14204545,"word_repetition_ratio":0.026666667,"special_character_ratio":0.31657225,"punctuation_ratio":0.2955224,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9935055,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-27T10:12:22Z\",\"WARC-Record-ID\":\"<urn:uuid:091e7044-e020-4e9e-813b-ffbbbc4abbdb>\",\"Content-Length\":\"13727\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b23face0-dc9f-496b-bc20-6d312b29e306>\",\"WARC-Concurrent-To\":\"<urn:uuid:659f4248-1fff-4aa8-854d-5cd06a496a92>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/implementing-loss-function-for-bivariate-gaussian-distribution/52151\",\"WARC-Payload-Digest\":\"sha1:PJYPFO37PJ52T3GMLO4OJSPE6K5PVSAB\",\"WARC-Block-Digest\":\"sha1:RWQ4LRVJXBIPEUSFWVYJKVQPJCZUHGFX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335004.95_warc_CC-MAIN-20220927100008-20220927130008-00373.warc.gz\"}"}