To get the mean and variance in tensorflow just use tf.nn.moments.

mean, var = tf.nn.moments(x, axes=[1])

and in numpy mean, var = np.std(x, axes=[1])

What is the equivalent function of getting variance in pytorch?

To get the mean and variance in tensorflow just use tf.nn.moments.

mean, var = tf.nn.moments(x, axes=[1])

and in numpy mean, var = np.std(x, axes=[1])

What is the equivalent function of getting variance in pytorch?

3 Likes

Tensor.mean([dim]), Tensor.std([dim]), and Tensor.var([dim]) for mean, standard deviation, and variance optionally along an axis (dim).

```
>>> import torch
>>> x = torch.Tensor([[0, 1], [2, 3]])
>>> x.mean(dim=1)
0.5000
2.5000
[torch.FloatTensor of size 2]
>>> x.std(dim=1)
0.7071
0.7071
[torch.FloatTensor of size 2]
```

10 Likes

Thanks for your reply. In pytorch, is gradients calculate for mean and standard deviation automatically?

Btw, the std computed by numpy is different!

```
import numpy
>>> x = numpy.array([[0, 1], [2, 3]])
>>> x.std(1)
array([0.5, 0.5])
```

Which one should we use if we want e.g. to compute the std for `transforms.Normalize(mean, std)`

?

1 Like