一。标量
a=torch.tensor(4.0)
print(a)
结果:tensor(4.)
二。向量
b=torch.tensor([1.5,0,3])
print(b)
结果:tensor([1.5000, 0.0000, 3.0000])
三。矩阵
c=torch.tensor([[1,2],[3,4]])
print(c)
结果: tensor([[1, 2],
[3, 4]])
四。创建一个空的tensor矩阵,torch.empty(行,列)
d=torch.empty(3,4)
print(d)
结果:
tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
五。创建一个随机矩阵,torch.rand(行,列)
e=torch.rand(3,2)
print(e)
结果:
tensor([[0.0818, 0.4541], [0.8915, 0.2545], [0.1289, 0.6292]])
六。创建一个全0 /1矩阵。torch.zeros/ones(行,列,dtype)
f=torch.zeros(3,2,dtype=torch.int)
print(f)
结果:
tensor([[0, 0],
[0, 0],
[0, 0]], dtype=torch.int32)
f=torch.ones(3,2,dtype=torch.int)
print(f)
结果:
tensor([[1, 1], [1, 1], [1, 1]], dtype=torch.int32)
七。创建一个随机矩阵,shape大小与f一样
g=torch.rand_like(f,dtype=torch.float)
print(g)
结果:
tensor([[0.4747, 0.0751], [0.6963, 0.0123], [0.3358, 0.8233]])
八。加法
x1=torch.tensor([1,2,3])
x2=torch.tensor([4,5,6])
1.print(x1+x2)
结果:
tensor([5, 7, 9])
2.print(torch.add(x1,x2))
结果:
tensor([5, 7, 9])
3.print(x1.add_(x2)) x1 的值改变
结果:
tensor([5, 7, 9])
print(x1)
print(x2)
结果:
tensor([5, 7, 9]) tensor([4, 5, 6])
九。除法
x3=torch.tensor([6,8,9])
x4=torch.tensor([2,4,3])
1.print(x3//x4)
结果:
tensor([3, 2, 3])
2.print(torch.true_divide(x3,x4))
结果:
tensor([3., 2., 3.])
十。乘法
x5=torch.tensor([[1,2],[3,4]])
x6=torch.tensor([[3,4],[5,6]])
1.print(torch.mm(x5,x6))
结果:
tensor([[13, 16], [29, 36]])
2.print(torch.matmul(x5,x6)
结果:
tensor([[13, 16], [29, 36]])
3.print(x5@x6)
结果:
tensor([[13, 16], [29, 36]])
4.print(x5.mm(x6)) x5 值不改变
结果:
tensor([[13, 16], [29, 36]])
十一。幂运算
x7=torch.tensor([1,2,3])
1.print(torch.pow(x7,2))
结果:
tensor([1, 4, 9])
2.print(x7.pow(2))
结果:
tensor([1, 4, 9])
3.print(x7**2)
结果:
tensor([1, 4, 9])
十二。开平方
x8=torch.tensor([1, 4, 9])
1.print(x8.sqrt())
结果:
tensor([1., 2., 3.])
2.print(torch.sqrt(x8))
结果:
tensor([1., 2., 3.])
十三。对数
x9=torch.tensor([2,4,8])
print(torch.log2(x9))
结果:
tensor([1., 2., 3.])
十四。取整/取余
1.向下取整
x10=torch.tensor([3.5,1.3,3.8])
print(x10.floor())
结果:
tensor([3., 1., 3.])
2.向上取整
print(x10.ceil())
结果:
tensor([4., 2., 4.])
3,四舍五入
print(x10.round())
结果:
tensor([4., 1., 4.])
4.只取整数部分
print(x10.trunc())
结果:
tensor([3., 1., 3.])
4.只取小数部分
print(x10.frac())
结果:
tensor([0.5000, 0.3000, 0.8000])
5.取余
x11=torch.tensor([34,55])
print(x11%5)
结果:
tensor([4, 0])
十五。其他tensor操作
1.resize
x12=torch.randint(0,10,(3,2))
print(x12)
结果:
tensor([[5, 1], [0, 6], [8, 7]])
print(x12.resize(2,3))
tensor([[5, 1, 0], [6, 8, 7]])
print(x12.shape)
torch.Size([3, 2])
http://t.csdnimg.cn/gpE4Nhttp://t.csdnimg.cn/gpE4N
2.reshape
print(x12.reshape(3,2))
tensor([[5, 1], [0, 6], [8, 7]])
print(x12.shape)
torch.Size([3, 2])
3.获取一个tensor的值
x13=torch.tensor([3.8])
print(x13.item())
结果:
3.799999952316284
4.排序
x14=torch.tensor([2,1,4,3,6,5,7,8])
print(torch.sort(x14))
结果:
torch.return_types.sort( values=tensor([1, 2, 3, 4, 5, 6, 7, 8]), indices=tensor([1, 0, 3, 2, 5, 4, 6, 7]))
x14=torch.tensor([2,1,4,3,6,5,7,8])
print(torch.sort(x14,descending=True)) 逆排序
结果:
torch.return_types.sort( values=tensor([8, 7, 6, 5, 4, 3, 2, 1]), indices=tensor([7, 6, 4, 5, 2, 3, 0, 1]))