Skip to content
Easy Framework

Stack Tensors

Stacking gives each input vector its own row. It adds an axis that identifies which vector a value came from.

Your task

Implement stack_tensors(tensors).

  • tensors is a nonempty list of one-dimensional tensors, each of shape (n,).
  • All inputs have the same length, dtype and device.
  • Return shape (len(tensors), n), preserving the dtype, device and list order. Leave the input tensors unchanged.

For vectors [1, 2, 3] and [4, 5, 6], return [[1, 2, 3], [4, 5, 6]], with shape (2, 3). Each input becomes a row: output[i, j] = tensors[i][j].

Even a list containing just one vector produces a matrix. For [5, 6], the result has shape (1, 2) and values [[5, 6]], not shape (2,).

Use torch.stack(tensors, dim=0) or jnp.stack(tensors, axis=0). Concatenating these vectors would join their values into one longer vector; stacking keeps them separate along a new first axis.