PyTorch中Tensor的拼接与拆分的实现

拼接张量:torch.cat() 、torch.stack()

  1. torch.cat(inputs, dimension=0) → Tensor

在给定维度上对输入的张量序列 seq 进行连接操作

举个例子:

>>> import torch
>>> x = torch.randn(2, 3)
>>> x
tensor([[-0.1997, -0.6900, 0.7039],
    [ 0.0268, -1.0140, -2.9764]])
>>> torch.cat((x, x, x), 0) # 在 0 维(纵向)进行拼接
tensor([[-0.1997, -0.6900, 0.7039],
    [ 0.0268, -1.0140, -2.9764],
    [-0.1997, -0.6900, 0.7039],
    [ 0.0268, -1.0140, -2.9764],
    [-0.1997, -0.6900, 0.7039],
    [ 0.0268, -1.0140, -2.9764]])
>>> torch.cat((x, x, x), 1) # 在 1 维(横向)进行拼接
tensor([[-0.1997, -0.6900, 0.7039, -0.1997, -0.6900, 0.7039, -0.1997, -0.6900,
     0.7039],
    [ 0.0268, -1.0140, -2.9764, 0.0268, -1.0140, -2.9764, 0.0268, -1.0140,
     -2.9764]])
>>> y1 = torch.randn(5, 3, 6)
>>> y2 = torch.randn(5, 3, 6)
>>> torch.cat([y1, y2], 2).size()
torch.Size([5, 3, 12])
>>> torch.cat([y1, y2], 1).size()
torch.Size([5, 6, 6])

对于需要拼接的张量,维度数量必须相同,进行拼接的维度的尺寸可以不同,但是其它维度的尺寸必须相同。

  • torch.stack(sequence, dim=0)

沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状

举个例子:

>>> x1 = torch.randn(2, 3)
>>> x2 = torch.randn(2, 3)
>>> torch.stack((x1, x2), 0).size() # 在 0 维插入一个维度,进行区分拼接
torch.Size([2, 2, 3])
>>> torch.stack((x1, x2), 1).size() # 在 1 维插入一个维度,进行组合拼接
torch.Size([2, 2, 3])
>>> torch.stack((x1, x2), 2).size()
torch.Size([2, 3, 2])
>>> torch.stack((x1, x2), 0)
tensor([[[-0.3499, -0.6124, 1.4332],
     [ 0.1516, -1.5439, -0.1758]],

    [[-0.4678, -1.1430, -0.5279],
     [-0.4917, -0.6504, 2.2512]]])
>>> torch.stack((x1, x2), 1)
tensor([[[-0.3499, -0.6124, 1.4332],
     [-0.4678, -1.1430, -0.5279]],

    [[ 0.1516, -1.5439, -0.1758],
     [-0.4917, -0.6504, 2.2512]]])
>>> torch.stack((x1, x2), 2)
tensor([[[-0.3499, -0.4678],
     [-0.6124, -1.1430],
     [ 1.4332, -0.5279]],

    [[ 0.1516, -0.4917],
     [-1.5439, -0.6504],
     [-0.1758, 2.2512]]])

把相同形状的张量合并,并根据提供的维度序列在相应位置插入维度,方法会根据位置来排列数据。代码中,根据第 0 维和第 1 维来进行合并时,虽然合并后的张量维度和尺寸相等,但是数据的位置并不是相同的。

拆分张量:torch.split()、torch.chunk()

  • torch.split(tensor, split_size, dim=0)

将输入张量分割成相等形状的 chunks(如果可分)。 如果沿指定维的张量形状大小不能被 split_size 整分, 则最后一个分块会小于其它分块。

举个例子:

>>> x = torch.randn(3, 10, 6)
>>> a, b, c = x.split(1, 0) # 在 0 维进行间隔维 1 的拆分
>>> a.size(), b.size(), c.size()
(torch.Size([1, 10, 6]), torch.Size([1, 10, 6]), torch.Size([1, 10, 6]))
>>> d, e = x.split(2, 0) # 在 0 维进行间隔维 2 的拆分
>>> d.size(), e.size()
(torch.Size([2, 10, 6]), torch.Size([1, 10, 6]))

把张量在 0 维度上以间隔 1 来拆分时,其中 x 在 0 维度上的尺寸为 3,就可以分成 3 份。

把张量在 0 维度上以间隔 2 来拆分时,只能分成 2 份,且只能把前面部分先以间隔 2 来拆分,后面不足 2 的部分就直接作为一个分块。

  • torch.chunk(tensor, chunks, dim=0)

在给定维度(轴)上将输入张量进行分块儿

直接用上面的数据来举个例子:

>>> l, m, n = x.chunk(3, 0) # 在 0 维上拆分成 3 份
>>> l.size(), m.size(), n.size()
(torch.Size([1, 10, 6]), torch.Size([1, 10, 6]), torch.Size([1, 10, 6]))
>>> u, v = x.chunk(2, 0) # 在 0 维上拆分成 2 份
>>> u.size(), v.size()
(torch.Size([2, 10, 6]), torch.Size([1, 10, 6]))

把张量在 0 维度上拆分成 3 部分时,因为尺寸正好为 3,所以每个分块的间隔相等,都为 1。

把张量在 0 维度上拆分成 2 部分时,无法平均分配,以上面的结果来看,可以看成是,用 0 维度的尺寸除以需要拆分的份数,把余数作为最后一个分块的间隔大小,再把前面的分块以相同的间隔拆分。

在某一维度上拆分的份数不能比这一维度的尺寸大

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • pytorch中tensor的合并与截取方法

    合并: torch.cat(inputs=(a, b), dimension=1) e.g. x = torch.cat((x,y), 0) 沿x轴合并 截取: x[:, 2:4] 以上这篇pytorch中tensor的合并与截取方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • 在PyTorch中Tensor的查找和筛选例子

    本文源码基于版本1.0,交互界面基于0.4.1 import torch 按照指定轴上的坐标进行过滤 index_select() 沿着某tensor的一个轴dim筛选若干个坐标 >>> x = torch.randn(3, 4) # 目标矩阵 >>> x tensor([[ 0.1427, 0.0231, -0.5414, -1.0009], [-0.4664, 0.2647, -0.1228, -1.1068], [-1.1734, -0.6571, 0.7230,

  • PyTorch之图像和Tensor填充的实例

    在PyTorch中可以对图像和Tensor进行填充,如常量值填充,镜像填充和复制填充等.在图像预处理阶段设置图像边界填充的方式如下: import vision.torchvision.transforms as transforms img_to_pad = transforms.Compose([ transforms.Pad(padding=2, padding_mode='symmetric'), transforms.ToTensor(), ]) 对Tensor进行填充的方式如下: i

  • 详解PyTorch中Tensor的高阶操作

    条件选取:torch.where(condition, x, y) → Tensor 返回从 x 或 y 中选择元素的张量,取决于 condition 操作定义: 举个例子: >>> import torch >>> c = randn(2, 3) >>> c tensor([[ 0.0309, -1.5993, 0.1986], [-0.0699, -2.7813, -1.1828]]) >>> a = torch.ones(2,

  • Pytorch Tensor的索引与切片例子

    1. Pytorch风格的索引 根据Tensor的shape,从前往后索引,依次在每个维度上做索引. 示例代码: import torch a = torch.rand(4, 3, 28, 28) print(a[0].shape) #取到第一个维度 print(a[0, 0].shape) # 取到二个维度 print(a[1, 2, 2, 4]) # 具体到某个元素 上述代码创建了一个shape=[4, 3, 28, 28]的Tensor,我们可以理解为4张图片,每张图片有3个通道,每个通道

  • pytorch: tensor类型的构建与相互转换实例

    Summary 主要包括以下三种途径: 使用独立的函数: 使用torch.type()函数: 使用type_as(tesnor)将张量转换为给定类型的张量. 使用独立函数 import torch tensor = torch.randn(3, 5) print(tensor) # torch.long() 将tensor投射为long类型 long_tensor = tensor.long() print(long_tensor) # torch.half()将tensor投射为半精度浮点类型

  • PyTorch中Tensor的拼接与拆分的实现

    拼接张量:torch.cat() .torch.stack() torch.cat(inputs, dimension=0) → Tensor 在给定维度上对输入的张量序列 seq 进行连接操作 举个例子: >>> import torch >>> x = torch.randn(2, 3) >>> x tensor([[-0.1997, -0.6900, 0.7039], [ 0.0268, -1.0140, -2.9764]]) >>&

  • pytorch中tensor.expand()和tensor.expand_as()函数详解

    tensor.expend()函数 >>> import torch >>> a=torch.tensor([[2],[3],[4]]) >>> print(a.size()) torch.Size([3, 1]) >>> a.expand(3,2) tensor([[2, 2], [3, 3], [4, 4]]) >>> a tensor([[2], [3], [4]]) 可以看出expand()函数括号里面为变形

  • pytorch中tensor张量数据类型的转化方式

    1.tensor张量与numpy相互转换 tensor ----->numpy import torch a=torch.ones([2,5]) tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]) # ********************************** b=a.numpy() array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], dtype=float32) numpy --

  • 对Pytorch中Tensor的各种池化操作解析

    AdaptiveAvgPool1d(N) 对一个C*H*W的三维输入Tensor, 池化输出为C*H*N, 即按照H轴逐行对W轴平均池化 >>> a = torch.ones(2,3,4) >>> a[0,1,2] = 0 >>>> a tensor([[[1., 1., 1., 1.], [1., 1., 0., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1., 1., 1., 1.], [1.,

  • PyTorch中 tensor.detach() 和 tensor.data 的区别详解

    PyTorch0.4中,.data 仍保留,但建议使用 .detach(), 区别在于 .data 返回和 x 的相同数据 tensor, 但不会加入到x的计算历史里,且require s_grad = False, 这样有些时候是不安全的, 因为 x.data 不能被 autograd 追踪求微分 . .detach() 返回相同数据的 tensor ,且 requires_grad=False ,但能通过 in-place 操作报告给 autograd 在进行反向传播的时候. 举例: ten

  • PyTorch中Tensor的数据统计示例

    张量范数:torch.norm(input, p=2) → float 返回输入张量 input 的 p 范数 举个例子: >>> import torch >>> a = torch.full([8], 1) >>> b = a.view(2, 4) >>> c = a.view(2, 2, 2) >>> a.norm(1), b.norm(1), c.norm(1) # 求 1- 范数 (tensor(8.),

  • PyTorch中Tensor的数据类型和运算的使用

    在使用Tensor时,我们首先要掌握如何使用Tensor来定义不同数据类型的变量.Tensor时张量的英文,表示多维矩阵,和numpy对应,PyTorch中的Tensor可以和numpy的ndarray相互转换,唯一不同的是PyTorch可以在GPU上运行,而numpy的ndarray只能在cpu上运行. 常用的不同数据类型的Tensor,有32位的浮点型torch.FloatTensor,   64位浮点型 torch.DoubleTensor,   16位整形torch.ShortTenso

  • Pytorch中Tensor与各种图像格式的相互转化详解

    前言 在pytorch中经常会遇到图像格式的转化,例如将PIL库读取出来的图片转化为Tensor,亦或者将Tensor转化为numpy格式的图片.而且使用不同图像处理库读取出来的图片格式也不相同,因此,如何在pytorch中正确转化各种图片格式(PIL.numpy.Tensor)是一个在调试中比较重要的问题. 本文主要说明在pytorch中如何正确将图片格式在各种图像库读取格式以及tensor向量之间转化的问题.以下代码经过测试都可以在Pytorch-0.4.0或0.3.0版本直接使用. 对py

随机推荐