How to manually obtain the minus log-likelihood in Pytorch?
I'm implementing a VAE and I want to obtain the negativev log-likelihood manually (not using an existing function). The given equation is equation1, and I have also found it can as well be expressed as equation2. I have been stuck on this for a couple of days now, and don't know where my code is wrong.
def loss_loglik(y_mean, y_logvar, x):
    out_1 = (x.size()[2]*x.size()[3] / 2) * np.log(2 * np.pi)
    out_2 = (x.size()[2]*x.size()[3] / 2) * torch.log(y_logvar.exp())
    x_diff = x - y_mean
    out_3 = torch.sum(x_diff.pow(2)) / (2 * y_logvar.exp())
    loss = out_1 + out_2 + out_3

The shape of the three arguments is (batch_size, 1, 28, 28).

It seems like eq. 2 is wrong.
It should have been something like. I did not derive it, just trying to match with the input... so, please verify.

I modified your function below.
def loss_loglik(y_mean, y_logvar, x):
    m, n  = x.size()[2], x.size()[3]
    b = x.size()[0]
    
    y_mean = y_mean.reshape(B, -1)
    y_logvar = y_loagvar.reshape(B, -1)


    out_1 = (m*n / 2) * np.log(2 * np.pi)
    
    out_2 = (1 / 2) * y_logvar.sum(dim=1)
    
    x_diff = x - y_mean
    # note sigma is inside the sum
    out_3 = torch.sum(x_diff.pow(2) / (2 * y_logvar.exp()), dim=1)
    
    loss = out_1 + out_2 + out_3
    
    return -loss      
    # shape of loss will be (batchsize,) 

