megengine.random.permutation

permutation(n, *, dtype='int32')
随机打乱一个序列,或者返回一个打乱后的范围。

如果 n 是一个张量,只会沿着第一个轴打乱

参数
  • n (Union[int, Tensor]) – 如果 n 是一个整数,会返回 0 到 n-1 的一个随机张量。如果 n 是一个张量会返回一个新的打乱后的张量

  • dtype (str) – 当 n 为整数时返回张量设置的类型。支持 int32,int16 和 float32,默认为 int32

返回

返回一个张量

实际案例

import numpy as np
import megengine as mge
import megengine.random as rand

x = rand.permutation(10, dtype="int32")
print(x.numpy())

x = rand.permutation(10, dtype="float32")
print(x.numpy())

x = mge.tensor(np.arange(18)).reshape(6,3)
x = rand.permutation(x)
print(x.numpy())

输出:

[4 5 0 7 3 8 6 1 9 2]
[3. 4. 9. 0. 6. 8. 7. 1. 5. 2.]
[[12 13 14]
 [ 3  4  5]
 [15 16 17]
 [ 0  1  2]
 [ 9 10 11]
 [ 6  7  8]]