torch.stack() 和 torch.cat() 都可以按照指定的维度进行拼接,但是两者也有区别,torch.satck() 是增加新的维度进行堆叠,即其维度拼接后会增加一个维度;而torch.cat() 是在原维度上进行堆叠,即其维度拼接后的维度个数和原来一致。具体说明如下:

torch.stack(input,dim)

input: 待拼接的张量序列组(list or tuple),拼接的tensor的维度必须要相等,即tensor1.shape = tensor2.shape

dim: 在哪个新增的维度上进行拼接,不能超过拼接后的张量数据的维度大小,默认为 0

  1. import torch
  2. x1 = torch.tensor([[1, 2, 3],
  3. [4, 5, 6],
  4. [7, 8, 9]])
  5. x2 = torch.tensor([[10, 20, 30],
  6. [40, 50, 60],
  7. [70, 80, 90]])
  8. print(torch.stack((x1,x2),dim=0).shape)
  9. print(torch.stack((x1,x2),dim=1).shape)
  10. print(torch.stack((x1,x2),dim=2).shape)
  11. print(torch.stack((x1,x2),dim=0))
  12. print(torch.stack((x1,x2),dim=1))
  13. print(torch.stack((x1,x2),dim=2))
  14. >> torch.Size([2, 3, 3]) # 2 表示是有两个tensor的拼接,且在第一个维度的位置拼接
  15. >> torch.Size([3, 2, 3])
  16. >> torch.Size([3, 3, 2])
  17. >> tensor([[[ 1, 2, 3],
  18. [ 4, 5, 6],
  19. [ 7, 8, 9]],
  20. [[10, 20, 30],
  21. [40, 50, 60],
  22. [70, 80, 90]]])
  23. >> tensor([[[ 1, 2, 3],
  24. [10, 20, 30]],
  25. [[ 4, 5, 6],
  26. [40, 50, 60]],
  27. [[ 7, 8, 9],
  28. [70, 80, 90]]])
  29. >> tensor([[[ 1, 10],
  30. [ 2, 20],
  31. [ 3, 30]],
  32. [[ 4, 40],
  33. [ 5, 50],
  34. [ 6, 60]],
  35. [[ 7, 70],
  36. [ 8, 80],
  37. [ 9, 90]]])

torch.cat(input, dim)

input: 待拼接的张量序列组(list or tuple),拼接的tensor的维度必须要相等,即tensor1.shape = tensor2.shape

dim: 在哪个已存在的维度上进行拼接,不能超过拼接后的张量数据的维度大小(即原来的维度大小),默认为 0

  1. import torch
  2. x1 = torch.tensor([[1, 2, 3],
  3. [4, 5, 6],
  4. [7, 8, 9]])
  5. x2 = torch.tensor([[10, 20, 30],
  6. [40, 50, 60],
  7. [70, 80, 90]])
  8. print(torch.cat((x1,x2),dim=0).shape)
  9. print(torch.cat((x1,x2),dim=1).shape)
  10. print(torch.cat((x1,x2),dim=0))
  11. print(torch.cat((x1,x2),dim=1))
  12. >> torch.Size([6, 3])
  13. >> torch.Size([3, 6])
  14. >> tensor([[ 1, 2, 3],
  15. [ 4, 5, 6],
  16. [ 7, 8, 9],
  17. [10, 20, 30],
  18. [40, 50, 60],
  19. [70, 80, 90]])
  20. >> tensor([[ 1, 2, 3, 10, 20, 30],
  21. [ 4, 5, 6, 40, 50, 60],
  22. [ 7, 8, 9, 70, 80, 90]])

Pytorch 中 tensor的维度拼接的更多相关文章

  1. pytorch中tensor数据和numpy数据转换中注意的一个问题

    转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...

  2. 对pytorch中Tensor的剖析

    不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...

  3. [Pytorch]Pytorch中tensor常用语法

    原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...

  4. pytorch中tensor张量数据基础入门

    pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...

  5. pytorch中tensor的属性 类型转换 形状变换 转置 最大值

    import torch import numpy as np a = torch.tensor([[[1]]]) #只有一个数据的时候,获取其数值 print(a.item()) #tensor转化 ...

  6. pytorch中tensor张量的创建

    import torch import numpy as np print(torch.tensor([1,2,3])) print(torch.tensor(np.arange(15).reshap ...

  7. pytorch 调整tensor的维度位置

    target.permute([0, 3, 1, 2]) 一定要使用permute以及中括号 一些在我这里没起到作用的网上的例子: 1. https://blog.csdn.net/zouxiaolv ...

  8. tensorflow中tensor的静态维度和动态维度

    tf中使用张量(tensor)这种数据结构来表示所有的数据,可以把张量看成是一个具有n个维度的数组或列表,张量会在各个节点之间流动,参与计算. 张量具有静态维度和动态维度. 在图构建过程中定义的张量拥 ...

  9. pytorch 中的数据类型,tensor的创建

    pytorch中的数据类型 import torch a=torch.randn(2,3) b=a.type() print(b) #检验是否是该数据类型 print(isinstance(a,tor ...

随机推荐

  1. Django-----cookie&session

    cookie 保存在用户浏览器端的一个键值对(别人给的凭证) 服务端可以向用户浏览器写cookie 客户端每次发请求会携带cookie去(放在请求头里面) 淘宝的cookie  京东的cookie(h ...

  2. 将VScode添加至右键菜单

    首先展示下最终效果: 右击单个文件: 右击文件夹: 操作流程: 1.右击VScode快捷方式查看属性,找到快捷方式对应的目标路径 2.随便找个地方新建个XXX.reg的注册表脚本文件,文件名叫啥都可以 ...

  3. VMware安装Ubuntu20(图文教程,超详细)

    VMware安装Ubuntu20(图文教程,超详细) 此文讲述使用 VMware 工具安装 Ubuntu 系列虚拟机,不同位数和不同版本的 Ubuntu 安装过程相差无几,这里以 Ubuntu20 6 ...

  4. 5.文件共享总结中篇-Linux服务器文件共享

    今天我们来讨论Linux系统之间的文件共享,包含:SCP.FTP.rz / sz.wget 一.SCP 其实我在写SSH协议常见问题排错章节中已介绍其SCP用法. 现我把SCP常用语法格式,给大家说下 ...

  5. Redis GEO 地理位置

    目录 GEO指令 GEOADD GEODIST GEOPOP GEOHASH GEORADIUS GEORADIUSBYMEMBER 指令补充 删除操作 避免单集合数量过多 存储原理 GEOADD存储 ...

  6. WinUI3开发笔记(Ⅰ)

    ·背景:自从接触了微软的WinUI3的界面,瞬间觉得C# .NetFramework不香了,于是入坑网上教程极少的WinUI3的开发...... 难 (一,安装开发环境) 具体参考微软官网说明http ...

  7. 好客租房57-props深入(4props的默认值)

    1给props设置默认值 //导入react     import React from 'react'     import ReactDOM from 'react-dom'     import ...

  8. Vue2-组件通讯传值

    Vue2组件通讯传值 方法 Slot插槽--父向子内容分发,子组件只读 mixin混入--定义公共变量或方法,mixin数据不共享,组件中mixin实例互不影响 provide+inject--依赖注 ...

  9. 【Unity Shader学习笔记】Unity光照基础-漫反射光照

    本代码只适用于平行光. 1.逐顶点漫反射光照 1.1漫反射光照原理 1.2代码实现 在Properties语义块中声明一个漫反射颜色属性 Properties { //漫反射参数,用于调整漫反射效果 ...

  10. 【抬杠C#】如何实现接口的base调用

    背景 在三年前发布的C#8.0中有一项重要的改进叫做接口默认实现,从此以后,接口中定义的方法可以包含方法体了,即默认实现.不过对于接口的默认实现,其实现类或者子接口在重写这个方法的时候不能对其进行ba ...