torch.tensordot
function tensordot<D extends DType = DType, Dev extends DeviceType = DeviceType>(a: Tensor<Shape, D, Dev>, b: Tensor<Shape, D, Dev>, options?: TensordotOptions): Tensor<Shape, D, Dev>function tensordot<D extends DType = DType, Dev extends DeviceType = DeviceType>(a: Tensor<Shape, D, Dev>, b: Tensor<Shape, D, Dev>, dims: number | [number[], number[]], options?: TensordotOptions): Tensor<Shape, D, Dev>Computes generalized tensor contraction over specified dimensions.
Generalizes dot product and matrix multiplication to arbitrary dimensional tensors. Sums over specified dimensions of two tensors (Einstein summation convention). One of the most powerful and flexible operations in linear algebra. Essential for:
- Tensor networks: Physical systems with entanglement and complex interactions
- Multi-dimensional data: Processing batches of matrices, multi-modal data
- Physics simulations: Computing forces, stress tensors, Hamiltonians
- Machine learning: Custom layer operations beyond standard matrix multiplication
- Signal processing: Multi-dimensional convolution and filtering
- Quantum computing: Tensor network algorithms and state evolution
Syntax: tensordot(A, B, dims) contracts dimensions of A with dimensions of B. Two forms: dims=n (contract last n dims of a with first n of b) or dims=[[a_dims], [b_dims]] (contract specified dimensions). The dimensions to contract must have matching sizes. Remaining dimensions are concatenated in output.
Parameters
optionsTensordotOptionsoptional- Optional settings for tensor contraction
Returns
Examples
// Basic tensor contraction
const a = torch.arange(60).reshape([3, 4, 5]); // Shape [3, 4, 5]
const b = torch.arange(24).reshape([4, 3, 2]); // Shape [4, 3, 2]
torch.tensordot(a, b, { dims: [[1, 0], [0, 1]] });
// Contracts a[i,j,k] * b[j,i,l] → result[k,l]
// Result shape: [5, 2]