当前位置: 首页 > news >正文

【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 tx = 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 tx = 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 torchx = 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 torchx= 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官网注释

相关文章:

  • C#学习第15天:泛型
  • list.
  • 【工具变量】各地级市人口集聚及多中心程度数据集(2000-2023年)
  • Unity入门笔记(缘更)
  • 探索大语言模型(LLM):马尔可夫链——从诗歌分析到人工智能的数学工具
  • 高精求小数幂--高精度乘法+小数
  • k230学习笔记-疑难点(1)
  • 第19章:基于efficientNet实现的视频内容识别系统
  • 何小鹏在得意的笑
  • 第五章 SQLite数据库:3、SQLite 常用语法及使用案例
  • Lesson 12 Goodbye and good luck
  • 《TCP/IP网络编程》学习笔记 | Chapter 24:制作 HTTP 服务器端
  • C#winform主线程刷新UI时竟抛异常“从不是创建控件的线程访问它“
  • c++STL——vector的使用和模拟实现
  • 【Android 】ContentProvider深度解析
  • ssh用户秘钥登录设置
  • 接口测试:实用指南4.0
  • 医疗设备预测性维护合规架构:从法规遵循到技术实现的深度解析
  • Electricity Market Optimization(VI) - 机组组合模型以及 Gurobi 求解
  • 20250417-vue-条件插槽
  • 国内多景区实行一票游多日:从门票经济向多元化“链式经济”转型
  • 政府效率部效果不佳?马斯克有意寻求支持,含糊表态部门未来
  • 辽宁男篮被横扫这一晚,中国篮球的一个时代落幕了
  • 讲座预告|政府在人工智能研究和应用领域的作用
  • 增诉滥用职权罪,尹锡悦遭韩国检方追加起诉
  • Meta一季度净利增长三成:上调全年资本支出,受关税影响亚洲出口电商广告支出减少