torch.cat() 和 torch.stack()略有不同
torch.cat(tensors,dim=0,out=None)→ Tensor
torch.cat()对tensors沿指定维度拼接,但返回的Tensor的维数不会变,可理解为续接;
torch.stack(tensors,dim=0,out=None)→ Tensor
torch.stack()同样是对tensors沿指定维度拼接,但返回的Tensor会多一维,可理解为叠加;
————————————————
版权声明:本文为CSDN博主「进阶媛小吴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wuli_xin/article/details/118972316

torch.cat((a,b),dim=1)和torch.cat((a,b)axis=1)一样。

同理:torch.stack((a,b),dim=1)和torch.stack((a,b)axis=1)一样。

zz=torch.rand(100)#默认zz是列向量。而非行向量。

上述3行的情况,自己已经实际实验过。

结果为:

上述行数相同d,c,在第一维度也即列上拼接时,能拼接成100行六列的tensor.

import torch
a=torch.rand(100)
b=torch.rand(100)
c=torch.rand((100,2))
d=torch.rand((100,2))
e=torch.rand((100,2)) ab,ab1,cd,cd1,cd2,cd3=torch.stack((a,b)),torch.stack((a,b),dim=0),torch.stack((c,d),dim=0),torch.stack((c,d),dim=1),torch.stack((c,d,e),axis=1),torch.stack((c,d,e),dim=-1)
'a.shape',a.shape,"b.shape",b.shape,"ab.shape",ab.shape,"ab1.shape",ab1.shape,'cd.shape',cd.shape,'cd1.shape',cd1.shape,'cd2.shape',cd2.shape,cd3.shape

  此处需注意的是:torch.stack((c,d,e),dim=-1)和torch.stack((c,d,e),dim=2)结果是一样的;

('a.shape',
torch.Size([100]),
'b.shape',
torch.Size([100]),
'ab.shape',
torch.Size([2, 100]),
'ab1.shape',
torch.Size([2, 100]),
'cd.shape',
torch.Size([2, 100, 2]),
'cd1.shape',
torch.Size([100, 2, 2]),
'cd2.shape',
torch.Size([100, 3, 2]),
torch.Size([100, 2, 3]))

  

import torch
# t1=torch.tensor([1,1,1])
# t2=torch.tensor([2,2,2])
# t3=torch.tensor([3,3,3]) f1=torch.tensor([[1,2,3],[4,5,6]])
f2=torch.tensor([[7,8,9],[10,11,12]])
c=torch.tensor([[13,14,15],[16,17,18]]) # a=torch.cat((f1,f2,c),dim=0)
# b=torch.cat((f1,f2,c),1)
# print(a.shape,b.shape,sep='\n')
# d=torch.rand(100,4)
# e=torch.cat((d,c),1)
# print(e.shape)
g=torch.stack((f1,f2,c),0)
g1=torch.stack((f1,f2,c),1)
print(f1.shape,f2.shape,c.shape)
print('g: ',g.shape,g,sep='\n')
print('g1: ',g1.shape,g1,sep='\n') 输出结果为: torch.Size([2, 3]) torch.Size([2, 3]) torch.Size([2, 3])
g:
torch.Size([3, 2, 3])#本来3个,就3个;本来2行3列就两行三列;只不过把他们放到一起,变成了3维的,多了一个维度;个人理解,可能有误。
tensor([[[ 1, 2, 3],
[ 4, 5, 6]], [[ 7, 8, 9],
[10, 11, 12]], [[13, 14, 15],
[16, 17, 18]]])
g1:
torch.Size([2, 3, 3])#把本来的三个中,每个的第一列拼在一块;第二列拼在一块;再把拼过后的第一列和第二列分别作为一个二维矩阵; 个人理解,可能有误。 tensor([[[ 1, 2, 3],
[ 7, 8, 9],
[13, 14, 15]], [[ 4, 5, 6],
[10, 11, 12],
[16, 17, 18]]])
import torch
# t1=torch.tensor([1,1,1])
# t2=torch.tensor([2,2,2])
# t3=torch.tensor([3,3,3]) f1=torch.tensor([[1,2,3],[4,5,6]])
f2=torch.tensor([[7,8,9],[10,11,12]])
c=torch.tensor([[13,14,15],[16,17,18]]) # a=torch.cat((f1,f2,c),dim=0)
# b=torch.cat((f1,f2,c),1)
# print(a.shape,b.shape,sep='\n')
# d=torch.rand(100,4)
# e=torch.cat((d,c),1)
# print(e.shape)
g=torch.stack((f1,f2,c),0)
g1=torch.stack((f1,f2,c),2)
print(f1.shape,f2.shape,c.shape)
print('g: ',g.shape,g,sep='\n')
print('g1: ',g1.shape,g1,sep='\n') 输出结果:
torch.Size([2, 3]) torch.Size([2, 3]) torch.Size([2, 3])
g:
torch.Size([3, 2, 3])
tensor([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]],
[[13, 14, 15],
[16, 17, 18]]])
g1:
torch.Size([2, 3, 3])
tensor([[[ 1, 7, 13],
[ 2, 8, 14],
[ 3, 9, 15]],
[[ 4, 10, 16],
[ 5, 11, 17],
[ 6, 12, 18]]])

  

import torch
# t1=torch.tensor([1,1,1])
# t2=torch.tensor([2,2,2])
# t3=torch.tensor([3,3,3]) f1=torch.tensor([[1,2,3],[4,5,6]])
f2=torch.tensor([[7,8,9],[10,11,12]])
c=torch.tensor([[13,14,15],[16,17,18]]) # a=torch.cat((f1,f2,c),dim=0)
# b=torch.cat((f1,f2,c),1)
# print(a.shape,b.shape,sep='\n')
# d=torch.rand(100,4)
# e=torch.cat((d,c),1)
# print(e.shape)
g=torch.stack((f1,f2,c),0)
g1=torch.stack((f1,f2,c),3)#此处dim=3,或比3大的任何正数,都是如下报错结果。
print(f1.shape,f2.shape,c.shape)
print('g: ',g.shape,g,sep='\n')
print('g1: ',g1.shape,g1,sep='\n') 输出结果:
Traceback (most recent call last):
File "<input>", line 17, in <module>
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)

此外: 

如果,torch.stack()的维度dim输入的是-1,-2,-3,也都可以正确输出结果。但是如果输入比-3小的任何数则会报错;具体如下:

import torch
# t1=torch.tensor([1,1,1])
# t2=torch.tensor([2,2,2])
# t3=torch.tensor([3,3,3]) f1=torch.tensor([[1,2,3],[4,5,6]])
f2=torch.tensor([[7,8,9],[10,11,12]])
c=torch.tensor([[13,14,15],[16,17,18]]) # a=torch.cat((f1,f2,c),dim=0)
# b=torch.cat((f1,f2,c),1)
# print(a.shape,b.shape,sep='\n')
# d=torch.rand(100,4)
# e=torch.cat((d,c),1)
# print(e.shape)
g=torch.stack((f1,f2,c),0)
g1=torch.stack((f1,f2,c),-1) #此时,维度是-1
print(f1.shape,f2.shape,c.shape)
print('g: ',g.shape,g,sep='\n')
print('g1: ',g1.shape,g1,sep='\n') 输出结果为:
torch.Size([2, 3]) torch.Size([2, 3]) torch.Size([2, 3])
g:
torch.Size([3, 2, 3])
tensor([[[ 1, 2, 3],
[ 4, 5, 6]], [[ 7, 8, 9],
[10, 11, 12]], [[13, 14, 15],
[16, 17, 18]]])
g1:
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.stack()的维度dim输入的是--2;

输出结果为:
torch.Size([2, 3]) torch.Size([2, 3]) torch.Size([2, 3])
g:
torch.Size([3, 2, 3])
tensor([[[ 1, 2, 3],
[ 4, 5, 6]], [[ 7, 8, 9],
[10, 11, 12]], [[13, 14, 15],
[16, 17, 18]]])
g1:
torch.Size([2, 3, 3])
tensor([[[ 1, 2, 3],
[ 7, 8, 9],
[13, 14, 15]], [[ 4, 5, 6],
[10, 11, 12],
[16, 17, 18]]])

torch.stack()的维度dim输入的是-3;

torch.Size([2, 3]) torch.Size([2, 3]) torch.Size([2, 3])
g:
torch.Size([3, 2, 3])
tensor([[[ 1, 2, 3],
[ 4, 5, 6]], [[ 7, 8, 9],
[10, 11, 12]], [[13, 14, 15],
[16, 17, 18]]])
g1:
torch.Size([3, 2, 3])
tensor([[[ 1, 2, 3],
[ 4, 5, 6]], [[ 7, 8, 9],
[10, 11, 12]], [[13, 14, 15],
[16, 17, 18]]])
import torch
# t1=torch.tensor([1,1,1])
# t2=torch.tensor([2,2,2])
# t3=torch.tensor([3,3,3]) f1=torch.tensor([[1,2,3],[4,5,6]])
f2=torch.tensor([[7,8,9],[10,11,12]])
c=torch.tensor([[13,14,15],[16,17,18]]) # a=torch.cat((f1,f2,c),dim=0)
# b=torch.cat((f1,f2,c),1)
# print(a.shape,b.shape,sep='\n')
# d=torch.rand(100,4)
# e=torch.cat((d,c),1)
# print(e.shape)
g=torch.stack((f1,f2,c),0)
g1=torch.stack((f1,f2,c),-4)#此处是dim=-4,小于-4的任何负数,输出类似的结果。
print(f1.shape,f2.shape,c.shape)
print('g: ',g.shape,g,sep='\n')
print('g1: ',g1.shape,g1,sep='\n') 输出结果为:
Traceback (most recent call last):
File "<input>", line 17, in <module>
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got -4)
import torch
a=torch.rand(100)
b=torch.rand(100) c=torch.stack((a,b))
d=torch.stack((a,b),dim=0)
e=torch.stack((a,b),dim=1) f=torch.cat((a,b))
f1=torch.cat((a,b),dim=0)
# f2=torch.cat((a,b),dim=1)#错误提示:Dimension out of range (expected to be in range of [-1, 0], but got 1)
c.size,c.size(),c.shape,d.shape,e.shape,f.shape,f.size(),f1.shape
#从输出结果可看出,torch.rand(100)生成的是100行1列的数据,也即是一个列向量;concat沿着已有的维度拼接,stack在新创建的维度上拼接;
#输出:
(<function Tensor.size>,
torch.Size([2, 100]),
torch.Size([2, 100]),
torch.Size([2, 100]),
torch.Size([100, 2]),
torch.Size([200]),
torch.Size([200]),
torch.Size([200]))

  

torch.cat()和torch.stack()的更多相关文章

  1. torch.stack()与torch.cat()

    torch.stack():http://www.45fan.com/article.php?aid=1D8JGDik5G49DE1X torch.stack()个人理解:属于先变形再cat的操作,所 ...

  2. torch.cat拼接 stack拼接 分块chunk

    torch.cat拼接 stack拼接 分块chunk 待办 https://blog.csdn.net/qq_39709535/article/details/80803003 stack dim理 ...

  3. Pytorch中的torch.cat()函数

    cat是concatnate的意思:拼接,联系在一起. 先说cat( )的普通用法 如果我们有两个tensor是A和B,想把他们拼接在一起,需要如下操作: C = torch.cat( (A,B),0 ...

  4. Pytorch的torch.cat实例

    import torch 通过 help((torch.cat)) 可以查看 cat 的用法 cat(seq,dim,out=None) 其中 seq表示要连接的两个序列,以元组的形式给出,例如:se ...

  5. 从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系

    从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系 relu多种实现之间的关系 relu 函数在 pytorch 中总共有 3 次出现: torc ...

  6. 【Pytorch】关于torch.matmul和torch.bmm的输出tensor数值不一致问题

    发现 对于torch.matmul和torch.bmm,都能实现对于batch的矩阵乘法: a = torch.rand((2,3,10))b = torch.rand((2,2,10))### ma ...

  7. [pytorch笔记] torch.nn vs torch.nn.functional; model.eval() vs torch.no_grad(); nn.Sequential() vs nn.moduleList

    1. torch.nn与torch.nn.functional之间的区别和联系 https://blog.csdn.net/GZHermit/article/details/78730856 nn和n ...

  8. Pytorch本人疑问(1) torch.nn和torch.nn.functional之间的区别

    在写代码时发现我们在定义Model时,有两种定义方法: torch.nn.Conv2d()和torch.nn.functional.conv2d() 那么这两种方法到底有什么区别呢,我们通过下述代码看 ...

  9. torch.rand、torch.randn、torch.normal、torch.linespace

    torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) # ...

随机推荐

  1. 搭建MySQL集群-注意版本

    系统环境采样(来自其他机器,直接copy过来的,在安装的机器上,按照步骤查看即可,当然这些还不够实际,后续补充) 检查系统内是否有其他mysql rpm -qa | grep mysql 是否存在my ...

  2. Java-GUI编程之菜单组件

    前面讲解了如果构建GUI界面,其实就是把一些GUI的组件,按照一定的布局放入到容器中展示就可以了.在实际开发中,除了主界面,还有一类比较重要的内容就是菜单相关组件,可以通过菜单相关组件很方便的使用特定 ...

  3. Blazor Bootstrap 组件库语音组件介绍

    Speech 语音识别与合成 通过麦克风语音采集转换为文字(STT),或者通过文字通过语音朗读出来(TTS) 本组件依赖于 BootstrapBlazor.AzureSpeech,使用本组件时需要引用 ...

  4. go interface{}使用

    先上代码 func In(haystack []interface{}, needle interface{}) (bool, error) { sVal := reflect.ValueOf(hay ...

  5. 20202127 实验二《Python程序设计》实验报告

    20202127 2021-2022-2 <Python程序设计>实验二报告 课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强 ...

  6. 树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统

    前言   树莓派也可以做商业应用,工业控制,其稳定性和可靠性已经得到了验证,故而工业控制,一些停车场等场景也有采用树莓派作为主控的,本片介绍了研华ADVANTECH的树莓派套件组UNO-220-P4N ...

  7. 攻防世界-MISC:如来十三掌

    这是攻防世界新手练习区的第三题,题目如下: 点击附件1下载,打开后内容如下: 没看懂是什么,还是参考一下WP吧.WP说去一个叫"与佛论禅"的网站,登进去后 发现是一个加解密网站,将 ...

  8. 自己的~/.vimrc

    " 语法高亮syntax on " 搜索高亮set hlsearch " 显示行号set number" let mapleader="," ...

  9. elasticSearch 7.6.1 入门及elasticSearch整合springboot

    一.ElasticSearch概述 官网:https://www.elastic.co/cn/downloads/elasticsearch Elaticsearch,简称为es,es是一个开源的高扩 ...

  10. 手脱UPX(3.91)

    1.使用Detect It Easy进行查壳: 2.使用x32dbg打开该带壳程序,在选项->选项->异常中添加区间设置0~FFFFFFFF全忽略: 3.我们F9运行到程序入口处,看到了p ...