torch.atleast_2d
function atleast_2d(input: Tensor): voidReturns a tensor with at least 2 dimensions.
Ensures input tensors have at least 2 dimensions. Useful for:
- Matrix operations: Functions requiring 2D or higher inputs
- Batch dimension addition: Converting 1D vectors to [1, N] for batch processing
- Linear algebra: Preparing inputs for matrix multiplication
- Uniform shape enforcement: Standardizing tensor ranks across pipelines
Reshaping rules:
- 0D (scalar): Reshaped to
[1, 1] - 1D (vector): Reshaped to
[1, N]to add batch dimension - 2D or higher: Returned unchanged
- 1D becomes [1, N]: A 1D vector of shape
[N]becomes[1, N](row vector), not[N, 1](column vector). This preserves the original dimensionality. - Scalar becomes [1, 1]: A 0D scalar becomes a 1×1 matrix, useful for operations that need explicit 2D structure.
- Zero-copy for higher dims: Tensors that are already 2D or higher are returned unchanged with no reshape overhead.
- Matrix compatibility: After atleast_2d, you can reliably use matrix operations like matmul, transpose, determinant, etc.
- 1D reshaping direction: 1D vectors are always reshaped to row vectors
[1, N]. If you need column vectors[N, 1], useunsqueezeorreshapedirectly. - Broadcasting gotcha: A 1D vector of shape
[N]becomes[1, N]. When broadcasting with a 2D matrix of shape[M, N], it will broadcast along the first dimension.
Parameters
inputTensor- The input tensor (any dimensions, including 0D and 1D)
Returns
Tensor with at least 2 dimensions: - 0D tensors become shape [1, 1] - 1D tensors of shape [N] become [1, N] - Higher-dimensional tensors are unchanged
Examples
// Scalar becomes [1, 1]
const scalar = torch.tensor(42);
torch.atleast_2d(scalar); // Shape [1, 1]
// 1D vector becomes [1, N] (adds batch dimension)
const vec = torch.tensor([1, 2, 3]);
torch.atleast_2d(vec); // Shape [1, 3]
// 2D matrix unchanged
const matrix = torch.tensor([[1, 2], [3, 4]]);
torch.atleast_2d(matrix); // Shape [2, 2], unchanged// Batch single sample for processing
const single_sample = torch.randn([784]); // Single MNIST image
const batched = torch.atleast_2d(single_sample); // [1, 784]
// Now compatible with batch processing functions
const output = model(batched); // Expects batch dimension// Matrix multiplication with vector safety
function safe_matmul(a: Tensor, b: Tensor): Tensor {
const a_2d = torch.atleast_2d(a);
const b_2d = torch.atleast_2d(b);
// Both guaranteed to be at least 2D
return a_2d.matmul(b_2d);
}// Feature normalization pipeline
const features = torch.tensor([1.0, 2.0, 3.0]); // 1D: [3]
const normalized = torch.atleast_2d(features); // [1, 3]
const mean = torch.mean(normalized, 1, true); // [1, 1] - feature means
const std = torch.std(normalized, 1, true); // [1, 1] - feature stds
const standardized = (normalized - mean) / std; // Broadcasting works correctlySee Also
- PyTorch torch.atleast_2d()
- atleast_1d - Ensure at least 1 dimension
- atleast_3d - Ensure at least 3 dimensions
- unsqueeze - Add a dimension at specific position
- reshape - Arbitrary shape transformation