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

torch.stack(input,dim)

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

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

import torch 

x1 = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
x2 = torch.tensor([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]]) print(torch.stack((x1,x2),dim=0).shape)
print(torch.stack((x1,x2),dim=1).shape)
print(torch.stack((x1,x2),dim=2).shape) print(torch.stack((x1,x2),dim=0))
print(torch.stack((x1,x2),dim=1))
print(torch.stack((x1,x2),dim=2)) >> torch.Size([2, 3, 3]) # 2 表示是有两个tensor的拼接,且在第一个维度的位置拼接
>> torch.Size([3, 2, 3])
>> torch.Size([3, 3, 2])
>> tensor([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]], [[10, 20, 30],
[40, 50, 60],
[70, 80, 90]]])
>> tensor([[[ 1, 2, 3],
[10, 20, 30]], [[ 4, 5, 6],
[40, 50, 60]], [[ 7, 8, 9],
[70, 80, 90]]])
>> tensor([[[ 1, 10],
[ 2, 20],
[ 3, 30]], [[ 4, 40],
[ 5, 50],
[ 6, 60]], [[ 7, 70],
[ 8, 80],
[ 9, 90]]])

torch.cat(input, dim)

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

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

import torch

x1 = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
x2 = torch.tensor([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]]) print(torch.cat((x1,x2),dim=0).shape)
print(torch.cat((x1,x2),dim=1).shape) print(torch.cat((x1,x2),dim=0))
print(torch.cat((x1,x2),dim=1)) >> torch.Size([6, 3])
>> torch.Size([3, 6]) >> tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
>> tensor([[ 1, 2, 3, 10, 20, 30],
[ 4, 5, 6, 40, 50, 60],
[ 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. Kafka Kerberos 安全认证

    本主要介绍在 Kafka 中如何配置 Kerberos 认证,文中所使用到的软件版本:Java 1.8.0_261.Kafka_2.12-2.6.0.Kerberos 1.15.1. 1. Kerbe ...

  2. 五二不休息,今天也学习,从JS执行栈角度图解递归以及二叉树的前、中、后遍历的底层差异

    壹 ❀ 引 想必凡是接触过二叉树算法的同学,在刚上手那会,一定都经历过题目无从下手,甚至连题解都看不懂的痛苦.由于leetcode不方便调试,题目做错了也不知道错在哪里,最后无奈的cv答案后心里还不断 ...

  3. Python 函数进阶-递归函数

    递归函数 什么是递归函数 如果一个函数,可以自己调用自己,那么这个函数就是一个递归函数. 递归,递就是去,归就是回,递归就是一去一回的过程. 递归函数的条件 一般来说,递归需要边界条件,整个递归的结构 ...

  4. 交换机POE技术知识大全

    公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ 一个典型的以太网供电系统,在配线柜里保留以太网交换机设备,用一个带电源供电集线器(Midspan HUB)给局域网的双绞线提 ...

  5. C#/VB.NET 在Excel单元格中应用多种字体格式

    在Excel中,可对单元格中的字符串设置多种不同样式,通常只需要获取到单元格直接设置样式即可,该方法设置的样式会应用于该单元格中的所有字符.如果需要对单元格中某些字符设置样式,则可以参考本文中的方法. ...

  6. git 1.2

    1.git服务器的搭建 gitlab 常用命令: gitlab-rails console -e production  进入控制台指令 sudo gitlab-ctl start # 启动所有 gi ...

  7. 常见的邮箱服务器(SMTP,POP3)地址,端口

    163.com: POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25) sina.com: POP3服务器地址:pop3.sina.c ...

  8. C# 通关手册(持续更新......)

    String 常用静态方法 string.Compare(string str1,string str2,bool ignoreCase) 按照字典顺序比较字符串 当str1 > str2时,返 ...

  9. QTP——功能测试

    一.前言(课设目的及内容) QTP是quicktest Professional的简称,是一种自动测试工具.使用QTP的目的是想用它来执行重复的手动测试,主要是用于回归测试和测试同一软件的新版本.因此 ...

  10. Ubuntu 静默安装DEB包(非交互式)~解决Ubuntu下安装DEB包弹窗交互的问题

    在Ubuntu环境下安装DEB包时,比如安装MySQL式经常会弹出交互式要输入密码的操作.有时候我们期望编写Shell脚本一键部署MySQL时不想要弹窗交互时,则可以使用以下方式实现自动化安装Deb软 ...