Skip to content

gradient for Multivariate normal  #5954

@svd3

Description

@svd3

I was trying to achieve backprop through the parameters of a multivariate normal distributions something like pathwise derivative.
Pytorch Docs for MultivariateNormal
My loss was negative log probability, but backward() gives error. To debug I tried to generate sample from this distribution but and it returns a matrix when it should return a vector (individual sample)

Here's a simple code to reproduce

import numpy as np
import torch
from torch.autograd import Variable
from torch.distributions.normal import Normal

mu = Variable(torch.randn(5), requires_grad=True)
C = np.random.randn((5,5)) * 0.01
C = C.T.dot(C) # only to make it +ve semi definite
C = Variable(torch.Tensor(C), requires_grad=True)

# for my case these variables are created by a network and i need to take a backward
m = Normal(mu, C)

target = Variable(torch.randn(5)) # target variable is from test

# need to minimize the -ve log probability of target wrt the parameters (here mu and C)

loss = -m.log_prob(target) # this gives loss as a matrix with nan
# but log_prob even for multivariate should be a scalar for single input
loss.backward()  # loss.backward() won't work as it needs a scalar
print(m.sample()) # should be a vector either (5, 1) or (5, ) but it gives (5, 5) size output 

I think there is some problem with implementation for MultivariateNormal

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions