# Use tensor.mean() method but ignore 0 values

**URL:** <https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170>\
**Category:** Uncategorized\
**Created:** [November 6, 2019, 10:47am UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170 "2019-11-06T10:47:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwillette](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jwillette/32/17366_2.png) [@jwillette](https://discuss.pytorch.org/u/jwillette)\
**Post date:** [November 6, 2019, 10:47am UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170/1 "2019-11-06T10:47:21Z")

</div>

I was calling `nonzero()` on a tensor and then getting the mean values, but it turns out that I will need to keep the shape of the original tensor, but just ignore the values that are 0 for the mean calculation, is there a way to do this?

---

<div class="post-metadata">

**Author:** ![JuanFMontesinos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/32/76115_2.png) [@JuanFMontesinos](https://discuss.pytorch.org/u/JuanFMontesinos)\
**Post date:** [November 6, 2019, 11:24am UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170/2 "2019-11-06T11:24:09Z")

</div>

I don’t understand what do you mean by “keep the shape…”. A mean is an scalar, thus, you can’t “keep the shape”.

You just can filter them out like mean = tensor[tensor !=0].mean()

---

<div class="post-metadata">

**Author:** ![jwillette](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jwillette/32/17366_2.png) [@jwillette](https://discuss.pytorch.org/u/jwillette)\
**Post date:** [November 6, 2019, 11:48am UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170/3 "2019-11-06T11:48:21Z")

</div>

right, but the original tensor has a shape and I need to preserve the shape which would exist if I were to do…

```auto
x = torch.ones((2, 3, 4))
x.mean(dim=0)

```

which would be a `(3,4)` tensor of the means along dimension 0. I have found that I could do this by summing the elements along the dimension and then somehow dividing by the count of nonzero elements along the same dimension…

But then I realized my problem is a little bit harder than that because I am dealing with one tensor of values and then one probability distribution tensor with the same shape, which gives the probabilities of those values…

```auto
x = torch.rand((2, 3, 4))
y = torch.distributions.Normal(torch.randn((2, 3, 4), torch.randn((2, 3, 4)))
pr = y.log_prob(x)
...

```

at this point I need to get the mean of dimension 0 of the values in y which are 0 in `x` but they are not 0 in `y` because of the `log_prob` call… very confusing about how to go about it

---

<div class="post-metadata">

**Author:** ![JuanFMontesinos](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/32/76115_2.png) [@JuanFMontesinos](https://discuss.pytorch.org/u/JuanFMontesinos)\
**Post date:** [November 6, 2019, 12:07pm UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170/4 "2019-11-06T12:07:21Z")

</div>

So then use a masking  
If I got it you has a tensor (N,M,P)

```auto
mask = x!=0
y_mean = (y*mask).sum(dim=0)/mask.sum(dim=0)

```

I think this would fit what you want right?

---

<div class="post-metadata">

**Author:** ![jwillette](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jwillette/32/17366_2.png) [@jwillette](https://discuss.pytorch.org/u/jwillette)\
**Post date:** [November 6, 2019, 12:09pm UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170/5 "2019-11-06T12:09:03Z")

</div>

Yes I believe that will work. thanks for taking the time to show me the way

---

<div class="post-metadata">

**Author:** ![Uwais\_Iqbal](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/uwais_iqbal/32/10458_2.png) [@Uwais\_Iqbal](https://discuss.pytorch.org/u/Uwais_Iqbal)\
**Post date:** [December 11, 2020, 11:23am UTC](https://discuss.pytorch.org/t/use-tensor-mean-method-but-ignore-0-values/60170/6 "2020-12-11T11:23:44Z")

</div>

You could use `.nonzero()` but just reshape back to the original dimensions

`x[x.nonzero(as_tuple=True)].view(-1, x.shape[1])`

and then do `torch.mean()` as normal
