megengine.random.permutation¶
- permutation(n, *, dtype='int32')¶
- 随机打乱一个序列,或者返回一个打乱后的范围。
如果 n 是一个张量,只会沿着第一个轴打乱
- 参数
- 返回
返回一个张量
实际案例
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]]