Explain Tensor.reshape by examples

I don’t understand how exactly Tensor.reshape transforms input data. Can you give me some examples?

From the docs it says for one dimension use -1 but what does it mean? https://pytorch.org/docs/master/torch.html#torch.reshape

-1 is a bit of “deduce it” number here.

For example, if you have an input tensor of shape (128, 128) and you try to reshape it into (-1, 128, 32), the first dimension is deduced so the tensor will still contain 128*128 elements. In this case it would be 4 (because 4*128*32 == 128*128). If you would use shape (64, -1), the resulting tensor would have shape (64, 256).

1 Like

Hey @umrashrf

Reshaping returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input.

For more details on reshape operation- check this