# Understanding tensor.view(M, N)

Could someone kindly explain the logic behind the tensor reshape below? According to what kind of rule is the `b.view(3*2, 2)` tensor being re-organized?

``````import torch¬
a = torch.eye(2)¬
b = a.repeat(1, 3)¬
``````

tensor([[1., 0., 1., 0., 1., 0.],¬
[0., 1., 0., 1., 0., 1.]])¬

`b.view(3*2, 2)¬`

tensor([[1., 0.],¬
[1., 0.],¬
[1., 0.],¬
[0., 1.],¬
[0., 1.],¬
[0., 1.]])¬

Hi,

The view operation will just change the view you look at the memory.
A contiguous Tensor is stored in a row major way, meaning that the rows contain elements that are next to each other in memory.
When you do view, you just read these with different size, still with rows containing elements that are next to each other in memory.

In general, view are often used to collapse and uncollapse dimensions.
In particular, if you want to work with a 1D Tensor in your case, you can do `b.view(3*2*2)`. Then work with this 1D Tensor and finally do `res.view(2, 6)` to get back to the original shape.