chainer.functions.permutate

chainer.functions.permutate(x, indices, axis=0, inv=False)[source]

Permutates a given variable along an axis.

This function permutate x with given indices. That means y[i] = x[indices[i]] for all i. Note that this result is same as y = x.take(indices). indices must be a permutation of [0, 1, ..., len(x) - 1].

When inv is True, indices is treated as its inverse. That means y[indices[i]] = x[i].

Parameters:
  • x (Variable or numpy.ndarray or cupy.ndarray) – Variable to permutate. A \((s_1, s_2, ..., s_N)\) -shaped float array.
  • indices (Variable or numpy.ndarray or cupy.ndarray) – Indices to extract from the variable. A one-dimensional int array.
  • axis (int) – Axis that the input array is permutate along.
  • inv (bool) – If True, indices is treated as its inverse.
Returns:

Output variable.

Return type:

Variable

Example

>>> x = np.arange(6).reshape((3, 2)).astype(np.float32)
>>> x
array([[0., 1.],
       [2., 3.],
       [4., 5.]], dtype=float32)
>>> indices = np.array([2, 0, 1], np.int32)
>>> y = F.permutate(x, indices)
>>> y.data
array([[4., 5.],
       [0., 1.],
       [2., 3.]], dtype=float32)
>>> y = F.permutate(x, indices, inv=True)
>>> y.data
array([[2., 3.],
       [4., 5.],
       [0., 1.]], dtype=float32)
>>> indices = np.array([1, 0], np.int32)
>>> y = F.permutate(x, indices, axis=1)
>>> y.data
array([[1., 0.],
       [3., 2.],
       [5., 4.]], dtype=float32)