2020-10-10 15:51:54 +08:00
|
|
|
import numpy as np
|
|
|
|
import paddle
|
|
|
|
|
|
|
|
def shuffle_dim(x, axis, perm=None):
|
|
|
|
"""Permute input tensor along aixs given the permutation or randomly.
|
|
|
|
|
2020-12-18 15:30:56 +08:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
x : Tensor
|
|
|
|
The input tensor.
|
|
|
|
|
|
|
|
axis : int
|
|
|
|
The axis to shuffle.
|
|
|
|
|
|
|
|
perm : List[int], ndarray, optional
|
|
|
|
The order to reorder the tensor along the `axis`-th dimension.
|
|
|
|
|
|
|
|
It is a permutation of ``[0, d)``, where d is the size of the
|
|
|
|
``axis``-th dimension of the input tensor. If not provided,
|
|
|
|
a random permutation is used. Defaults to None.
|
2020-10-10 15:51:54 +08:00
|
|
|
|
2020-12-18 15:30:56 +08:00
|
|
|
Returns
|
|
|
|
---------
|
|
|
|
Tensor
|
|
|
|
The shuffled tensor, which has the same shape as x does.
|
2020-10-10 15:51:54 +08:00
|
|
|
"""
|
|
|
|
size = x.shape[axis]
|
|
|
|
if perm is not None and len(perm) != size:
|
|
|
|
raise ValueError("length of permutation should equals the input "
|
|
|
|
"tensor's axis-th dimension's size")
|
|
|
|
if perm is not None:
|
|
|
|
perm = np.array(perm)
|
|
|
|
else:
|
|
|
|
perm = np.random.permutation(size)
|
|
|
|
|
|
|
|
perm = paddle.to_tensor(perm)
|
|
|
|
out = paddle.gather(x, perm, axis)
|
2020-10-14 10:05:26 +08:00
|
|
|
return out
|