Reshape, View, Squeeze & Unsqueeze
Change dimensions without copying data.
Same Data, Different Shape
Often the numbers are right but the layout is wrong. Reshaping rearranges a tensor's dimensions without changing the values inside.
reshape Picks a New Layout
Call reshape with the dimensions you want. The total number of elements must stay the same.
x = torch.arange(6)
y = x.reshape(2, 3)
print(y.shape) # torch.Size([2, 3])All lessons in this course
- Shapes, Dtypes & Indexing
- Reshape, View, Squeeze & Unsqueeze
- Broadcasting Rules That Save You Loops
- Tensors Talk to NumPy