torch.combinations
function combinations(input: Tensor, options?: CombinationsOptions): Tensorfunction combinations(input: Tensor, r: number, with_replacement: boolean, options?: CombinationsOptions): TensorReturns all r-length combinations of elements from the input tensor.
Generates all possible r-length combinations of input elements without regard to order. With with_replacement=false (default), each element appears at most once per combination (classical combinations). With with_replacement=true, elements can repeat (combinations with replacement). Essential for:
- Feature interaction: Generating all pairwise/n-wise feature interactions
- Hyperparameter search: Exploring combinations of hyperparameters
- Combinatorial optimization: Enumeration problems with constraints
- Statistical testing: Generating all possible test cases
- Data mining: Frequent itemset generation
- Network analysis: Finding all paths/cliques of given length
Implementation: Generates all combinations systematically. With_replacement case is often larger (multiset combinations). Output sorted in lexicographic order.
- Lexicographic order: Results are sorted in lexicographic order of indices
- No repetition (default): Each element at most once per combination
- Combination count: C(n, r) = n!/(r!(n-r)!) without replacement
- Combination count (with rep): C(n+r-1, r) with replacement
- 1D input only: Input must be exactly 1-D
- CPU only: Currently limited to CPU device (conversion via .cpu() if needed)
- Exponential growth: Number of combinations grows rapidly with n and r
- Memory explosion: C(n, r) can be huge; n=20, r=10 → ~184k combinations
- 1D requirement: Will error if input is not 1-D
- CPU only: Input must be on CPU device (no GPU support)
- Dtype: Works with any dtype but indices/values treated as distinct elements
- With replacement growth: With_replacement case is often much larger
Parameters
inputTensor- 1D tensor of elements to combine
optionsCombinationsOptionsoptional
Returns
Tensor– 2D tensor of shape [num_combinations, r] containing all r-combinationsExamples
// All 2-combinations without replacement (C(4,2) = 6)
const x = torch.tensor([1, 2, 3, 4]);
const combs = torch.combinations(x);
// [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
// All 3-combinations (C(4,3) = 4)
const combs3 = torch.combinations(x, 3);
// [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
// All 2-combinations with replacement (C(4+2-1,2) = 10)
const combs_rep = torch.combinations(x, 2, true);
// [[1, 1], [1, 2], [1, 3], [1, 4], [2, 2], [2, 3], [2, 4], [3, 3], [3, 4], [4, 4]]
// Feature interactions (e.g., polynomial features)
const features = torch.tensor([0, 1, 2]); // 3 features
const feature_pairs = torch.combinations(features, 2);
// Get all pairwise interactions: (0,1), (0,2), (1,2)
// Can use indices to create interaction features from original data
// Hyperparameter combinations
const learning_rates = torch.tensor([0, 1, 2]); // Indices of 3 learning rates
const batch_sizes = torch.tensor([0, 1, 2]); // Indices of 3 batch sizes
// For grid search: combine all pairs to test
// With replacement: all pairs including same element twice
const elements = torch.tensor([1, 2, 3]);
const pairs_rep = torch.combinations(elements, 2, true);
// (1,1), (1,2), (1,3), (2,2), (2,3), (3,3)
// Single element combinations
const x = torch.tensor([1, 2, 3]);
const singles = torch.combinations(x, 1);
// [[1], [2], [3]] - not very useful but valid
// Zero-length combinations (edge case)
const empty = torch.combinations(x, 0);
// [[]] - one empty combinationSee Also
- PyTorch torch.combinations()
- cartesian_prod - Cartesian product (related but allows all repeats)
- permutations - All permutations (order matters, different from combinations)
- multinomial - Random sampling with replacement from distribution