We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Shapes and Strides step 4 of 9
Remove the channel axis while preserving the batch
Implement drop_channel(x) for a tensor with shape (batch, 1, features).
Return the same values with shape (batch, features). Preserve both the batch
and feature axes, including when either has length one.
Why the starter fails
Calling squeeze() without a dimension removes every axis of length one:
input shape squeeze() result required result
(4, 1, 3) (4, 3) (4, 3)
(1, 1, 3) (3,) (1, 3)
(2, 1, 1) (2,) (2, 1)
(1, 1, 1) () (1, 1)
The starter works only when both batch and features are greater than one.
A small final batch can expose the missing batch axis, but a single feature
can cause the same kind of mistake.
Remove only the channel axis
squeeze(dim=1) removes axis 1 when its length is one and leaves every other
axis in place. Here, axis 1 is the channel axis you want to remove.
Use an explicit dimension when you need to preserve other length-one axes. The argument-free form is useful when removing all such axes is intentional.
Your task
def drop_channel(x: torch.Tensor) -> torch.Tensor
For example, values [[[1., 2., 3.]]] should become [[1., 2., 3.]], with
shape (1, 3) rather than (3,).
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Implement drop_channel(x) for a tensor with shape (batch, 1, features).
Return the same values with shape (batch, features). Preserve both the batch
and feature axes, including when either has length one.
Why the starter fails
Calling squeeze() without a dimension removes every axis of length one:
input shape squeeze() result required result
(4, 1, 3) (4, 3) (4, 3)
(1, 1, 3) (3,) (1, 3)
(2, 1, 1) (2,) (2, 1)
(1, 1, 1) () (1, 1)
The starter works only when both batch and features are greater than one.
A small final batch can expose the missing batch axis, but a single feature
can cause the same kind of mistake.
Remove only the channel axis
squeeze(dim=1) removes axis 1 when its length is one and leaves every other
axis in place. Here, axis 1 is the channel axis you want to remove.
Use an explicit dimension when you need to preserve other length-one axes. The argument-free form is useful when removing all such axes is intentional.
Give each tensor axis a meaning
A tensor's shape tells you how many entries each axis has. It does not tell you
what those entries represent. For example, (batch, features) and
(features, batch) use different axis orders. Name the axes before choosing an
operation.
A reduction combines entries along an axis, for example by summing or
averaging. Averaging the features of a (B, D) tensor gives one number per
example, with shape (B,). Keeping the reduced axis gives shape (B, 1) instead.
For [[2, 4, 6], [1, 3, 5]], the row means with the axis kept are [[4], [3]].
Subtracting them from the input gives [[-2, 0, 2], [-2, 0, 2]]: each row has
been centered using its own mean.
Reshape and transpose do different jobs
A reshape regroups elements using the operation's traversal order. A transpose
changes the axis order. For the usual row-major reshape, [[1, 2, 3], [4, 5, 6]]
reshaped to (3, 2) becomes [[1, 2], [3, 4], [5, 6]]. Transposing the original
instead gives [[1, 4], [2, 5], [3, 6]].
Both results have shape (3, 2), but their entries mean different things. Check
values as well as shapes. Unequal axis sizes make many axis mistakes easier to
spot than square examples do.
Concatenate along an axis or stack on a new one
Concatenation extends an existing axis. Stacking creates a new axis. For two
arrays of shape (2, 3), concatenating on axis zero gives (4, 3). Stacking on
a new leading axis gives (2, 2, 3).
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.