liveBook · Manning

Transposition

As explained in Deep Learning with PyTorch, Second Edition

An operation that rearranges the dimensions of a tensor by swapping selected axes, changing its shape and stride without changing its values.

Transposition rearranges a tensor’s dimensions by swapping axes. The values stay the same, while the tensor’s shape and stride ordering change. For a two-dimensional tensor, PyTorch provides both transpose and the shorter t method. section 3.9.2 section 3.7

Turning rows into columns

A 3 × 2 points tensor becomes a 2 × 3 tensor with points.t(). The transposed tensor shares the original storage, so PyTorch creates a new Tensor object with different strides rather than copying the values. section 3.9.2

A stride records how far storage advances when an index increases along each dimension. Transposition swaps the relevant stride ordering: a tensor with stride (2, 1) can become one with stride (1, 2). This view is inexpensive, but it is usually noncontiguous. section 3.9.2 section 3.9.1

# In[30]:
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points

# Out[30]:
tensor([[4., 1.],
        [5., 3.],
        [2., 1.]])

# In[31]:
points_t = points.t()
points_t

# Out[31]:
tensor([[4., 5., 2.],
        [1., 3., 1.]])

Inspect the input points tensor and its transposed rows-and-columns layout. section 3.9.2

For higher-dimensional tensors, transpose(dim0, dim1) swaps the chosen dimensions. A tensor shaped 3 × 4 × 5 becomes 5 × 4 × 3 when dimensions 0 and 2 are exchanged. section 3.9.3

Preparing time-series data

Daily bike data is reshaped into 730 days, 24 hours, and 17 channels, then transposed from N × L × C to N × C × L so the channel dimension comes before time. section 4.4.2

Because a transpose commonly produces a noncontiguous view, operations such as view may require contiguous(). That call creates new storage with the same contents and a row-by-row stride layout. section 3.9.4

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains shapes, strides, shared storage, contiguity, and conversion, stopping short of exhaustive tensor API coverage.
Examples
Demonstrates point coordinates, a three-dimensional tensor, and daily bike data organized by days, channels, and hours.
What it assumes
Assumes basic tensor use while teaching shape, storage, stride, views, and contiguous layouts through concrete examples.
What you can do after
Apply it via PyTorch’s transpose, t, and contiguous methods while checking shape, stride, storage sharing, and contiguity.
How it connects
Connects transposition with tensors, views, strides, storage, contiguity, reshaping, and time-series data representation.

Also explained in

Compare how 6 books explain transposition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.