randperm

paddle.fluid.layers.randperm(n, out=None, dtype='int64', device=None, stop_gradient=True, seed=0)[source]

This operator returns a random permutation of integers from 0 to n-1.

Parameters
  • n (int) – The upper bound (exclusive), and it should be greater than 0.

  • out (Variable, optional) – Optional output which can be any created Variable that meets the requirements to store the result of operation. If out is None, a new Varibale will be create to store the result. Default: None.

  • dtype (np.dtype|core.VarDesc.VarType|str, optional) – The type of the output Tensor. Supported data types: int64, int32. Default: int32.

  • device (str, optional) – Specific the output variable to be saved in cpu or gpu memory. Supported None, ‘cpu’, ‘gpu’. If it is None, the output variable will be automatically assigned devices. Default: None.

  • stop_gradient (bool, optional) – Whether grad should record operations on the returned tensor. Default: True.

  • seed (int, optional) – Random seed used for permute samples. If seed is equal to 0, it means use a seed generated by the system. Note that if seed is not 0, this operator will always generate the same random permutation every time. Default: 0.

Returns

The output tensor of randperm op.

Return Type:

Variable

Examples

import paddle.fluid as fluid

num = 6
is_use_gpu = False

data_1 = fluid.layers.randperm(num)
fluid.layers.Print(data_1)

data_2 = fluid.layers.randperm(num, dtype="int32", seed=1)
fluid.layers.Print(data_2)

data_3 = fluid.layers.randperm(num, stop_gradient=False, device="cpu")
fluid.layers.Print(data_3)

fluid.layers.randperm(num, out=data_3)
fluid.layers.Print(data_3)

place = fluid.CUDAPlace(0) if is_use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
exe.run()