Pytorch 笔记
torch.cat()
在给定维度上对输入的张量序列进行连接操作
- Args
- tensors (sequence of Tensors): any python sequence of tensors of the same type. 任意
Tensor类型的 python 序列,序列中的数据是任意相同shape的同类型的Tensor- Non-empty tensors provided must have the same shape, except in the cat dimension.
- dim (int, optional): the dimension over which the tensors are concatenated 维度不能超过输入数据任意一个张量的维度
- tensors (sequence of Tensors): any python sequence of tensors of the same type. 任意
- Keyword args: out (Tensor, optional): the output tensor.
torch.gather()
1 | |
沿着由 dim 指定的轴收集数值
dim = 0的情况,代表的是横向,按行取值length矩阵中的数的值代表的是行数,所在列代表的是列数
1 | |
1 | |
用矩阵的方式演示
length矩阵中的数的值代表的是行数,数的位置代表的列数,比如 length 矩阵中的第三行第三列(从 0 数起)的数 0,其值是 0,代表在 input 中所取的数是第 0 行,位置是第三列,则表示在 input 中所取的数是第三列,
dim = 1的情况length矩阵中的数的值代表的是列数,所在行代表的是行数
1 | |
1 | |
对应的取值矩阵是
unsqueeze()和 squeeze()
unsqueeze()函数
torch.unsqueeze(input, dim, out=None) 扩展维度,返回一个新的张量,对输入的既定位置插入维度1
返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个
1 | |
边界测试
A dim value within the range [-input.dim() - 1, input.dim() + 1)
1 | |
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got -3)
1 | |
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
为何取值范围要如此设计呢?
原因:方便操作
0(-2)- 行扩展
1(-1)- 列扩展
正向:我们在 0,1 位置上扩展
逆向:我们在 -2,-1 位置上扩展
维度扩展:1 维 ->2 维,2 维 ->3 维,…,n 维 ->n+1 维
维度降低:n 维 ->n-1 维,n-1 维 ->n-2 维,…,2 维 ->1 维以 1 维 ->2 维 为例,
从 正向 的角度思考:
torch.Size([4])
最初的tensor([1., 2., 3., 4.])是 1 维,我们想让它扩展成 2 维,那么,可以有两种扩展方式:一种是:扩展成 1 行 4 列 ,即 tensor([[1., 2., 3., 4.]])
针对第一种,扩展成 [1, 4] 的形式,那么,在 dim=0 的位置上添加 1另一种是:扩展成 4 行 1 列,即
tensor([[1.], [2.], [3.], [4.]])
针对第二种,扩展成[4, 1]的形式,那么,在dim=1的位置上添加 1从 逆向 的角度思考:
原则:一般情况下,-1是代表的是 最后一个元素
在上述的原则下
扩展成[1, 4]的形式,就变成了,在dim=-2的的位置上添加 1
扩展成[4, 1]的形式,就变成了,在dim=-1的的位置上添加 1
unsequeeze_和 unsqueeze 的区别
unsqueeze_ 和unsqueeze实现一样的功能,区别在于 unsqueeze_ 是in_place操作,即 unsqueeze不会对使用 unsqueeze 的tensor进行改变,想要获取 unsqueeze 后的值必须赋予个新值,unsqueeze_则会对自己改变
squeeze()函数
作用是降维,torch.squeeze(input, dim=None, out=None)将输入张量形状中的 1 去除并返回。 如果输入是形如 (A×1×B×1×C×1×D),那么输出形状就为:(A×B×C×D)。当给定dim 时,那么挤压操作只在给定维度上。例如,输入形状为:(A×1×B), squeeze(input, 0)将会保持张量不变,只有用squeeze(input, 1),形状会变成(A×B)
和 unsqueeze 一样,返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个
多维张量本质上就是一个变换,如果维度是 1 ,那么,1 仅仅起到扩充维度的作用,而没有其他用途,因而,在进行降维操作时,为了加快计算,是可以去掉这些 1 的维度
1 | |