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的创建

 结合代码操作:

import torch
a=torch.Tensor([[1,2],[3,4]])
print(a)
print(a.type())

b=torch.ones(2,2)
print(b)
print(b.type())

c=torch.Tensor(2,3)
c=torch.zeros_like(c)  #把c的格式仿过来就变成两行三列都是0
d=torch.ones_like(c)   #把c的格式仿过来就变成两行三列都是1
print(c)
print(d)

#随机
e=torch.rand(2,2)
print(e)

#随机打乱0-9
e=torch.randperm(10)
print(e)

#标准分布
f=torch.normal(mean=0.0,std=torch.rand(5))  #std是标准差
print(f)

#取范围
g= torch.Tensor( 2,2).uniform_( -1,1) #在-1,1之间
print(g)

#torch 中的序列
h= torch.arange (0,10,1)
print(h)

I= torch.linspace( 2,10,3)
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类型表示了非零元素的坐标形式 
#稀疏的张量
import torch
dev=torch.device("cpu")  #指定cpu
a=torch.tensor([2,2],
               dtype=torch.float32,  #tensor的三个参数
               device = dev)
i= torch.tensor([[0,1,2],[0,1,2]])
v = torch.tensor([3,4,5],dtype =torch.float32)
x= torch.sparse_coo_tensor(i, v,[4,4],dtype=int,device="cuda").to_dense()
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()只取小数部分
  • %取余
import torch
a=torch.rand(2,2)
a=a*10
print(a)
print(torch.floor(a))
print(torch.ceil(a))
print(torch.round(a))
print(torch.trunc(a))
print(torch.frac(a))
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= <other
  • torch.It(input, other, out=None) # input<other
  • torch.ne(input, other, out=None) # input != other 不等于
import torch
a=torch.rand(2,3)
b=torch.rand(2,3)
print(a)
print(b)
print(torch.eq(a,b))
print(torch.equal(a,b))
print(torch.ge(a,b))#大于等于
print(torch.gt(a,b))#大于
print(torch.lt(a,b))#小于
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个最小值及其索引值
c=torch.tensor([[1,2,3,4,2],
                [2,3,4,12,3]])
print(c.shape)
print(torch.sort(c))
print(torch.sort(c,dim=1,descending=True))
#value就是值,indices就是索引值对应原来摆放的位置

##topk
d=torch.tensor([[12,3,1,2,4],[4,2,6,4,5]])
print(d.shape)
print(torch.topk(d,k=1,dim=0)) #在0维中组合成一个最大数组[12,3,3,4,5]
print(torch.topk(d,k=2,dim=1))#在一维中找到最大值两个【12,4】,【5,4】
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 张量。
  • ##有界无界是否为空
    a=torch.rand (2,3)
    print(a)
    print (torch.isfinite(a) )
    print(torch.isfinite(a/0))
    print (torch. isinf(a/0))
    print(torch.isnan (a))

    测试结果:

 (13).tensor的三角函数

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

猜你喜欢

转载自blog.csdn.net/qq_40694323/article/details/126626368