a = torch.tensor([1,2,3])print(a)# tensor([1, 2, 3])
b = torch.empty(1,3)# 创建一个指定大小但未初始化的tensorprint(b)# tensor([[9.7209e+14, 4.5914e-41, 9.7209e+14]])
c = torch.zeros(1,3)# 创建一个指定大小且值全为0的tensorprint(c)# tensor([[0., 0., 0.]])
d = torch.ones(1,3)# 创建一个指定大小且值全为1的tensorprint(d)# tensor([[1., 1., 1.]])
e = torch.full((1,3),2)# 创建一个指定大小且值全为指定值的tensorprint(e)# tensor([[2., 2., 2.]])
f = torch.rand(1,3)# 创建一个指定大小且值服从(0,1)间的均匀分布的tensorprint(f)# tensor([[0.4869, 0.1052, 0.5883]])
g = torch.randn(1,3)# 创建一个指定大小且值服从标准正态分布的tensorprint(g)# tensor([[ 0.6983, -0.3339, -0.3049]])
h = torch.randint(low=1, high=10, size=(1,3))# 创建一个指定大小且值是指定区间整数的tensorprint(h)# tensor([[8, 5, 6]])
数据处理操作
x = torch.tensor([-1,1,1,1])
y = torch.tensor([3,3,3,3])print('\nsize:')print(x.size(),type(x.size()))# torch.Size([4]) <class 'torch.Size'>;torch.Size是tuple的子类print(torch.abs(x))# 取绝对值