当前位置:网站首页>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()是为了把多个tensorexist 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得到tensorexist for splicing.
边栏推荐
猜你喜欢

相关系数计算,热力图绘制,代码实现

备战金三银四:如何成功拿到阿里offer(经历+面试题+如何准备)

2021-01-11-雪碧图做表情管理器

Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only

pytorch widedeep文档

人物 | 从程序员到架构师,我是如何快速成长的?

MNIST机器学习入门

CSDN的markdown编辑器语法完整大全

Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 1 Find programs in the PATH
一天半的结果——xmms on E2
随机推荐
分类预测 | MATLAB实现CNN-LSTM(卷积长短期记忆神经网络)多特征分类预测
学习NET-SNMP之二-----------VisualStudio编译Net-SNMP
解决1.tensorflow运行使用CPU不使用GPU 2.tensorflow环境下的GPU版本号 3.tensorflow和cuda以及cudnn版本对应问题 4.查看cuda和cudnn版本
批量转换经纬度的网页实现方法
深度学习--生成对抗网络(Generative Adversarial Nets)
numpy库中的函数 bincount() where() diag() all()
pip常见命令和更改源文件
10000以内素数表(代码块)
Transformer+Embedding+Self-Attention原理详解
xmms已经发布到v1.3了,好久没写博客了
Unix Environment Programming Chapter 15 15.3 Functions popen and pclose
TELNET协议相关RFC
按键精灵之输出文本
[华为云在线课程][SQL语法分类][数据操作][学习笔记]
cnn的输入输出
【原创】VMware Workstation实现Openwrt软路由功能,非ESXI,内容非常详细!
unix环境编程 第十四章 14.8 存储映射I/O
编解码(seq2seq)+注意机制(attention) 详细讲解
一键完成物联网产品注册,快速体验在线调试设备
autogluon安装,使用指南,代码