torch.cat和torch.stack的区别

torch.cattorch.stack 是 PyTorch 中用于组合张量的两个常用函数,它们的核心区别在于输入张量的维度和输出张量的维度变化。以下是详细对比:

1. torch.cat (Concatenate)

  • 作用 :沿现有维度 拼接多个张量,不创建新维度

  • 输入要求 :所有张量的形状必须除拼接维度外完全相同

  • 语法

    python 复制代码
    torch.cat(tensors, dim=0)  # dim 指定拼接的维度
  • 示例

    python 复制代码
    a = torch.tensor([[1, 2], [3, 4]])  # shape (2, 2)
    b = torch.tensor([[5, 6]])           # shape (1, 2)
    
    # 沿 dim=0 拼接(行方向)
    c = torch.cat([a, b], dim=0)
    print(c)
    # tensor([[1, 2],
    #         [3, 4],
    #         [5, 6]])  # shape (3, 2)
  • 特点

    • 拼接后的张量在指定维度上的大小是输入张量该维度大小的总和。

    • 其他维度必须完全一致。

2. torch.stack

  • 作用 :沿新维度 堆叠多个张量,创建新维度

  • 输入要求 :所有张量的形状必须完全相同

  • 语法

    python 复制代码
    torch.stack(tensors, dim=0)  # dim 指定新维度的位置
  • 示例

    python 复制代码
    a = torch.tensor([1, 2])  # shape (2,)
    b = torch.tensor([3, 4])  # shape (2,)
    
    # 沿新维度 dim=0 堆叠
    c = torch.stack([a, b], dim=0)
    print(c)
    # tensor([[1, 2],
    #         [3, 4]])  # shape (2, 2)
    
    # 沿新维度 dim=1 堆叠
    d = torch.stack([a, b], dim=1)
    print(d)
    # tensor([[1, 3],
    #         [2, 4]])  # shape (2, 2)
  • 特点

    • 输出张量比输入张量多一个维度

    • 适用于将多个相同形状的张量合并为批次(如 batch_size 维度)。

3. 关键区别总结

4. 直观对比示例

假设有两个张量:

python 复制代码
x = torch.tensor([1, 2])  # shape (2,)
y = torch.tensor([3, 4])  # shape (2,)

torch.cat 结果

python 复制代码
torch.cat([x, y], dim=0)  # tensor([1, 2, 3, 4]), shape (4,)

torch.stack 结果

python 复制代码
torch.stack([x, y], dim=0)  # tensor([[1, 2], [3, 4]]), shape (2, 2)

5. 如何选择?

  • torch.cat 当需要扩展现有维度(如拼接多个特征图)。

  • torch.stack 当需要创建新维度(如构建批次数据或堆叠不同模型的输出)

通过理解两者的维度变化逻辑,可以避免常见的形状错误(如 size mismatch)。

相关推荐
蹦蹦跳跳真可爱5897 分钟前
Python----循环神经网络(Transformer ----注意力机制)
人工智能·深度学习·nlp·transformer·循环神经网络
空中湖2 小时前
tensorflow武林志第二卷第九章:玄功九转
人工智能·python·tensorflow
lishaoan772 小时前
使用tensorflow的线性回归的例子(七)
人工智能·tensorflow·线性回归
千宇宙航5 小时前
闲庭信步使用SV搭建图像测试平台:第三十一课——基于神经网络的手写数字识别
图像处理·人工智能·深度学习·神经网络·计算机视觉·fpga开发
onceco6 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
天水幼麟6 小时前
动手学深度学习-学习笔记(总)
笔记·深度学习·学习
jndingxin8 小时前
OpenCV CUDA模块设备层-----高效地计算两个 uint 类型值的带权重平均值
人工智能·opencv·计算机视觉
天水幼麟9 小时前
动手学深度学习-学习笔记【二】(基础知识)
笔记·深度学习·学习
Sweet锦9 小时前
零基础保姆级本地化部署文心大模型4.5开源系列
人工智能·语言模型·文心一言
hie9889410 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab