问题
随机数的应用场景十分广泛,例如搭建完成网络进行测试的时候需要随机输入,PyTorch提供了rand/randn/randint/randperm等多种随机数的生成方法,那么这些方法的区别是什么呢?在实际开发时,应该如何选择呢?
方法
import torch x = torch.zeros(size=(3, 224, 224))# [0, n) 随机序列a = torch.randperm(10)print(a) # tensor([8, 0, 9, 1, 7, 2, 3, 5, 6, 4])# [a,b) 均匀分布, 注意size参数接受的是一个tuple,而不是整数# b = torch.rand(size=1) # rand(): argument 'size' must be tuple of ints, not intb = torch.rand(size=(1, ))b0 = torch.rand_like(x)print(b0.shape)# [a,b) 标准正太分布d = torch.randn(size=(1,))# [a, b) 随机整数c = torch.randint(1, 10, (1,) )