Section 1 - Instruction

Welcome back! You've mastered creating and accessing tensors. Now let's learn to reshape them - one of the most powerful operations in machine learning.

Reshaping changes how your data is organized without changing the actual values. It's like rearranging books on different shelves.

Engagement Message

Can you think of why you might want to reorganize tensor dimensions?

Section 2 - Instruction

The reshape() operation changes tensor dimensions while preserving total elements. A tensor with 12 elements can become (3,4), (2,6), or (12,1).

Engagement Message

What other valid shapes could this 6-element tensor take?

Section 3 - Instruction

Sometimes, tensors include "singleton" dimensions—dimensions where the size is 1, such as (1, 3, 5) or (4, 1, 2). These dimensions don't hold extra data, but they can change how tensors interact in operations like addition or multiplication.

Engagement Message

Have you ever seen a tensor shape with a 1 in it and wondered what purpose it serves?

Section 4 - Instruction

squeeze() removes dimensions of size 1. It's perfect for cleaning up unnecessary dimensions.

Engagement Message

Why might you want to remove these "singleton" dimensions?

Section 5 - Instruction

unsqueeze(dim) adds a dimension of size 1 at the specified position. This is crucial for matching dimensions in operations.

Engagement Message

What would tensor.unsqueeze(1) create instead?

Section 6 - Instruction

transpose(dim0, dim1) swaps two dimensions. For matrices, tensor.transpose(0, 1) flips rows and columns.

Engagement Message

How does transposing help in matrix multiplication?

Section 7 - Instruction

Reshaping is safe when total elements match: (2,6) to (3,4) works because 2×6 = 3×4 = 12.

Dangerous reshapes include (2,3) to (2,4) - you can't create elements from nothing! PyTorch will throw an error.

Engagement Message

What's the total element count for a tensor with shape (5, 3, 2)?

Section 8 - Practice

Type

Sort Into Boxes

Practice Question

Sort the following reshape targets into the correct boxes for a tensor with shape (4, 6).

Labels

  • First Box Label: Valid
  • Second Box Label: Invalid

First Box Items

  • (3,8)
  • (2,12)
  • (24,1)

Second Box Items

  • (6,5)
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal