• Pytorch框架的学习(1)


    目录

    一、Pytorch的基本概念

     1、tensor(张量)概念

    2、tensor的用法

     2.1、tensor的类型

    2.2、tensor的创建

    2.3、tensor的属性

    2.4、tensor的运算


    (1条消息) Pytorch框架的学习(2)_An efforter的博客-CSDN博客 

    (1条消息) Pytorch框架的学习(3)_An efforter的博客-CSDN博客

    一、Pytorch的基本概念

    pytorch的三大基本的概念:tensor(张量),Variable(变量),nn.Module(网络结构)

     1、tensor(张量)概念

           从上图中,我们一点一点的引入,标量是零维的张量,向量是一维的张量,矩阵是二维的张量。从上图右边中就可以看出来。

    引入到真实操作中:

           先解释样本与张量关系:

           算法工程师经常提到一个名词:我的数据怎么怎么样,这个数据其实就是样本,tensor(张量)就是把样本的特征给描述出来。

    再深入一点,怎么描述?

             可以这么想,其实就是我们选择一张图片,彩色图片(RGB)含有三个通道,每一通道就是当成一矩阵,图片中含有特征的,那么在矩阵中就会对应第几行几列的数值。那么H*W*C(长*宽*通道)就是一个标量。

           解释模型与张量关系:

          模型包含两类:有参与无参模型。 对应着上图中Y=W*X+b函数来看,X就是样本,W与b在未知的情况下是变量。变量也是用tensor表示的。最后的到Y(标签)也是进行数字化的,所以和tensor也形成了直接联系。

    2、tensor的用法

     2.1、tensor的类型

    tensor的类型,跟c语言中学习的差不多,无非加了在torch下的类型。

    2.2、tensor的创建

     结合代码操作:

    1. import torch
    2. a=torch.Tensor([[1,2],[3,4]])
    3. print(a)
    4. print(a.type())
    5. b=torch.ones(2,2)
    6. print(b)
    7. print(b.type())
    8. c=torch.Tensor(2,3)
    9. c=torch.zeros_like(c) #把c的格式仿过来就变成两行三列都是0
    10. d=torch.ones_like(c) #把c的格式仿过来就变成两行三列都是1
    11. print(c)
    12. print(d)
    13. #随机
    14. e=torch.rand(2,2)
    15. print(e)
    16. #随机打乱0-9
    17. e=torch.randperm(10)
    18. print(e)
    19. #标准分布
    20. f=torch.normal(mean=0.0,std=torch.rand(5)) #std是标准差
    21. print(f)
    22. #取范围
    23. g= torch.Tensor( 2,2).uniform_( -1,1) #在-1,1之间
    24. print(g)
    25. #torch 中的序列
    26. h= torch.arange (0,10,1)
    27. print(h)
    28. I= torch.linspace( 2,10,3)
    29. print(I)

    对应的输出:

    2.3、tensor的属性

    •  每一个Tensor有torch.dtype、torch.device、torch.layout三种属性。
    • torch.device标识了torch.Tensor对象在创建之后所存储在的设备名称。
    • torch.layout表示torch.Tensor内存布局的对象。

    实例:torch.tensor ( [1,2,3], dtype=torch.float32,device=torch.device('cpu') )

    • torch.sparse_coo_tensor  (稀疏矩阵)
    • coo类型表示了非零元素的坐标形式 
    1. #稀疏的张量
    2. import torch
    3. dev=torch.device("cpu") #指定cpu
    4. a=torch.tensor([2,2],
    5. dtype=torch.float32, #tensor的三个参数
    6. device = dev)
    7. i= torch.tensor([[0,1,2],[0,1,2]])
    8. v = torch.tensor([3,4,5],dtype =torch.float32)
    9. x= torch.sparse_coo_tensor(i, v,[4,4],dtype=int,device="cuda").to_dense()
    10. print(x)

    2.4、tensor的运算

    以下图片代码都是一些使用方式:

        (1).加法运算

          (2).减法运算

      

         (3).乘法运算

    • 哈达玛积(element wise,对应元素相乘)

       (4).除法运算

    (5).矩阵运算

    •  二维矩阵乘法运算操作包括torch.mm).torch.matmul()、@

    • 对于高维的Tensor (dim>2),定义其矩阵乘法仅在最后的两个维度上,要求前面的维度必须保持一致,就像矩阵的索引一样并且运算操只有torch.matmul()。 

     (6).幂运算

                特殊的e^(a):

     (7).开方运算

    (8).对数运算    第三、四个方式底数是e

    (9).tensor的取整/取余运算

    • floor()向下取整数
    • ceil()向上取整数
    • round()四舍五入>=0.5向上取整,<0.5向下取整
    • trunc()裁剪,只取整数部分
    • frac()只取小数部分
    • %取余
    1. import torch
    2. a=torch.rand(2,2)
    3. a=a*10
    4. print(a)
    5. print(torch.floor(a))
    6. print(torch.ceil(a))
    7. print(torch.round(a))
    8. print(torch.trunc(a))
    9. print(torch.frac(a))
    10. print(a%2)

    测试结果: 

    (10).tensor的比较运算

    • torch.eq(input, other, out=None)#按成员进行等式操作,相同返回True
    • torch.equal(tensor1, tensor2)#如果tensor1和tensor2有相同的size和elements,则为true
    • torch.ge(input, other, out=None)# input>= other
    • torch.gt(input, other, out=None)# input>other
    • torch.le(input, other, out=None) # input=
    • torch.It(input, other, out=None) # input
    • torch.ne(input, other, out=None) # input != other 不等于
    1. import torch
    2. a=torch.rand(2,3)
    3. b=torch.rand(2,3)
    4. print(a)
    5. print(b)
    6. print(torch.eq(a,b))
    7. print(torch.equal(a,b))
    8. print(torch.ge(a,b))#大于等于
    9. print(torch.gt(a,b))#大于
    10. print(torch.lt(a,b))#小于
    11. print(torch.ne(a,b))#不等于

    (11).Tensor的取前k大/前k小/第k小的数值及其索引

    • torch.sort(input, dim=None, descending=False, out=None)#对目标input进行排序
    • torch.topk(input, k, dim=None, largest=True, sorted=True,out=None)#沿着指定维度返回最大k个数值及其索引值
    • torch.kthvalue(input, k, dim=None, out=None)#沿着指定维度返回第k个最小值及其索引值
    1. c=torch.tensor([[1,2,3,4,2],
    2. [2,3,4,12,3]])
    3. print(c.shape)
    4. print(torch.sort(c))
    5. print(torch.sort(c,dim=1,descending=True))
    6. #value就是值,indices就是索引值对应原来摆放的位置
    7. ##topk
    8. d=torch.tensor([[12,3,1,2,4],[4,2,6,4,5]])
    9. print(d.shape)
    10. print(torch.topk(d,k=1,dim=0)) #在0维中组合成一个最大数组[12,3,3,4,5]
    11. print(torch.topk(d,k=2,dim=1))#在一维中找到最大值两个【12,4】,【5,4】
    12. print(torch.kthvalue(d,k=2,dim=1)) #在一维中找第二小的【2,4】

    测试结果:

     (12).Tensor判定是否为finite/inf/nan  (有界/无界/张量中是否存在空值)

    •  torch.isfinite(tensor) / torch.isinf(tensor) / torch.isnan(tensor)
    • 返回一个标记元素是否为finite/inf/nan的mask 张量。
      1. ##有界无界是否为空
      2. a=torch.rand (2,3)
      3. print(a)
      4. print (torch.isfinite(a) )
      5. print(torch.isfinite(a/0))
      6. print (torch. isinf(a/0))
      7. print(torch.isnan (a))

      测试结果:

     (13).tensor的三角函数

     (14)tensor的其他的数学函数

  • 相关阅读:
    浅谈无线测温产品在马来西亚某配电项目的应用
    Flutter折腾学习成长记(25)
    Android问题解决
    1996-2023年各省农林牧渔总产值及农业、林业、牧业、渔业总产值数据(无缺失)
    Excel使用笔记
    PlatformIO 创建 libopencm3 + FreeRTOS 项目
    用译码器来设计组合逻辑电路
    QT环境下,easylogging++解析配置参数错误的解决记录
    JavaWeb开发之——SQL简介&通用语法及分类(05)
    复杂AB实验
  • 原文地址:https://blog.csdn.net/qq_40694323/article/details/126626368