torch.rand和torch.randn有什么区别?
一个均匀分布,一个是标准正态分布。
torch.rand()
#rand(*size, out=None, dtype=None)
t1 = torch.rand(2,3)
print(t1,t1.type())
randn(*size, out=None, dtype=None)和randn_like(input, dtype=None)
#randn(*size, out=None, dtype=None)
#randn_like(input, dtype=None)
t1 = torch.randn(2,3)
t2 = torch.randn_like(t1)
print(t1,t1.type())
print(t2,t2.type())
torch.randint(),torch,randint_like()
#randint(low=0, high, size, out=None, dtype=None)
#randint_like(input, low=0, high, dtype=None)
#整数范围[low, high)
t1 = torch.randint(1,4,(2,3,2)) #形状写成[2,3,2]也行
t2 = torch.randint_like(t1,4)
print(t1)
print(t2)
torch.randperm()
randperm(n, out=None, dtype=torch.int64)-> LongTensor
#torch中没有random.shuffle
#y = torch.randperm(n) y是把1到n这些数随机打乱得到的一个数字序列
import torch
#randperm(n, out=None, dtype=torch.int64)-> LongTensor
idx = torch.randperm(3)
a = torch.Tensor(4,2)
print(a)
print(idx,idx.type())
print(a[idx])