# 2d output of torch.max instead of 1d array

**URL:** <https://discuss.pytorch.org/t/2d-output-of-torch-max-instead-of-1d-array/100163>\
**Category:** Uncategorized\
**Created:** [October 21, 2020, 3:16pm UTC](https://discuss.pytorch.org/t/2d-output-of-torch-max-instead-of-1d-array/100163 "2020-10-21T15:16:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NadiaMe](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nadiame/32/8864_2.png) [@NadiaMe](https://discuss.pytorch.org/u/NadiaMe)\
**Post date:** [October 21, 2020, 3:16pm UTC](https://discuss.pytorch.org/t/2d-output-of-torch-max-instead-of-1d-array/100163/1 "2020-10-21T15:16:40Z")

</div>

I have the following tensor `T=[100, 10, 10]`  
I want to find max for each of the `100` matrices and get output `[100]`.

When I run  
`out = torch.max(T, dim=0)[0]`

I get 2d output of size `[10, 10]`.  
What does it mean and why I dont get 1d output of size 100 ?

Thanks!

---

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [October 21, 2020, 4:22pm UTC](https://discuss.pytorch.org/t/2d-output-of-torch-max-instead-of-1d-array/100163/2 "2020-10-21T16:22:46Z")

</div>

Hi Nadia!

> [@NadiaMe](#):
>
> I have the following tensor `T=[100, 10, 10]`  
> I want to find max for each of the `100` matrices and get output `[100]`.

`T.max (dim = -1)[0].max (dim = -1)[0]` should work, or you can  
use the equivalent `torch.max()` version, if you prefer. (This could also  
be written `T.max (dim = 2)[0].max (dim = 1)[0]`.)

(If you want to be courageous, you could try the unstable, nightly build,  
e.g., version “1.8.0.dev20201021,” and run `T.amax (dim = (1, 2))`.)

> [@](#):
>
> When I run  
> `out = torch.max(T, dim=0)[0]`
> 
> I get 2d output of size `[10, 10]`.  
> What does it mean and why I dont get 1d output of size 100 ?

`dim = 0` doesn’t mean “keep dim 0.” It means take the `max()` over  
dim 0. So it builds tensor of shape `[10, 10]`, each element of which  
is the `max()` of 100 values, namely the `max()` of the corresponding  
elements of the 100 shape-`[10, 10]` slices of your original tensor.

Best.

K. Frank
