Can't use keepdim in torch.mean()

Tried this example code but got an error:

>>> a = torch.randn(4, 4)
>>> torch.mean(a, 1)

[torch.FloatTensor of size 4x1]

>>> torch.mean(a, 1, True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: torch.mean received an invalid combination of arguments - got (torch.FloatTensor, int, bool), but expected one of:
 * (torch.FloatTensor source)
 * (torch.FloatTensor source, int dim)

How can I solve this error ?
Also, how can I compute the mean of a matrix ? For instance, I have a b x c x h x w tensor, and I would like to compute the mean of each feature map, so that I got a b x c x 1 x 1 mean tensor. Just like np.mean(a, dim=(2,3)) does.

This is a relatively recent change, you may want to upgrade your pytorch version. Also I am not even sure that it is in the binary releases yet and may only be available with the source install.

For your problem, doesn’t the regular torch.mean does what you want?

Thanks for this quick response. You’re right, it’s not in the binary release. I will try compile with the source. Also, it doesn’t do what I want. For instance, I have a 1x3x4x4 tensor, what I would like to do is to compute the mean of the 3 channels so that I get a tensor of size 1x3x1x1, while torch.mean will only calculate mean of a single dimension not cross two dimensions.
>>> import torch
>>> a = torch.randn(1, 3, 4, 4)
>>> torch.mean(a)
>>> torch.mean(a,dim=1)

(0 ,0 ,.,.) = 
 -0.3284  0.1365 -0.4091 -0.6045
 -0.3535 -0.8380  0.0724  0.4533
 -0.2629  0.7440  0.2587 -0.0283
 -0.5283 -0.3177 -0.4128  0.0697
[torch.FloatTensor of size 1x1x4x4]


I use torch.mean(torch.mean(input, dim=2),dim=2) to get what I want now, just wonder if there’s more elegant way.

Found the solution, first view the tensor as bxcx(hxw), then use torch.mean(). Thanks for your help.