• (PyTorch)PyTorch中的常见运算(*、@、Mul、Matmul)


    1. 矩阵与标量

    矩阵(张量)每一个元素与标量进行操作。

    1. import torch
    2. a = torch.tensor([1,2])
    3. print(a+1)
    4. >>> tensor([2, 3])

    2. 哈达玛积(Mul)

    两个相同尺寸的张量相乘,然后对应元素的相乘就是这个哈达玛积。

    1. a = torch.tensor([1,2])
    2. b = torch.tensor([2,3])
    3. print(a*b)
    4. print(torch.mul(a,b))
    5. >>> tensor([2, 6])
    6. >>> tensor([2, 6])

    这个torch.mul()和*以及torch.dot()是等价的

    当然,除法也是类似的:

    1. a = torch.tensor([1.,2.])
    2. b = torch.tensor([2.,3.])
    3. print(a/b)
    4. print(torch.div(a/b))
    5. >>> tensor([0.5000, 0.6667])
    6. >>> tensor([0.5000, 0.6667])

    我们可以发现的torch.div()其实就是/, 类似的:torch.add就是+,torch.sub()就是-,不过符号的运算更简单常用。

    3. 矩阵乘法

    在代码中矩阵相乘有三种写法:

    • torch.mm()
    • torch.matmul()
    • @
    1. a = torch.tensor([1.,2.])
    2. b = torch.tensor([2.,3.]).view(1,2)
    3. print(torch.mm(a, b))
    4. print(torch.matmul(a, b))
    5. print(a @ b)

    输出结果:

    1. tensor([[2., 3.],
    2. [4., 6.]])
    3. tensor([[2., 3.],
    4. [4., 6.]])
    5. tensor([[2., 3.],
    6. [4., 6.]])

    上面的是对二维矩阵而言的,假如参与运算的是一个多维张量,那么只有torch.matmul()可以使用

    torch.mv()等价于torch.mm(),不过不同的是mv适用与矩阵和向量相乘

    在多维张量中,参与矩阵运算的其实只有后两个维度,前面的维度其实就像是索引一样,举个例子:

    1. a = torch.rand((1,2,64,32))
    2. b = torch.rand((1,2,32,64))
    3. print(torch.matmul(a, b).shape)
    4. >>> torch.Size([1, 2, 64, 64])

    4. 幂与开方

    1. a = torch.tensor([1.,2.])
    2. b = torch.tensor([2.,3.])
    3. c1 = a ** b
    4. c2 = torch.pow(a, b)
    5. print(c1,c2)
    6. >>> tensor([1., 8.]) tensor([1., 8.])

    5. 对数运算

    pytorch中log是以e自然数为底数的,然后log2和log10才是以2和10为底数的运算。

    1. import numpy as np
    2. print('对数运算')
    3. a = torch.tensor([2,10,np.e])
    4. print(torch.log(a))
    5. print(torch.log2(a))
    6. print(torch.log10(a))
    7. >>> tensor([0.6931, 2.3026, 1.0000])
    8. >>> tensor([1.0000, 3.3219, 1.4427])
    9. >>> tensor([0.3010, 1.0000, 0.4343])

    6. 近似值运算

    • .ceil() 向上取整
    • .floor()向下取整
    • .trunc()取整数
    • .frac()取小数
    • .round()四舍五入
    1. a = torch.tensor(1.2345)
    2. print(a.ceil())
    3. >>>tensor(2.)
    4. print(a.floor())
    5. >>> tensor(1.)
    6. print(a.trunc())
    7. >>> tensor(1.)
    8. print(a.frac())
    9. >>> tensor(0.2345)
    10. print(a.round())
    11. >>> tensor(1.)

    7. 剪裁运算

    这个是让一个数,限制在你自己设置的一个范围内[min,max],小于min的话就被设置为min,大于max的话就被设置为max。这个操作在一些对抗生成网络中,好像是WGAN-GP,通过强行限制模型的参数的值。

    1. a = torch.rand(5)
    2. print(a)
    3. print(a.clamp(0.3,0.7))

    输出为:

    1. tensor([0.5271, 0.6924, 0.9919, 0.0095, 0.0340])
    2. tensor([0.5271, 0.6924, 0.7000, 0.3000, 0.3000])

  • 相关阅读:
    VisionMaster自定义模块
    RestTemplate:简化HTTP请求的强大工具
    HCIE-Security Day46:AC准入控制Dot1x
    数据结构之Map&Set
    k8s实践记录
    前端项目运行报错webpack: Failed to compile.解决
    简述直线模组的发展前景
    Esxi 8 更换Nvme硬盘后 如何迁移Esxi主机和虚拟机到新硬盘
    搭建Pytorch的GPU环境超详细
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
  • 原文地址:https://blog.csdn.net/qq_40728667/article/details/134013899