# Swap axes in pytorch?

**URL:** <https://discuss.pytorch.org/t/swap-axes-in-pytorch/970>\
**Category:** Uncategorized\
**Created:** [March 9, 2017, 1:08pm UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970 "2017-03-09T13:08:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Response777](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/response777/32/4596_2.png) [@Response777](https://discuss.pytorch.org/u/Response777)\
**Post date:** [March 9, 2017, 1:08pm UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970/1 "2017-03-09T13:08:30Z")

</div>

Hi, in tensorflow, we have `data_format` option in `tf.nn.conv2d` which could specify the data format as `NHWC` or `NCHW`.

Is there equivalent operation in pytorch?

If not, should we convert `Variable` to `numpy.array`, use `np.swapaxes` and convert it back into `Variable`?  
And under such circumstances, will the gradient tracked properly?

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [March 9, 2017, 1:14pm UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970/2 "2017-03-09T13:14:14Z")

</div>

No, we only support `NCHW` format. You can use [`.permute`](http://pytorch.org/docs/tensors.html?highlight=permute#torch.Tensor.permute) to swap the axis.

---

<div class="post-metadata">

**Author:** ![Response777](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/response777/32/4596_2.png) [@Response777](https://discuss.pytorch.org/u/Response777)\
**Post date:** [March 9, 2017, 1:18pm UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970/3 "2017-03-09T13:18:51Z")

</div>

Thanks, I have just checked the Docs, but it seems that I just miss it…

---

<div class="post-metadata">

**Author:** ![fmassa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/32/58_2.png) [@fmassa](https://discuss.pytorch.org/u/fmassa)\
**Post date:** [March 9, 2017, 2:37pm UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970/5 "2017-03-09T14:37:12Z")

</div>

@Veril transpose only applies to 2 axis, while permute can be applied to all the axes at the same time.  
For example

```python
a = torch.rand(1,2,3,4)
print(a.transpose(0,3).transpose(1,2).size())
print(a.permute(3,2,1,0).size())

```

BTW, [`permute` internally calls `transpose` a number of times](https://github.com/pytorch/pytorch/blob/master/torch/tensor.py#L199)

---

<div class="post-metadata">

**Author:** ![fmassa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/32/58_2.png) [@fmassa](https://discuss.pytorch.org/u/fmassa)\
**Post date:** [March 9, 2017, 4:18pm UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970/7 "2017-03-09T16:18:54Z")

</div>

Indeed, it can be a shortcut to use

```python
tensor.transpose_(0, 1)

```

instead of

```python
tensor = tensor.transpose(0, 1)

```

But note that the difference in performance is not significant, as `transpose` does not copy memory nor allocate new memory, and only swaps the strides.

---

<div class="post-metadata">

**Author:** ![rahulbhalley](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rahulbhalley/32/39041_2.png) [@rahulbhalley](https://discuss.pytorch.org/u/rahulbhalley)\
**Post date:** [August 24, 2018, 8:36am UTC](https://discuss.pytorch.org/t/swap-axes-in-pytorch/970/8 "2018-08-24T08:36:40Z")

</div>

Awesome method! Why not combine [`permute`](http://pytorch.org/docs/tensors.html?highlight=permute#torch.Tensor.permute) and [`transpose`](https://pytorch.org/docs/stable/torch.html#torch.transpose) or make [`transpose`](https://pytorch.org/docs/stable/torch.html#torch.transpose) inaccessible to user since it’s used internally by [`permute`](https://pytorch.org/docs/stable/torch.html#torch.transpose) as mentioned by [fmassa](https://discuss.pytorch.org/u/fmassa).
