基于PyTorch深度学习实战入门系列-张量计算

Torch的使用-张量计算

比较函数
函数功能
allclose()比较两个元素是否接近
eq()逐元素比较是否相等
equal()判断两个张量是否具有相同的形状和元素
ge()逐元素比较是否大于等于
gt()逐元素比较是否大于
le()逐元素比较是否小于等于
lt()逐元素比较是否大于
ne()逐元素比较不等于
isnan()判断是否为缺失值
比较两个元素是否接近

input(Tensor) -第一个要比较的张量

other(Tensor) -要比较的第二张量

atol(float,可选的) -绝对宽容。默认值:1e-08

rtol(float,可选的) -相对公差。默认值:1e-05

equal_nan(bool,可选的) -如果 True ,则两个 NaN 将被视为相等。默认值:False

此函数检查是否所有 input 和 other 都满足条件:

∣ i n p u t − o t h e r ∣ ≤ a t o l + r t o l ∗ ∣ o t h e r ∣ |input - other| \le atol + rtol * |other| inputotheratol+rtolother

逐元素比较是否相等
# 逐元素比较是否相等
tensor1 = torch.Tensor([i for i in range(10)])
tensor2 = torch.arange(0, 10)
tensor3 = torch.unsqueeze(tensor2, dim=0)
print(tensor1)
print(tensor2)
print(tensor3)
# 逐元素比较是否相等
print(torch.eq(tensor1, tensor2))
print(torch.eq(tensor1, tensor3))

输出:

tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
tensor([True, True, True, True, True, True, True, True, True, True])
tensor([[True, True, True, True, True, True, True, True, True, True]])
判断两个张量是否具有相同的形状和元素:
tensor1 = torch.Tensor([i for i in range(10)])
tensor2 = torch.arange(0, 10)
tensor3 = torch.unsqueeze(tensor2, dim=0)
print(tensor1)
print(tensor2)
print(tensor3)
# 判断两个张量是否具有相同的形状和元素
print(torch.equal(tensor1, tensor2))
print(torch.equal(tensor1, tensor3))

输出:

tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
True
False
逐元素比较:
# 逐元素比较大小
tensor1 = torch.Tensor([i for i in range(10)])
tensor2 = torch.randperm(10)
print(tensor1)
print(tensor2)
# 是否大于等于
print(torch.ge(tensor1, tensor2))
# 是否大于
print(torch.gt(tensor1, tensor2))
# 是否小于等于
print(torch.le(tensor1, tensor2))
# 是否小于
print(torch.lt(tensor1, tensor2))
# 逐元素比较不等于
print(torch.ne(tensor1, tensor2))
# 判断是否为缺失值
print(torch.isnan(tensor1))

输出:

tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([4, 6, 2, 9, 8, 0, 5, 3, 7, 1])
tensor([False, False,  True, False, False,  True,  True,  True,  True,  True])
tensor([False, False, False, False, False,  True,  True,  True,  True,  True])
tensor([ True,  True,  True,  True,  True, False, False, False, False, False])
tensor([ True,  True, False,  True,  True, False, False, False, False, False])
tensor([ True,  True, False,  True,  True,  True,  True,  True,  True,  True])
tensor([False, False, False, False, False, False, False, False, False, False])
逐元素加减乘除:
# 逐元素加减乘除
tensor1 = torch.Tensor([i for i in range(1, 11)])
tensor2 = torch.randperm(10) + 1
print(tensor1)
print(tensor2)
# +
print(tensor1 + tensor2)
# -
print(tensor1 - tensor2)
# *
print(tensor1 * tensor2)
# /
print(tensor1 / tensor2)
# //
print(tensor1 // tensor2)

输出:

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
tensor([ 1,  2,  9,  5,  7,  6, 10,  8,  4,  3])
tensor([ 2.,  4., 12.,  9., 12., 12., 17., 16., 13., 13.])
tensor([ 0.,  0., -6., -1., -2.,  0., -3.,  0.,  5.,  7.])
tensor([ 1.,  4., 27., 20., 35., 36., 70., 64., 36., 30.])
tensor([1.0000, 1.0000, 0.3333, 0.8000, 0.7143, 1.0000, 0.7000, 1.0000, 2.2500,
        3.3333])
tensor([1., 1., 0., 0., 0., 1., 0., 1., 2., 3.])
张量的幂:
# 张量的幂
tensor1 = torch.Tensor([i for i in range(10)])
print(torch.pow(tensor1, 2))
print(tensor1 ** 2)

输出:

tensor([ 0.,  1.,  4.,  9., 16., 25., 36., 49., 64., 81.])
tensor([ 0.,  1.,  4.,  9., 16., 25., 36., 49., 64., 81.])
指数、对数、平方根、平方根倒数:
# 张量的指数、对数、平方根、平方根倒数
tensor1 = torch.Tensor([i for i in range(10)])
print(tensor1)
# 指数
print(torch.exp(tensor1))
# 对数
print(torch.log(tensor1))
# 平方根
print(torch.sqrt(tensor1))
# 平方根倒数
print(torch.rsqrt(tensor1))

输出:

tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([1.0000e+00, 2.7183e+00, 7.3891e+00, 2.0086e+01, 5.4598e+01, 1.4841e+02,
        4.0343e+02, 1.0966e+03, 2.9810e+03, 8.1031e+03])
tensor([  -inf, 0.0000, 0.6931, 1.0986, 1.3863, 1.6094, 1.7918, 1.9459, 2.0794,
        2.1972])
tensor([0.0000, 1.0000, 1.4142, 1.7321, 2.0000, 2.2361, 2.4495, 2.6458, 2.8284,
        3.0000])
tensor([   inf, 1.0000, 0.7071, 0.5774, 0.5000, 0.4472, 0.4082, 0.3780, 0.3536,
        0.3333])
张量的剪裁:
# 张量的剪裁
tensor1 = torch.Tensor([i for i in range(10)]).reshape(2, 5)
print(tensor1)
# 根据最大值剪裁
print(torch.clamp_max(tensor1, 6))
# 根据最小值剪裁
print(torch.clamp_min(tensor1, 3))
# 根据范围剪裁
print(torch.clamp(tensor1, 2, 8))

输出:

tensor([[0., 1., 2., 3., 4.],
        [5., 6., 7., 8., 9.]])
tensor([[0., 1., 2., 3., 4.],
        [5., 6., 6., 6., 6.]])
tensor([[3., 3., 3., 3., 4.],
        [5., 6., 7., 8., 9.]])
tensor([[2., 2., 2., 3., 4.],
        [5., 6., 7., 8., 8.]])
矩阵操作:
# 矩阵操作
tensor1 = torch.rand(4, 4) * 10
tensor2 = torch.randperm(8).reshape(4, 2).float()
print(tensor1)
print(tensor2)
# 转置
print(torch.t(tensor1))
# 逆
print(torch.inverse(tensor1))
# 矩阵乘法
print(tensor1.matmul(tensor2))
# 矩阵的迹
print(torch.trace(torch.arange(16).reshape(4, 4)))

输出:

tensor([[0.5914, 5.7943, 3.5240, 0.5632],
        [5.6683, 3.1371, 4.2062, 5.3590],
        [9.8913, 8.4539, 9.0679, 0.7691],
        [3.1266, 7.2945, 4.9690, 2.4820]])
tensor([[5., 7.],
        [0., 3.],
        [2., 6.],
        [4., 1.]])
tensor([[0.5914, 5.6683, 9.8913, 3.1266],
        [5.7943, 3.1371, 8.4539, 7.2945],
        [3.5240, 4.2062, 9.0679, 4.9690],
        [0.5632, 5.3590, 0.7691, 2.4820]])
tensor([[-1.0311, -0.3084,  0.0610,  0.8810],
        [-0.9460, -0.4209, -0.0636,  1.1432],
        [ 2.0004,  0.7119,  0.1128, -2.0258],
        [ 0.0743,  0.2005, -0.1159, -0.0110]])
tensor([[ 12.2578,  43.2300],
        [ 58.1902,  79.6860],
        [ 70.6687, 149.7774],
        [ 35.4989,  76.0656]])
tensor(30)
统计计算:
# 统计计算
tensor1 = torch.randperm(10)
print(tensor1)
# 最大值
print(f"最大值:{tensor1.max()}")
print(f"最大值:{torch.max(tensor1)}")
# 最小值
print(f"最小值:{tensor1.min()}")
print(f"最小值:{torch.min(tensor1)}")
# 最大值的下标
print(f"最大值下标:{tensor1.argmax()}")
print(f"最大值下标:{torch.argmax(tensor1)}")
# 最小值的下标
print(f"最小值下标:{tensor1.argmin()}")
print(f"最小值下标:{torch.argmin(tensor1)}")

输出:

tensor([2, 6, 7, 4, 1, 9, 5, 0, 8, 3])
最大值:9
最大值:9
最小值:0
最小值:0
最大值下标:5
最大值下标:5
最小值下标:7
最小值下标:7
二维张量:
# 二维张量统计计算
tensor1 = torch.randperm(16).reshape(4, 4)
print(tensor1)
# 最大值 行
print(tensor1.max(dim=1))
print(torch.max(tensor1, dim=1))
# 最大值索引
print(tensor1.argmax(dim=1))
print(torch.argmax(tensor1, dim=1))
# 最大值 列
print(tensor1.max(dim=0))
print(torch.min(tensor1, dim=0))
# 最小值索引
print(tensor1.argmin(dim=0))
print(torch.argmin(tensor1, dim=0))

输出:

tensor([[ 2, 13,  7, 14],
        [11,  0, 15,  3],
        [ 5,  6,  4,  1],
        [10,  9,  8, 12]])
torch.return_types.max(
values=tensor([14, 15,  6, 12]),
indices=tensor([3, 2, 1, 3]))
torch.return_types.max(
values=tensor([14, 15,  6, 12]),
indices=tensor([3, 2, 1, 3]))
tensor([3, 2, 1, 3])
tensor([3, 2, 1, 3])
torch.return_types.max(
values=tensor([11, 13, 15, 14]),
indices=tensor([1, 0, 1, 0]))
torch.return_types.min(
values=tensor([2, 0, 4, 1]),
indices=tensor([0, 1, 2, 2]))
tensor([0, 1, 2, 2])
tensor([0, 1, 2, 2])
排序:
print(torch.argmin(tensor1, dim=0))
# 排序
tensor1 = torch.randperm(10)
print(tensor1)
# 升序
print(torch.sort(tensor1, descending=False))
# 降序
print(torch.sort(tensor1, descending=True))

输出:

tensor([4, 0, 8, 7, 2, 5, 3, 6, 9, 1])
torch.return_types.sort(
values=tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
indices=tensor([1, 9, 4, 6, 0, 5, 7, 3, 2, 8]))
torch.return_types.sort(
values=tensor([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]),
indices=tensor([8, 2, 3, 7, 5, 0, 6, 4, 9, 1]))
二维张量排序:
# 二维张量排序
tensor1 = torch.randperm(16).reshape(4, 4)
print(tensor1)
print(torch.sort(tensor1))

输出:

tensor([[ 7,  2,  5, 13],
        [12,  4,  1, 10],
        [ 9,  3, 11, 14],
        [15,  0,  6,  8]])
torch.return_types.sort(
values=tensor([[ 2,  5,  7, 13],
        [ 1,  4, 10, 12],
        [ 3,  9, 11, 14],
        [ 0,  6,  8, 15]]),
indices=tensor([[1, 2, 0, 3],
        [2, 1, 3, 0],
        [1, 0, 2, 3],
        [1, 2, 3, 0]]))
平均值:
# 平均值
tensor1 = torch.randperm(16).reshape(4, 4).to(dtype=torch.float64)
print(tensor1)
# 按行求平均值
print(torch.mean(tensor1, dim=1, keepdim=True))
# 按列求平均值
print(torch.mean(tensor1, dim=0, keepdim=True))

输出:

tensor([[ 6.,  8.,  9.,  5.],
        [ 0., 11.,  1.,  3.],
        [15.,  4.,  7., 10.],
        [ 2., 12., 13., 14.]], dtype=torch.float64)
tensor([[ 7.0000],
        [ 3.7500],
        [ 9.0000],
        [10.2500]], dtype=torch.float64)
tensor([[5.7500, 8.7500, 7.5000, 8.0000]], dtype=torch.float64)
求和:
# 求和
tensor1 = torch.randperm(16).reshape(4, 4).to(dtype=torch.float64)
print(tensor1)
# 按行求和
print(torch.sum(tensor1, dim=1, keepdim=True))
# 按列求和
print(torch.sum(tensor1, dim=0, keepdim=True))

输出:

tensor([[ 9.,  1.,  7.,  2.],
        [10.,  3., 11.,  8.],
        [ 0.,  5., 12., 13.],
        [ 4.,  6., 15., 14.]], dtype=torch.float64)
tensor([[19.],
        [32.],
        [30.],
        [39.]], dtype=torch.float64)
tensor([[23., 15., 45., 37.]], dtype=torch.float64)
累加和:
# 累加和
tensor1 = torch.randperm(16).reshape(4, 4)
print(tensor1)
# 按行求累加和
print(torch.cumsum(tensor1, dim=1))
# 按列求累加和
print(torch.cumsum(tensor1, dim=0))

输出:

tensor([[14, 13,  7,  3],
        [ 2,  4,  1, 15],
        [ 0, 12,  8,  5],
        [10, 11,  9,  6]])
tensor([[14, 27, 34, 37],
        [ 2,  6,  7, 22],
        [ 0, 12, 20, 25],
        [10, 21, 30, 36]])
tensor([[14, 13,  7,  3],
        [16, 17,  8, 18],
        [16, 29, 16, 23],
        [26, 40, 25, 29]])
中位数:
# 中位数
tensor1 = torch.randperm(16).reshape(4, 4)
print(tensor1)
# 按行求中位数
print(torch.median(tensor1, dim=1))
# 按列求中位数
print(torch.median(tensor1, dim=0))

输出:

tensor([[ 0,  3,  5,  2],
        [ 7, 15, 14, 11],
        [10,  8,  9,  1],
        [13, 12,  4,  6]])
torch.return_types.median(
values=tensor([ 2, 11,  8,  6]),
indices=tensor([3, 3, 1, 3]))
torch.return_types.median(
values=tensor([7, 8, 5, 2]),
indices=tensor([1, 2, 0, 0]))
标准差:
# 标准差
tensor1 = torch.randperm(16).reshape(4, 4).to(dtype=torch.float64)
print(tensor1)
# 按行求标准差
print(torch.std(tensor1, dim=1))
# 按列求标准差
print(torch.std(tensor1, dim=0))

输出:

tensor([[ 3., 10.,  2., 12.],
        [ 6., 14.,  4.,  5.],
        [ 7.,  8., 15.,  0.],
        [ 9., 13.,  1., 11.]], dtype=torch.float64)
tensor([4.9917, 4.5735, 6.1373, 5.2599], dtype=torch.float64)
tensor([2.5000, 2.7538, 6.4550, 5.5976], dtype=torch.float64)
乘积:
# 乘积
tensor1 = torch.randperm(16).reshape(4, 4).to(dtype=torch.float64)
print(tensor1)
# 按行乘积
print(torch.prod(tensor1, dim=1, keepdim=True))
# 按列乘积
print(torch.prod(tensor1, dim=0, keepdim=True))

输出:

tensor([[ 6., 11.,  8.,  4.],
        [14.,  3.,  9., 15.],
        [ 2.,  7.,  0., 12.],
        [13.,  1.,  5., 10.]], dtype=torch.float64)
tensor([[2112.],
        [5670.],
        [   0.],
        [ 650.]], dtype=torch.float64)
tensor([[2184.,  231.,    0., 7200.]], dtype=torch.float64)
累乘:
# 累乘
tensor1 = torch.randperm(16).reshape(4, 4)
print(tensor1)
# 按行求累乘
print(torch.cumprod(tensor1, dim=1))
# 按列求累乘
print(torch.cumprod(tensor1, dim=0))

输出:

tensor([[15,  6,  0,  8],
        [10, 11, 13,  9],
        [ 2,  3, 12,  1],
        [14,  5,  4,  7]])
tensor([[   15,    90,     0,     0],
        [   10,   110,  1430, 12870],
        [    2,     6,    72,    72],
        [   14,    70,   280,  1960]])
tensor([[  15,    6,    0,    8],
        [ 150,   66,    0,   72],
        [ 300,  198,    0,   72],
        [4200,  990,    0,  504]])
  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇的布欧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值