pytorch 模块

pytorch的中文手册:https://github.com/zergtant/pytorch-handbook

一、定义/初始化张量Define tensors

tensor,即“张量”。实际上跟numpy数组、向量、矩阵的格式基本一样。但是是专门针对GPU来设计的,可以运行在GPU上来加快计算效率。

PyTorch中定义tensor,就跟numpy定义矩阵、向量差不多,例如定义一个5×3的tensor,每一项都是0的张量:
x = torch.zeros(5,3)

如果想查看某个tensor的形状的话,使用:
z.size(),或者z.shape,但是前者更常用。

下面列举一些常用的定义tensor的方法:

常数初始化:

  • torch.empty(size)返回形状为size的空tensor
  • torch.zeros(size)全部是0的tensor
  • torch.zeros_like(input)返回跟input的tensor一个size的全零tensor
  • torch.ones(size)全部是1的tensor
  • torch.ones_like(input)返回跟input的tensor一个size的全一tensor
  • torch.arange(start=0, end, step=1)返回一个从start到end的序列,可以只输入一个end参数,就跟python的range()一样了。实际上PyTorch也有range(),但是这个要被废掉了,替换成arange了
  • torch.full(size, fill_value)这个有时候比较方便,把fill_value这个数字变成size形状的张量

随机抽样(随机初始化):

  • torch.rand(size) [0,1)内的均匀分布随机数
  • torch.rand_like(input)返回跟input的tensor一样size的0-1随机数
  • torch.randn(size)返回标准正太分布N(0,1)的随机数
  • torch.normal(mean, std, out=None)正态分布。这里注意,mean和std都是tensor,返回的形状由mean和std的形状决定,一般要求两者形状一样。如果,mean缺失,则默认为均值0,如果std缺失,则默认标准差为1.

更多的随机抽样方法,参见链接:
https://pytorch.org/docs/stable/torch.html#random-sampling


二、基本操作、运算 Basic operations

1.tensor的切片、合并、变形、抽取操作

(Indexing, Slicing, Joining, Mutating)

这里我就简单总结一些重要的tensor基本操作:

  • torch.cat(seq, dim=0, out=None),把一堆tensor丢进去,按照dim指定的维度拼接、堆叠在一起.
    比如:
In [70]: x = torch.tensor([[1,2,3]])
In [71]: x
Out[71]: tensor([[1, 2, 3]])

 #按第0维度堆叠,对于矩阵,相当于“竖着”堆
In [72]: print(torch.cat((x,x,x),0))
tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])

 #按第1维度堆叠,对于矩阵,相当于“横着”拼
In [73]: print(torch.cat((x,x,x),1)) 
tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3]])
  • torch.chunk(tensor, chunks, dim=0)把tensor切成块,数量由chunks指定。
    例如:
In [74]: a = torch.arange(10)
In [75]: a
Out[75]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [76]: torch.chunk(a,4)
Out[76]: (tensor([0, 1, 2]), tensor([3, 4, 5]), tensor([6, 7, 8]), tensor([9]))
  • 切块还有torch.split(tensor, split_size_or_sections, dim=0)具体区别大家自行查阅文档
  • 按index选择:torch.index_select(input, dim, index, out=None)
  • 按mask选择:torch.masked_select(input, mask, out=None)
  • 经常会使用的“压扁”函数:torch.squeeze(input),压缩成1维。注意,压缩后的tensor和原来的tensor共享地址
  • 改变形状:torch.reshape(input, shape)以及tensor.view(shape).前者是把tensor作为函数的输入,后者是任何tensor的函数。实际上,二者的返回值,都只是让我们从另一种视角看某个tensor,所以不会改变本来的形状,除非你把结果又赋值给原来的tensor。下面给一个例子对比二者的用法:
In [82]: a
Out[82]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# 单纯的调用view函数:
In [83]: a.view(2,5)
Out[83]:
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])

# a的形状并不会变化
In [84]: print(a) 
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# 试试reshape函数:
In [86]: torch.reshape(a,[5,2])
Out[86]:
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])

# a的形状依然不会变化:
In [87]: a
Out[87]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

要想让a的形状变化,比如把结果赋值给a,比如a = a.view(2,5)

还有好多有意思的操作,自己去发掘吧:
https://pytorch.org/docs/stable/torch.html#indexing-slicing-joining-mutating-ops

2.基本数学操作

  • 加法直接加:x+y
    或者用torch.add(x,y).
    实际上,.add()可以接受三个参数:torch.add(input, value, out=None)
    out怎么用呢?一般,如果直接torch.add(x,y),那么x,y本身都不会变化的。但是如果设置out=x,那么x就变变成加和后的值。

特别的,若想进行in-place操作,就比方说y加上x,y的值就改变了,就可以用y.add_(x)这样y就直接被改变了。Torch里面所有带"_"的操作,都是in-place的。例如x.copy_(y)

  • 乘法:torch.mul(input, other, out=None)用input乘以other
  • 除法:torch.div(input, other, out=None)用input除以other
  • 指数:torch.pow(input, exponent, out=None)
  • 开根号:torch.sqrt(input, out=None)
  • 四舍五入到整数:torch.round(input, out=None)
  • argmax函数torch.argmax(input, dim=None, keepdim=False)返回指定维度最大值的序号,dim给定的定义是:the demention to reduce.也就是把dim这个维度的,变成这个维度的最大值的index。例如:
     
    argmax
  • sigmoid函数:torch.sigmoid(input, out=None)
  • tanh函数:torch.tanh(input, out=None)
  • torch.abs(input, out=None)取绝对值
  • torch.ceil(input, out=None)向上取整,等于向下取整+1
  • torch.clamp(input, min, max, out=None)刀削函数,把输入数据规范在min-max区间,超过范围的用min、max代替

太多了,基本上,numpy里面有的数学函数这里都有,能想到的的基本都有。所以更详细的内容,还是去查看文档吧哈哈:
https://pytorch.org/docs/stable/torch.html#math-operations

三、Torch Tensor与Numpy的互相转换

  • Tensor-->Numpy
    直接用.numpy()即可。但是注意,转换后,numpy的变量和原来的tensor会共用底层内存地址,所以如果原来的tensor改变了,numpy变量也会随之改变。参见下面的例子:
In [11]: a = torch.ones(2,4)
In [12]: a
Out[12]:
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.]])

In [13]: b = a.numpy()
In [14]: b
Out[14]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)

In [15]: a.add_(1)
Out[15]:
tensor([[2., 2., 2., 2.],
        [2., 2., 2., 2.]])

In [16]: b
Out[16]:
array([[2., 2., 2., 2.],
       [2., 2., 2., 2.]], dtype=float32)
  • Numpy-->Tensor
    torch.from_numpy()来转换。参见下面例子:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

输出:

[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

同样,两者会共用内存地址。



作者:Stack_empty
链接:https://www.jianshu.com/p/7dbfc7076e5a
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/Ian-learning/p/11729766.html