当前位置:网站首页>Official explanation, detailed explanation and example of torch.cat() function
Official explanation, detailed explanation and example of torch.cat() function
2022-08-09 10:45:00 【Fuzzy Pack】
You can directly see the example below,Look back at the previous explanation,就很明白了.
在pytorch
中,常见的拼接函数主要是两个,分别是:
stack()
cat()
一般torch.cat()
是为了把多个tensor
exist for splicing.实际使用中,和torch.stack()
使用场景不同:参考链接torch.stack(),But this article mainly sayscat()
.
torch.cat()
和python
中的内置函数cat()
, in use and purpose,是没有区别的,The difference is that the former operation object istensor
.
1. cat()
函数目的: 在给定维度上对输入的张量序列seq 进行连接操作.
outputs = torch.cat(inputs, dim=?) → Tensor
参数
- inputs : 待连接的张量序列,可以是任意相同
Tensor
类型的python 序列 - dim : 选择的扩维, 必须在
0
到len(inputs[0])
之间,沿着此维连接张量序列.
2. 重点
- 输入数据必须是序列,序列中数据是任意相同的
shape
的同类型tensor
- 维度不可以超过输入数据的任一个张量的维度
3.举例子
- 准备数据,每个的
shape
都是[2,3]
# x1
x1 = torch.tensor([[11,21,31],[21,31,41]],dtype=torch.int)
x1.shape # torch.Size([2, 3])
# x2
x2 = torch.tensor([[12,22,32],[22,32,42]],dtype=torch.int)
x2.shape # torch.Size([2, 3])
- 合成
inputs
'inputs为2个形状为[2 , 3]的矩阵 '
inputs = [x1, x2]
print(inputs)
'打印查看'
[tensor([[11, 21, 31],
[21, 31, 41]], dtype=torch.int32),
tensor([[12, 22, 32],
[22, 32, 42]], dtype=torch.int32)]
3.查看结果, 测试不同的dim
拼接结果
In [1]: torch.cat(inputs, dim=0).shape
Out[1]: torch.Size([4, 3])
In [2]: torch.cat(inputs, dim=1).shape
Out[2]: torch.Size([2, 6])
In [3]: torch.cat(inputs, dim=2).shape
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
You can copy the code and run it and you will find the rules.
总结
通常用来,把torch.stack
得到tensor
exist for splicing.
边栏推荐
- 15.8 the semaphore Unix environment programming chapter 15
- arcgis制图之天地图符号样式配置
- tensor.eq() tensor.item() tensor.argmax()
- Unix Environment Programming Chapter 15 15.9 Shared Storage
- OpenGL 2.0编程例子
- 史上最小白之《Word2vec》详解
- Electron application development best practices
- faster-rcnn中的RPN原理
- Restful WebServices 学习笔记
- 常用语言图表库总结
猜你喜欢
Probably 95% of the people are still making PyTorch mistakes
LM小型可编程控制器软件(基于CoDeSys)笔记二十六:plc的数据存储区(模拟量输入通道部分)
备战金三银四:如何成功拿到阿里offer(经历+面试题+如何准备)
自启服务mock联调跨域问题
arcgis制图之天地图符号样式配置
今天做了手机播放器的均衡器
Cpolar内网穿透的面板功能介绍
商业技术解决方案与高阶技术专题 - 数据可视化专题
BERT预训练模型(Bidirectional Encoder Representations from Transformers)-原理详解
使用cpolar远程连接群晖NAS(创建临时链接)
随机推荐
【原创】JPA中@PrePersist和@PreUpdate的用法
对话跨国消费品牌DPO:数据安全合规从何做起?8.11直播见!
多行省略和选择器
用Word写代码
1004 成绩排名 (20 分)
[Original] Usage of @PrePersist and @PreUpdate in JPA
Solve the ali cloud oss - the original 】 【 exe double-click response can't open, to provide a solution
15.8 the semaphore Unix environment programming chapter 15
tensor.eq() tensor.item() tensor.argmax()
关于页面初始化
深度学习--自编码器(AutoEncoder)
snmp++编译错误问题解决方法
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
类与对象 (下)
libavcodec.dll导致游戏不能运行及explorer关闭
2021-01-11-雪碧图做表情管理器
小程序员的发展计划
Nodejs服务端
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only
Oracle数据库:for update 和for update nowait的区别