import torch
a = torch.Tensor([[1,2],[3,4]])
print(a)
print(a.type())
a = torch.Tensor(2,3)
print(a)
print(a.type())
'''几种特殊的tensor'''
a = torch.ones(2,2) # 全 1 的 tensor
print(a)
print(a.type())
a = torch.eye(2,2) # 对角线是 1 的 tensor
print(a)
print(a.type())
a = torch.zeros(2,2) # 全 0 的 tensor
print(a)
print(a.type())
b = torch.Tensor(2,3)
# b = torch.zeros_like(b) # 全为 0 ,和 b 一样的 tensor
b = torch.ones_like(b) # 全为 1 ,和 b 一样的 tensor
print(b)
print(b.type())
'''随机'''
a = torch.rand(2,2)
print(a) #生成0-1的随机值
print(a.type())
a = torch.normal(mean=0.0,std=torch.rand(5)) # 5 组正态分布
print(a)
print(a.type())
a = torch.normal(mean=torch.rand(5),std=torch.rand(5)) # 5 组正态分布数据
print(a)
print(a.type())
a = torch.Tensor(2,2).uniform_(-1,1) # 均匀分布
print(a)
print(a.type())
'''序列'''
a = torch.arange(0,11,2) # 0-11,步长为2
print(a)
print(a.type())
a = torch.linspace(2,10,4) # 拿到等间隔的4个数字
print(a)
print(a.type())
a = torch.randperm(10) # 将0-9这10个数字随机的打乱
print(a)
print(a.type())
输出:
tensor([[1., 2.],
[3., 4.]])
torch.FloatTensor
tensor([[0.0000e+00, 0.0000e+00, 1.4013e-45],
[0.0000e+00, 1.4013e-45, 0.0000e+00]])
torch.FloatTensor
tensor([[1., 1.],
[1., 1.]])
torch.FloatTensor
tensor([[1., 0.],
[0., 1.]])
torch.FloatTensor
tensor([[0., 0.],
[0., 0.]])
torch.FloatTensor
tensor([[1., 1., 1.],
[1., 1., 1.]])
torch.FloatTensor
tensor([[0.5896, 0.6741],
[0.4479, 0.3072]])
torch.FloatTensor
tensor([ 0.1183, -1.6184, 0.1392, -0.1067, 0.7052])
torch.FloatTensor
tensor([ 1.0508, -0.1558, 1.2574, 0.9370, 0.8480])
torch.FloatTensor
tensor([[ 0.1837, -0.7270],
[ 0.6481, 0.6339]])
torch.FloatTensor
tensor([ 0, 2, 4, 6, 8, 10])
torch.LongTensor
tensor([ 2.0000, 4.6667, 7.3333, 10.0000])
torch.FloatTensor
tensor([5, 3, 4, 0, 6, 8, 9, 7, 2, 1])
torch.LongTensor