一、比较大小
torch.allclose()函数,比较两个元素是否接近,比较A和B是否接近的公式为
|A-B| <= atol+rtol*|B|
import torch
A = torch.tensor([10.0])
B = torch.tensor([10.1])
print(torch.allclose(A,B,rtol=1e-05,atol=1e-08,equal_nan =False))
print(torch.allclose(A,B,rtol=0.1,atol=0.01,equal_nan = False))
# False
# True
#在不同的比较标准下,10和1.1是否接近有着不同的结果
#如果equal_nan=True,那么缺失值之间可以判定为接近
A = torch.tensor(float("nan"))
print(torch.allclose(A,A,equal_nan = False))
print(torch.allclose(A,A,equal_nan = True))
# False
# True
torch.eq()函数
torch.equal()函数
import torch
A = torch.tensor([1,2,3,4,5,6])
B = torch.arange(1,7)
C = torch.unsqueeze(B,dim = 0)
print(torch.eq(A,B))
print(torch.eq(A,C))
# tensor([True, True, True, True, True, True])
# tensor([[True, True, True, True, True, True]])
print(torch.equal(A,B))
print(torch.equal(A,C))
# True
# False
torch.ge()函数
torch.gt()函数
import torch
A = torch.tensor([1,2,3,4,5,6])
B = torch.arange(1,7)
C = torch.unsqueeze(B,dim = 0)
print(torch.ge(A,B))
print(torch.ge(A,C))
# tensor([True, True, True, True, True, True])
# tensor([[True, True, True, True, True, True]])
print(torch.gt(A,B))
print(torch.gt(A,B))
# tensor([False, False, False, False, False, False])
# tensor([False, False, False, False, False, False])
torch.le()函数
torch.lt()函数
import torch
A = torch.tensor([1,2,3,4,5,6])
B = torch.arange(1,7)
C = torch.unsqueeze(B,dim = 0)
print(torch.le(A,B))
print(torch.le(A,C))
# tensor([True, True, True, True, True, True])
# tensor([[True, True, True, True, True, True]])
print(torch.lt(A,B))
print(torch.lt(A,C))
# tensor([False, False, False, False, False, False])
# tensor([[False, False, False, False, False, False]])
torch.ne()函数
torch.isnan()函数
import torch
A = torch.tensor([1,2,3,4,5,6])
B = torch.arange(1,7)
C = torch.unsqueeze(B,dim = 0)
print(torch.ne(A,B))
print(torch.ne(A,C))
# tensor([False, False, False, False, False, False])
# tensor([[False, False, False, False, False, False]])
torch.isnan(torch.tensor([0,1,float("nan"),2]))
# tensor([False, False, True, False])
二、基本运算
逐元素运算
基本运算
import torch
A = torch.arange(6.0).reshape(2,3)
B = torch.linspace(10,20,steps = 6).reshape(2,3)
print("A:",A)
# A: tensor([[0., 1., 2.],
# [3., 4., 5.]])
print("B",B)
# B tensor([[10., 12., 14.],
# [16., 18., 20.]])
##加减乘除四则运算
print(A*B)
# tensor([[ 0., 12., 28.],
# [ 48., 72., 100.]])
print(A/B)
# tensor([[0.0000, 0.0833, 0.1429],
# [0.1875, 0.2222, 0.2500]])
print(A+B)
# tensor([[10., 13., 16.],
# [19., 22., 25.]])
print(A-B)
# tensor([[-10., -11., -12.],
# [-13., -14., -15.]])
print(B//A)
# tensor([[inf, 12., 7.],
# [ 5., 4., 4.]])
##幂运算
print(torch.pow(A,3))
print(A**3)
# tensor([[ 0., 1., 8.],
# [ 27., 64., 125.]])
# tensor([[ 0., 1., 8.],
# [ 27., 64., 125.]])
##指数运算
print(torch.exp(A))
# tensor([[ 1.0000, 2.7183, 7.3891],
# [ 20.0855, 54.5981, 148.4132]])
##对数运算
print(torch.log(A))
# tensor([[ -inf, 0.0000, 0.6931],
# [1.0986, 1.3863, 1.6094]])
##平方根
print(torch.sqrt(A))
print(A**0.5)
# tensor([[0.0000, 1.0000, 1.4142],
# [1.7321, 2.0000, 2.2361]])
# tensor([[0.0000, 1.0000, 1.4142],
# [1.7321, 2.0000, 2.2361]])
##张量的平方根的倒数
print(torch.rsqrt(A))
print(1/(A**0.5))
# tensor([[ inf, 1.0000, 0.7071],
# [0.5774, 0.5000, 0.4472]])
# tensor([[ inf, 1.0000, 0.7071],
# [0.5774, 0.5000, 0.4472]])
张量数据裁剪
根据最大值裁剪 torch.clamp_max()
根据最小值裁剪 torch.clamp_min()
根据范围裁剪 torch.clamp()
import torch
A = torch.arange(6.0).reshape(2,3)
B = torch.linspace(10,20,steps = 6).reshape(2,3)
print("A:",A)
# A: tensor([[0., 1., 2.],
# [3., 4., 5.]])
torch.clamp_max(A,4)
# tensor([[0., 1., 2.],
# [3., 4., 4.]])
torch.clamp_min(A,3)
# tensor([[3., 3., 3.],
# [3., 4., 5.]])
torch.clamp(A,2.5,4)
# tensor([[2.5000, 2.5000, 2.5000],
# [3.0000, 4.0000, 4.0000]])
对于矩阵的运算
import torch
A = torch.arange(6.0).reshape(2,3)
B = torch.linspace(10,20,steps = 6).reshape(2,3)
print("A:",A)
# A: tensor([[0., 1., 2.],
# [3., 4., 5.]])
print("B",B)
# B tensor([[10., 12., 14.],
# [16., 18., 20.]])
##矩阵的转置
C = torch.t(A)
C
# tensor([[0., 3.],
# [1., 4.],
# [2., 5.]])
##矩阵相乘
A.matmul(C)
# tensor([[ 5., 14.],
# [14., 50.]])
A = torch.arange(12.0).reshape(2,2,3)
B = torch.arange(12.0).reshape(2,3,2)
AB = torch.matmul(A,B)
print(A)
# tensor([[[ 0., 1., 2.],
# [ 3., 4., 5.]],
# [[ 6., 7., 8.],
# [ 9., 10., 11.]]])
print(B)
# tensor([[[ 0., 1.],
# [ 2., 3.],
# [ 4., 5.]],
# [[ 6., 7.],
# [ 8., 9.],
# [10., 11.]]])
AB
# tensor([[[ 10., 13.],
# [ 28., 40.]],
# [[172., 193.],
# [244., 274.]]])
##矩阵相乘只计算后边两个维度的乘法
print(AB[0].eq(torch.matmul(A[0],B[0])))
print(AB[1].eq(torch.matmul(A[1],B[1])))
# tensor([[True, True],
# [True, True]])
# tensor([[True, True],
# [True, True]])
##矩阵的逆
C = torch.rand(3,3)
print(C)
# tensor([[0.9568, 0.8140, 0.4885],
# [0.5586, 0.7726, 0.6561],
# [0.1674, 0.9509, 0.8874]])
D = torch.inverse(C)
print(D)
# tensor([[ -1.0509, 4.3900, -2.6674],
# [ 6.5686, -13.0628, 6.0425],
# [ -6.8405, 13.1696, -4.8448]])
torch.mm(C,D)
# tensor([[ 1.0000e+00, -4.8042e-07, -1.1227e-07],
# [-3.3606e-07, 1.0000e+00, -4.4348e-07],
# [-4.2905e-07, 4.1309e-07, 1.0000e+00]])
##torch.mm()是矩阵相乘
##torch.mul()是对应元素相乘
三、统计相关的计算
最大值最小值
import torch
A= torch.tensor([12.,34,25,11,67,32,29,30,99,55,23,44])
###一维张量的最大值最小值
print("最大值:",A.max())
print("最大值位置:",A.argmax())
print("最小值:",A.min())
print("最小值位置:",A.argmin())
# 最大值: tensor(99.)
# 最大值位置: tensor(8)
# 最小值: tensor(11.)
# 最小值位置: tensor(3)
##二维张量的最大值最小值
B = A.reshape(3,4)
print("B",B)
print("每一行最大值:",B.max(dim = 1))
print("最大值位置:",B.argmax(dim =1))
print("每一列最小值:",B.min(dim =0))
print("最小值位置:",B.argmin(dim = 0))
# B tensor([[12., 34., 25., 11.],
# [67., 32., 29., 30.],
# [99., 55., 23., 44.]])
# 每一行最大值: torch.return_types.max(
# values=tensor([34., 67., 99.]),
# indices=tensor([1, 0, 0]))
# 最大值位置: tensor([1, 0, 0])
# 每一列最小值: torch.return_types.min(
# values=tensor([12., 32., 23., 11.]),
# indices=tensor([0, 1, 2, 0]))
# 最小值位置: tensor([0, 1, 2, 0])
排序
import torch
A= torch.tensor([12.,34,25,11,67,32,29,30,99,55,23,44])
##由小到大排序,以及相应元素在原始位置的索引
torch.sort(A)
# torch.return_types.sort(
# values=tensor([11., 12., 23., 25., 29., 30., 32., 34., 44., 55., 67., 99.]),
# indices=tensor([ 3, 0, 10, 2, 6, 7, 5, 1, 11, 9, 4, 8]))
##由大到小排序
torch.sort(A,descending = True)
# torch.return_types.sort(
# values=tensor([99., 67., 55., 44., 34., 32., 30., 29., 25., 23., 12., 11.]),
# indices=tensor([ 8, 4, 9, 11, 1, 5, 7, 6, 2, 10, 0, 3]))
##二维张量排序,对每一行进行排序
B = A.reshape(3,4)
print("B:\n",B)
Bsort,Bsort_id = torch.sort(B)
print("Bsort:\n",Bsort)
print("Bsort index: \n",Bsort_id)
print("Bargsort:\n",torch.argsort(B))
# B:
# tensor([[12., 34., 25., 11.],
# [67., 32., 29., 30.],
# [99., 55., 23., 44.]])
# Bsort:
# tensor([[11., 12., 25., 34.],
# [29., 30., 32., 67.],
# [23., 44., 55., 99.]])
# Bsort index:
# tensor([[3, 0, 2, 1],
# [2, 3, 1, 0],
# [2, 3, 1, 0]])
# Bargsort:
# tensor([[3, 0, 2, 1],
# [2, 3, 1, 0],
# [2, 3, 1, 0]])
torch.topk()前k大的数值及其所在位置
torch.kthvalue()第k小的数值及其所在位置
import torch
A= torch.tensor([12.,34,25,11,67,32,29,30,99,55,23,44])
##获取张量前四个大的值及其所在位置
torch.topk(A,4)
# torch.return_types.topk(
# values=tensor([99., 67., 55., 44.]),
# indices=tensor([ 8, 4, 9, 11]))
##二维
B = A.reshape(3,4)
print("B:\n",B)
Btop2,Btop2_id = torch.topk(B,2,dim = 0)
print("B 每列 top2:\n",Btop2)
print("B 每列 top2 位置:\n",Btop2_id)
# B:
# tensor([[12., 34., 25., 11.],
# [67., 32., 29., 30.],
# [99., 55., 23., 44.]])
# B 每列 top2:
# tensor([[99., 55., 29., 44.],
# [67., 34., 25., 30.]])
# B 每列 top2 位置:
# tensor([[2, 2, 1, 2],
# [1, 0, 0, 1]])
##获取张量第k小的数值和位置
torch.kthvalue(A,3)
# torch.return_types.kthvalue(
# values=tensor(23.),
# indices=tensor(10))
#二维 每一列的第k小数值及其位置
torch.kthvalue(B,3,dim =1)
# torch.return_types.kthvalue(
# values=tensor([25., 32., 55.]),
# indices=tensor([2, 1, 1]))
Bkth,Bkth_id = torch.kthvalue(B,3,dim=1,keepdim = True)
Bkth
# tensor([[25.],
# [32.],
# [55.]])
均值、求和、累加和、中位数、乘积、累乘积、标准差、
import torch
A= torch.tensor([12.,34,25,11,67,32,29,30,99,55,23,44])
B = A.reshape(3,4)
print("B:\n",B)
# B:
# tensor([[12., 34., 25., 11.],
# [67., 32., 29., 30.],
# [99., 55., 23., 44.]])
##计算每一行的均值
print(torch.mean(B,dim = 1,keepdim=True))
# tensor([[20.5000],
# [39.5000],
# [55.2500]])
##计算每一列的均值
print(torch.mean(B,dim = 0,keepdim=True))
# tensor([[59.3333, 40.3333, 25.6667, 28.3333]])
##计算每一行的和
print(torch.sum(B,dim=1,keepdim = True))
# tensor([[ 82.],
# [158.],
# [221.]])
print(torch.sum(B,dim=0,keepdim = True))
# tensor([[178., 121., 77., 85.]])
##按照行计算累加和
print(torch.cumsum(B,dim=1))
# tensor([[ 12., 46., 71., 82.],
# [ 67., 99., 128., 158.],
# [ 99., 154., 177., 221.]])
##按照列计算累加和
print(torch.cumsum(B,dim=0))
# tensor([[ 12., 34., 25., 11.],
# [ 79., 66., 54., 41.],
# [178., 121., 77., 85.]])
##计算每一行的中位数
print(torch.median(B,dim=1,keepdim = True))
# values=tensor([[12.],
# [30.],
# [44.]]),
# indices=tensor([[0],
# [3],
# [3]]))
##计算每一列的中位数
print(torch.median(B,dim=0,keepdim = True))
# torch.return_types.median(
# values=tensor([[67., 34., 25., 30.]]),
# indices=tensor([[1, 0, 0, 1]]))
##按照行计算乘积
print(torch.prod(B,dim=1,keepdim = True))
# tensor([[ 112200.],
# [1865280.],
# [5510340.]])
##按照列计算乘积
print(torch.prod(B,dim=0,keepdim = True))
# tensor([[79596., 59840., 16675., 14520.]])
##按照行计算累乘积
print(torch.cumprod(B,dim = 1))
# tensor([[1.2000e+01, 4.0800e+02, 1.0200e+04, 1.1220e+05],
# [6.7000e+01, 2.1440e+03, 6.2176e+04, 1.8653e+06],
# [9.9000e+01, 5.4450e+03, 1.2524e+05, 5.5103e+06]])
##按照列计算累乘积
print(torch.cumprod(B,dim = 0))
# tensor([[1.2000e+01, 3.4000e+01, 2.5000e+01, 1.1000e+01],
# [8.0400e+02, 1.0880e+03, 7.2500e+02, 3.3000e+02],
# [7.9596e+04, 5.9840e+04, 1.6675e+04, 1.4520e+04]])
##标准差
torch.std(A)
# tensor(25.0108)