Torch

pytorch深度学习框架最重要的包torch

相关介绍

​torch​​ 官方文档说明如下:

The torch package contains data structures for multi-dimensional tensors and defines mathematical operations over these tensors. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful utilities.

句句不离​​tensor​​​,可知​​torch​​是包含了多维张量的数据结构以及对张量的多种数学操作,还提供了更加有效地对张量和任意类型进行序列化的工具。

基本操作

简单介绍几个目前自己看项目看到的。之后会补充

torch.rand

torch.rand(*size, out=None)  -> Tensor

返回一个张量,包含从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数​​size(int...)​​决定。

  • 例子
>>> torch.rand(2, 3)
tensor([[0.1183, 0.5954, 0.0256],
[0.1915, 0.2350, 0.4634]])

>>> torch.rand(4)
tensor([0.4275, 0.2355, 0.1684, 0.3253])
  • 与​​torch.rand​​​相似的是​​torch.randn​​,不同之处是张量包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取一组随机数

torch.zeros

torch.zeros(*sizes, out=None) → Tensor

返回一个全为标量0的张量,形状由​​sizes(int...)​​定义

  • 例子
>>> torch.zeros(3, 4)
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

torch.gather

torch.gather(input, dim, index, out=None) → Tensor

沿给定轴​​dim​​,将输入索引张量​index​​指定位置的值进行聚合

对于一个3维张量,输出可以定义为:

out[i][j][k] = tensor[index[i][j][k]][j][k]  # dim=0
out[i][j][k] = tensor[i][index[i][j][k]][k] # dim=1
out[i][j][k] = tensor[i][j][index[i][j][k]] # dim=3
  • 例子
>>> torch.gather(t, 1, torch.LongTensor([[0, 0],[1, 0]]))
tensor([[1., 1.],
[4., 3.]])
  • 过程图示(刚开始接触,确实很难理解。。。。)

torch.view

表示将原矩阵转化成​​i​​​行​​j​​​列的形式,​​i​​​为-1表示不限制行数,​​j​​为-1表示不限制列数

  • 例子
>>> tensor_a = torch.rand(4, 6)
>>> tensor_a
tensor([[0.5875, 0.8063, 0.4548, 0.3643, 0.6462, 0.7424],
[0.1108, 0.3584, 0.8895, 0.9736, 0.2135, 0.5241],
[0.9337, 0.0877, 0.5791, 0.1336, 0.6861, 0.2267],
[0.2795, 0.0140, 0.2651, 0.0198, 0.4842, 0.6934]])
>>> tensor_a.view(3, 8)
tensor([[0.5875, 0.8063, 0.4548, 0.3643, 0.6462, 0.7424, 0.1108, 0.3584],
[0.8895, 0.9736, 0.2135, 0.5241, 0.9337, 0.0877, 0.5791, 0.1336],
[0.6861, 0.2267, 0.2795, 0.0140, 0.2651, 0.0198, 0.4842, 0.6934]])
>>> tensor_a.view(-1, 2)
tensor([[0.5875, 0.8063],
[0.4548, 0.3643],
[0.6462, 0.7424],
[0.1108, 0.3584],
[0.8895, 0.9736],
[0.2135, 0.5241],
[0.9337, 0.0877],
[0.5791, 0.1336],
[0.6861, 0.2267],
[0.2795, 0.0140],
[0.2651, 0.0198],
[0.4842, 0.6934]])

torch.squeeze

torch.squeeze(input, dim=None, out=None)

将输入张量形状中的​​1​​​去除并返回。
如果输入的形如(A×1×B×1×C×1×D),那么输出形状就为(A×B×C×D)
当给定维度​​​dim​​​时,挤压操作只在给定的维度上。
如果输入形状为(A×1×B),​​​dim = 1​​​ 那么输出形状为(A×B)
一句话来说,如果不给定dim,则把input的所有size为1的维度给移除;如果给定dim,则只移除给定的且size为1的维度。

  • 例子
>>> x = torch.zeros(2,1,2,3,1)
>>> x.size()
torch.Size([2, 1, 2, 3, 1])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 3, 1])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 3, 1])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 3])

torch.unsqueeze

torch.unsqueeze(input, dim, out=None)

返回一个新张量,对输入的指定位置插入维度1
如果​​​dim​​​为负,则将会被转化为 ​​dim + input.dim() + 1​

>>> x = torch.Tensor([1, 2, 3, 4])
>>> x
tensor([1., 2., 3., 4.])
>>> x.size()
torch.Size([4])
>>> torch.unsqueeze(x, 0)
tensor([[1., 2., 3., 4.]])
>>> torch.unsqueeze(x, 1)
tensor([[1.],
[2.],
[3.],
[4.]])
>>> torch.unsqueeze(x, -2)
tensor([[1., 2., 3., 4.]])

torch.max/min

1、torch.max(input)-> tensor
2、torch.max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor)

1、返回输入tensor中所有元素的最大值

  • 例子
>>> a = torch.randn(1, 2)
>>> a
tensor([[ 0.6570, -1.8106]])
>>> torch.max(a)
tensor(0.6570)

2、按维度dim返回最大值,并且返回最大值的索引
返回的最大值和索引各是一个tensor,一起构成元组(Tensor,LongTensor)

>>> x = torch.rand(6, 2, 3)
>>> x
>tensor([[[0.4680, 0.2406, 0.6683],
[0.0046, 0.9367, 0.3410]],

[[0.5749, 0.1618, 0.0724],
[0.6462, 0.0960, 0.9497]],

[[0.4947, 0.7732, 0.4304],
[0.3440, 0.8931, 0.8361]],

[[0.4406, 0.5109, 0.2188],
[0.9521, 0.9525, 0.9238]],

[[0.7880, 0.8871, 0.2000],
[0.7416, 0.2076, 0.1980]],

[[0.4741, 0.6272, 0.2098],
[0.6330, 0.7999, 0.0243]]])
>>> torch.max(x,0, keepdim = True)
torch.return_types.max(values=tensor([[[0.7880, 0.8871, 0.6683],
[0.9521, 0.9525, 0.9497]]]), indices=tensor([[[4, 4, 0],
[3, 3, 1]]]))
>>> torch.max(x,1, keepdim = True)
torch.return_types.max(values=tensor([[[0.4680, 0.9367, 0.6683]],

[[0.6462, 0.1618, 0.9497]],

[[0.4947, 0.8931, 0.8361]],

[[0.9521, 0.9525, 0.9238]],

[[0.7880, 0.8871, 0.2000]],

[[0.6330, 0.7999, 0.2098]]]), indices=tensor([[[0, 1, 0]],

[[1, 0, 1]],

[[0, 1, 1]],

[[1, 1, 1]],

[[0, 0, 0]],

[[1, 1, 0]]]))
>>> torch.max(x,2, keepdim = True)

torch.return_types.max(values=tensor([[[0.6683],
[0.9367]],

[[0.5749],
[0.9497]],

[[0.7732],
[0.8931]],

[[0.5109],
[0.9525]],

[[0.8871],
[0.7416]],

[[0.6272],
[0.7999]]]), indices=tensor([[[2],
[1]],

[[0],
[2]],

[[1],
[1]],

[[1],
[1]],

[[1],
[0]],

[[1],
[1]]]))
>>> torch.max(x,2, keepdim = True)[1]
tensor([[[2],
[1]],

[[0],
[2]],

[[1],
[1]],

[[1],
[1]],

[[1],
[0]],

[[1],
[1]]])

。。。。。