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





