# Backward for negative log likelihood loss of MultivariateNormal (in distributions)

**URL:** <https://discuss.pytorch.org/t/backward-for-negative-log-likelihood-loss-of-multivariatenormal-in-distributions/15339>\
**Category:** autograd\
**Created:** [March 23, 2018, 2:57am UTC](https://discuss.pytorch.org/t/backward-for-negative-log-likelihood-loss-of-multivariatenormal-in-distributions/15339 "2018-03-23T02:57:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![svd3](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@svd3](https://discuss.pytorch.org/u/svd3)\
**Post date:** [March 23, 2018, 2:57am UTC](https://discuss.pytorch.org/t/backward-for-negative-log-likelihood-loss-of-multivariatenormal-in-distributions/15339/1 "2018-03-23T02:57:22Z")

</div>

I was trying to minimize the negative log likelihood of a multivariate normal wrt to the mean 1D and covariance matrix  
but it gives me an error

```auto
m = torch.distributions.normal.Normal(mu, C)  
# mu is Variable of size(n, ) 
# C is Variable of size (n, n)
loss = -m.log_prob(x) # x is a variable of size (n, )
# above line gives an error

```

I tried to sample from this distribution but it gives sample with size (n, n) not a (n, ) vector

```auto
 m.sample()

```

I’m stuck here.  
I want to get gradients wrt to mu and Cov. Any idea what can I do

---

<div class="post-metadata">

**Author:** ![fritzo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fritzo/32/34992_2.png) [@fritzo](https://discuss.pytorch.org/u/fritzo)\
**Post date:** [April 21, 2018, 2:08am UTC](https://discuss.pytorch.org/t/backward-for-negative-log-likelihood-loss-of-multivariatenormal-in-distributions/15339/2 "2018-04-21T02:08:59Z")

</div>

`Normal` is a batched univariate distribution. Your `mu` is being broadcast up to the same shape as `C`, producing an `n,n` batch of univariate normals. If you want a `MultivariateNormal` distribution, use

```auto
n = 5
mu = torch.zeros(n)
C = torch.eye(n, n)
m = torch.distributions.MultivariateNormal(mu, covariance_matrix=C)
x = m.sample() # should have shape (n,)
loss = -m.log_prob(x) # should be a scalar

```

---

<div class="post-metadata">

**Author:** ![bhavikngala](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@bhavikngala](https://discuss.pytorch.org/u/bhavikngala)\
**Post date:** [December 29, 2018, 4:41am UTC](https://discuss.pytorch.org/t/backward-for-negative-log-likelihood-loss-of-multivariatenormal-in-distributions/15339/3 "2018-12-29T04:41:29Z")

</div>

> [@svd3](#):
>
> mal is a batched univariate distribu

Hi @fritzo is the MultivariateNormal also a batched class now? if i give it a mean tensor of shape batch\_size x 2 and covar of shape batch\_size x 2 x 2 will it consider the input as batched?

---

<div class="post-metadata">

**Author:** ![fritzo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fritzo/32/34992_2.png) [@fritzo](https://discuss.pytorch.org/u/fritzo)\
**Post date:** [December 31, 2018, 6:43pm UTC](https://discuss.pytorch.org/t/backward-for-negative-log-likelihood-loss-of-multivariatenormal-in-distributions/15339/4 "2018-12-31T18:43:25Z")

</div>

Yes, the result will be batched.
