Skip to content

torchrelay.itertools

Same as the itertools python package, but in pytorch.

Overview

product

Cartesian product of a set.

permutations

All possible r-length permutations of a set.

invert_permutation

Return the inverse of a permutation.

product

product(*inputs, r=1, **backend)

Cartesian product of a set.

Parameters:

Name Type Description Default
inputs iterable of tensor_like

Input sets (tensors are flattened is not vectors) with n[i] elements each.

()
r int

Repeats.

keyword argument only

1

Returns:

Name Type Description
output (prod(n)**r, r) tensor

Cartesian product

permutations

permutations(input, r=None)

All possible r-length permutations of a set.

Note

This function loops over the number of input elements, It can therefore be slow if this number is large.

Parameters:

Name Type Description Default
input tensor_like

Input vector (tensor is flattened is not a vector) with n elements.

required
r int

Length of the permutation vectors

n

Returns:

Name Type Description
output (k, r) tensor

All possible r-length permutations of input. k = n! * (n-r)!

invert_permutation

invert_permutation(perm)

Return the inverse of a permutation

Parameters:

Name Type Description Default
perm (..., N) tensor_like

Permutations. A permutation is a shuffled set of indices.

required

Returns:

Name Type Description
iperm (..., N) tensor

Inverse permutation.

Examples:

perm = [0, 2, 3, 1]
a = torch.rand((len(perm),))
permuted_a = a[perm]
recovered_a = permuted_a[invert_permutation(perm)]
assert((a == recovered_a).all())