CodeBlanks Logo Icon CodeBlanks

Reshape

reshape() and view() change the tensor's shape.

Reshape and view

reshape and view change the shape while keeping the number of elements; -1 lets a dimension be worked out automatically. view only works on tensors that are contiguous in memory and always shares the data; reshape copies if needed:

x = torch.arange(12)
print(x.view(3, 4).shape)        # torch.Size([3, 4])
print(x.reshape(2, -1).shape)    # torch.Size([2, 6])
img = torch.zeros(32, 1, 28, 28)
print(img.view(32, -1).shape)    # torch.Size([32, 784]): flattening

When in doubt, use reshape; it works in every case.

Transpose and permute

transpose(a, b) swaps two axes, permute reorders all axes. Images are in (height, width, channel) order in NumPy/PIL and (channel, height, width) in PyTorch:

hwc = torch.zeros(28, 28, 3)
chw = hwc.permute(2, 0, 1)
print(chw.shape)               # torch.Size([3, 28, 28])
print(chw.is_contiguous())     # False: reshape may be needed instead of view

After permute the tensor isn't contiguous; it is fixed with .contiguous().

Practice Image shapes

Convert an HWC image batch of shape (4, 28, 28, 3) to NCHW with permute(0, 3, 1, 2) and print its shape. Then flatten it with reshape(4, -1) and print its shape, and finally print that it equals the shape of x.flatten(1).

Starter code

model.py
import torch

nhwc = torch.zeros(4, 28, 28, 3)

# buraya yaz

Sample solution

model.py
import torch

nhwc = torch.zeros(4, 28, 28, 3)

x = nhwc.permute(0, 3, 1, 2)
print(x.shape)
d = x.reshape(4, -1)
print(d.shape)
print(d.shape == x.flatten(1).shape)

Expected output

torch.Size([4, 3, 28, 28])
torch.Size([4, 2352])
True

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What shape does <code>x.view(8, -1)</code> give for a tensor of shape (8, 3, 4, 4)?

  • (8, 48) Correct answer
  • (8, 12)
  • (8, 3)
  • (384,)

Explanation: 3 × 4 × 4 = 48; the batch dimension is kept.

2 Convert the image from HWC to CHW.

Question
chw = hwc.____(2, 0, 1)
Answer
chw = hwc.permute(2, 0, 1)

Explanation: permute takes the new order of the axes.

3 Match the operations.

Term Meaning
view Shared reshape
reshape Copies if needed
permute Axis order
flatten(1) Flattens all but batch

All lessons in Advanced