这些方法只是开辟了空间,所附的初始值(非常大,非常小,0),后面还需要我们进行数据的存入。
#torch.empty(d1,d2,d3)函数输入的是shape torch.empty(2,3,5) #tensor([[[-1.9036e-22, 6.8944e-43, 0.0000e+00, 0.0000e+00, -1.0922e-20], # [ 6.8944e-43, -2.8812e-24, 6.8944e-43, -5.9272e-21, 6.8944e-43], # [ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]], # # [[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00], # [ 0.0000e+00, 0.0000e+00, 1.4013e-45, 0.0000e+00, 0.0000e+00], # [ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]]])
#torch.FloatTensor(d1,d2,d3) torch.FloatTensor(2,2) #tensor([[-0.0000e+00, 4.5907e-41], # [-7.3327e-21, 6.8944e-43]])
#torch.IntTensor(d1,d2,d3) torch.IntTensor(2,2) #tensor([[ 0, 1002524760], # [-1687359808, 492]], dtype=torch.int32)
随机均匀分布:rand/rand_like,randint
rand:[0,1)均匀分布;randint(min,max,[d1,d2,d3]) 返回[min,max)的整数均匀分布
#torch.rand(d1,d2,d3) torch.rand(2,2) #tensor([[0.8670, 0.6158], # [0.0895, 0.2391]]) #rand_like() a=torch.rand(3,2) torch.rand_like(a) #tensor([[0.2846, 0.3605], # [0.3359, 0.2789], # [0.5637, 0.6276]]) #randint(min,max,[d1,d2,d3]) torch.randint(1,10,[3,3,3]) #tensor([[[3, 3, 8], # [2, 7, 7], # [6, 5, 9]], # # [[7, 9, 9], # [6, 3, 9], # [1, 5, 6]], # # [[5, 4, 8], # [7, 1, 2], # [3, 4, 4]]])
随机正态分布 randn
randn返回一组符合N(0,1)正态分布的随机数据
#randn(d1,d2,d3) torch.randn(2,2) #tensor([[ 0.3729, 0.0548], # [-1.9443, 1.2485]]) #normal(mean,std) 需要给出均值和方差 torch.normal(mean=torch.full([10],0.),std=torch.arange(1,0,-0.1)) #tensor([-0.8547, 0.1985, 0.1879, 0.7315, -0.3785, -0.3445, 0.7092, 0.0525, 0.2669, 0.0744]) #后面需要用reshape修正成自己想要的形状
#full([d1,d2,d3],num) torch.full([2,2],6) #tensor([[6, 6], # [6, 6]]) torch.full([],6) #tensor(6) 标量 torch.full([1],6) #tensor([6]) 向量
#torch.arange(min,max,step):返回一个[min,max),步长为step的集体数组,默认为1 torch.arange(0,10) #tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) torch.arange(0,10,2) #tensor([0, 2, 4, 6, 8])
#torch.linspace(min,max,steps):返回一个[min,max],数量为steps的数组 torch.linspace(1,10,11) #tensor([ 1.0000, 1.9000, 2.8000, 3.7000, 4.6000, 5.5000, 6.4000, 7.3000, # 8.2000, 9.1000, 10.0000]) #torch.logspace(a,b,steps):返回一个[10^a,10^b],数量为steps的数组 torch.logspace(0,1,10) #tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948, # 7.7426, 10.0000])
#torch.ones(d1,d2) torch.ones(2,2) #tensor([[1., 1.], # [1., 1.]]) #torch.zeros(d1,d2) torch.zeros(2,2) #tensor([[0., 0.], # [0., 0.]]) #torch.eye() 只能接收一个或两个参数 torch.eye(3) #tensor([[1., 0., 0.], # [0., 1., 0.], # [0., 0., 1.]]) torch.eye(2,3) #tensor([[1., 0., 0.], # [0., 1., 0.]])
torch.randperm(8) #tensor([2, 6, 7, 5, 3, 4, 1, 0])
a=torch.rand(4,3,28,28) a[0].shape #torch.Size([3, 28, 28]) a[0,0,0,0] #tensor(0.9373)
a[:2].shape #torch.Size([2, 3, 28, 28]) a[1:].shape #torch.Size([3, 3, 28, 28])
a[:,:,0:28:2,:].shape #torch.Size([4, 3, 14, 28])
a.index_select(0,torch.tensor([0,2])).shape #torch.Size([2, 3, 28, 28]) a.index_select(1,torch.tensor([0,2])).shape #torch.Size([4, 2, 28, 28])
a[...].shape #torch.Size([4, 3, 28, 28]) a[0,...].shape #torch.Size([3, 28, 28]) a[:,2,...].shape #torch.Size([4, 28, 28])
#torch.masked_select 取出掩码对应位置的值 x=torch.randn(3,4) mask=x.ge(0.5) torch.masked_select(x,mask) #tensor([1.6950, 1.2207, 0.6035])
x=torch.randn(3,4) torch.take(x,torch.tensor([0,1,5])) #tensor([-2.2092, -0.2652, 0.4848])