【Pytorch之一】--torch.stack()方法详解

torch.stack方法详解

stack解释
pytorch官网注释
pytorch官网

Parameters
tensors:张量序列,也就是要进行stack操作的对象们,可以有很多个张量。
dim:按照dim的方式对这些张量进行stack操作,也就是你要按照哪种堆叠方式对张量进行堆叠。dim的取值范围为闭区间[0,输入Tensor的维数]
return
堆叠后的张量

二、例子

2.1 一维tensor进行stack操作

import torch as t

x = t.tensor([1, 2, 3, 4])
y = t.tensor([5, 6, 7, 8])

print(x.shape)
print(y.shape)

z1 = t.stack((x, y), dim=0)
print(z1)
print(z1.shape)

z2 = t.stack((x, y), dim=1)
print(z2)
print(z2.shape)
torch.Size([4])
torch.Size([4])
tensor([[1, 2, 3, 4],
        [5, 6, 7, 8]])
torch.Size([2, 4])
tensor([[1, 5],
        [2, 6],
        [3, 7],
        [4, 8]])
torch.Size([4, 2])

图解

2.2 2个二维tensor进行stack操作

 import torch as t
 x = t.tensor([[1,2,3],[4,5,6]])
 y = t.tensor([[7,8,9],[10,11,12]])
 print(x.shape)
 print(y.shape)

 z1 = t.stack((x,y), dim=0)
 print(z1)
 print(z1.shape)

 z2 = t.stack((x,y), dim=1)
 print(z2)
 print(z2.shape)

 z3 = t.stack((x,y), dim=2)
 print(z3)
 print(z3.shape)
torch.Size([2, 3])
torch.Size([2, 3])
tensor([[[ 1,  2,  3],
         [ 4,  5,  6]],

        [[ 7,  8,  9],
         [10, 11, 12]]])
torch.Size([2, 2, 3])
tensor([[[ 1,  2,  3],
         [ 7,  8,  9]],

        [[ 4,  5,  6],
         [10, 11, 12]]])
torch.Size([2, 2, 3])
tensor([[[ 1,  7],
         [ 2,  8],
         [ 3,  9]],

        [[ 4, 10],
         [ 5, 11],
         [ 6, 12]]])
torch.Size([2, 3, 2])

在这里插入图片描述

2.3 多个二维tensor进行stack操作

import torch

x = torch.tensor([[1,2,3],[4,5,6]])
y = torch.tensor([[7,8,9],[10,11,12]])
z = torch.tensor([[13,14,15],[16,17,18]])
print(x.shape)
print(y.shape)
print(z.shape)

r1 = torch.stack((x,y,z),dim=0)
print(r1)
print(r1.shape)

r2 = torch.stack((x,y,z),dim=1)
print(r2)
print(r2.shape)

r3 = torch.stack((x,y,z),dim=2)
print(r3)
print(r3.shape)
torch.Size([2, 3])
torch.Size([2, 3])
torch.Size([2, 3])
tensor([[[ 1,  2,  3],
         [ 4,  5,  6]],

        [[ 7,  8,  9],
         [10, 11, 12]],

        [[13, 14, 15],
         [16, 17, 18]]])
torch.Size([3, 2, 3])
tensor([[[ 1,  2,  3],
         [ 7,  8,  9],
         [13, 14, 15]],

        [[ 4,  5,  6],
         [10, 11, 12],
         [16, 17, 18]]])
torch.Size([2, 3, 3])
tensor([[[ 1,  7, 13],
         [ 2,  8, 14],
         [ 3,  9, 15]],

        [[ 4, 10, 16],
         [ 5, 11, 17],
         [ 6, 12, 18]]])
torch.Size([2, 3, 3])

2.4 2个三维tensor进行stack操作

import torch

x= torch.tensor([[[1,2,3],[4,5,6]],
                  [[2,3,4],[5,6,7]]])
y = torch.tensor([[[7,8,9],[10,11,12]],
                  [[8,9,10],[11,12,13]]])
print(x.shape)
print(y.shape)
z1 = torch.stack((x,y),dim=0)
print(z1)
print(z1.shape)
z2 = torch.stack((x,y),dim=1)
print(z2)
print(z2.shape)
z3 = torch.stack((x,y),dim=2)
print(z3)
print(z3.shape)
z4 = torch.stack((x,y),dim=3)
print(z4)
print(z4.shape)

torch.Size([2, 2, 3])
torch.Size([2, 2, 3])
tensor([[[[ 1,  2,  3],
          [ 4,  5,  6]],

         [[ 2,  3,  4],
          [ 5,  6,  7]]],


        [[[ 7,  8,  9],
          [10, 11, 12]],

         [[ 8,  9, 10],
          [11, 12, 13]]]])
torch.Size([2, 2, 2, 3])
tensor([[[[ 1,  2,  3],
          [ 4,  5,  6]],

         [[ 7,  8,  9],
          [10, 11, 12]]],


        [[[ 2,  3,  4],
          [ 5,  6,  7]],

         [[ 8,  9, 10],
          [11, 12, 13]]]])
torch.Size([2, 2, 2, 3])
tensor([[[[ 1,  2,  3],
          [ 7,  8,  9]],

         [[ 4,  5,  6],
          [10, 11, 12]]],


        [[[ 2,  3,  4],
          [ 8,  9, 10]],

         [[ 5,  6,  7],
          [11, 12, 13]]]])
torch.Size([2, 2, 2, 3])
tensor([[[[ 1,  7],
          [ 2,  8],
          [ 3,  9]],

         [[ 4, 10],
          [ 5, 11],
          [ 6, 12]]],


        [[[ 2,  8],
          [ 3,  9],
          [ 4, 10]],

         [[ 5, 11],
          [ 6, 12],
          [ 7, 13]]]])
torch.Size([2, 2, 3, 2])

参考文献

[1] PyTorch基础(18)-- torch.stack()方法

[2]pytorch官网注释

### PyTorch `torch.stack` 参数详解 #### 参数说明 `torch.stack` 是用于沿给定维度连接一系列相同形状的张量序列的方法。该函数的主要参数如下: - **tensors (Sequence[Tensor])**: 这是一个张量列表,所有的输入张量必须具有相同的形状[^1]。 - **dim (int)**: 表示新维度的位置索引,在这个位置上将会把传入的张量按照顺序堆叠起来,默认值为0。这意味着如果设置其他数值,则会在指定的新轴处插入这些张量[^4]。 - **out (Tensor, optional)**: 可选参数,指定了输出张量的目标存储位置;如果不提供此参数,则会创建一个新的张量来保存结果。 #### 使用方法与注意事项 当调用 `torch.stack()` 方法时,需要注意的是所有待处理的张量都应当具备一致的数据结构尺寸,即它们应该拥有相等数量的维度以及对应维度上的长度也需匹配。此外,通过调整 `dim` 参数可以控制新增加的那个维度位于何处,这对于构建多维数组非常有用[^2]。 #### 示例代码展示 下面给出一段简单的 Python 代码片段用来演示如何利用 `torch.stack` 来操作两个三维随机数列向量并将其沿着最后一个维度进行拼接: ```python import torch from torch import nn # 定义两个大小完全一样的3D Tensor a = torch.randn((1, 3, 5)) b = torch.randn((1, 3, 5)) # 将这两个tensor按最后一维(-1)方向叠加在一起形成新的四维tensor frustum = torch.stack((a, b), -1) print(f"Shape of stacked tensor: {frustum.shape}") ``` 这段程序首先导入必要的库文件,接着定义了两个形状均为 `(1, 3, 5)` 的三阶张量 `a` 和 `b` 。之后运用 `torch.stack` 函数将二者依据 `-1` 维度组合成更高一阶的新张量 `frustum` ,最终打印出其具体形态以验证操作成功与否[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不高明的骗子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值