Module phiml.math.perm

Functions related to tensor permutation.

Functions

def all_permutations(dims: phiml.math._shape.Shape, list_dim=(~permᵈ=None), index_dim: Optional[phiml.math._shape.Shape] = (indexᶜ=None), convert=False) ‑> phiml.math._tensors.Tensor

Returns a Tensor containing all possible permutation indices of dims along list_dim.

Args

dims
Dims along which elements are permuted.
list_dim
Single dim along which to list the permutations.
index_dim
Dim listing vector components for multi-dim permutations. Can be None if dims.rank == 1.
convert
Whether to convert the permutations to the default backend. If False, the result is backed by NumPy.

Returns

Permutations as a single index Tensor.

def optimal_perm(cost_matrix: phiml.math._tensors.Tensor)

Given a pair-wise cost matrix of two equal-size vectors, finds the optimal permutation to apply to one vector (corresponding to the dual dim of cost_matrix) in order to obtain the minimum total cost for a bijective map.

Args

cost_matrix
Pair-wise cost matrix. Must be a square matrix with a dual and primal dim.

Returns

dual_perm
Permutation that, when applied along the vector corresponding to the dual dim in cost_matrix, yields the minimum cost.
cost
Optimal cost vector, listed along primal dim of cost_matrix.
def pick_random(value: ~TensorOrTree, dim: Union[str, Sequence[+T_co], set, phiml.math._shape.Shape, Callable, None], count: Union[int, phiml.math._shape.Shape, None] = 1, weight: Optional[phiml.math._tensors.Tensor] = None) ‑> ~TensorOrTree

Pick one or multiple random entries from value.

Args

value
Tensor or tree. When containing multiple tensors, the corresponding entries are picked on all tensors that have dim. You can pass range (the type) to retrieve the picked indices.
dim
Dimension along which to pick random entries. Shape with one dim.
count
Number of entries to pick. When specified as a Shape, lists picked values along count instead of dim.
weight
Probability weight of each item along dim. Will be normalized to sum to 1.

Returns

Tensor or tree equal to value.

def random_permutation(*shape: Union[phiml.math._shape.Shape, Any], dims=<function non_batch>, index_dim=(indexᶜ=None)) ‑> phiml.math._tensors.Tensor

Generate random permutations of the integers between 0 and the size of shape.

When multiple dims are given, the permutation is randomized across all of them and tensor of multi-indices is returned.

Batch dims result in batches of permutations.

Args

*shape
Shape of the result tensor, including dims and batches.
*dims
Sequence dims for an individual permutation. The total Shape.volume defines the maximum integer. All other dims from shape are treated as batch.

Returns

Tensor