当前位置:网站首页>torch.cat()函数的官方解释,详解以及例子
torch.cat()函数的官方解释,详解以及例子
2022-08-09 10:43:00 【模糊包】
可以直接看最下面的例子,再回头看前面的解释,就很明白了。
在pytorch
中,常见的拼接函数主要是两个,分别是:
stack()
cat()
一般torch.cat()
是为了把多个tensor
进行拼接而存在的。实际使用中,和torch.stack()
使用场景不同:参考链接torch.stack(),但是本文主要说cat()
。
torch.cat()
和python
中的内置函数cat()
, 在使用和目的上,是没有区别的,区别在于前者操作对象是tensor
。
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)
大家可以复制代码运行一下就会发现其中规律了。
总结
通常用来,把torch.stack
得到tensor
进行拼接而存在的。
边栏推荐
猜你喜欢
cesium加载地图
【 original 】 VMware Workstation implementation Openwrt soft routing, the ESXI, content is very detailed!
StratoVirt 中的虚拟网卡是如何实现的?
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only
今天做了手机播放器的均衡器
Probably 95% of the people are still making PyTorch mistakes
2021-01-11-雪碧图做表情管理器
浅析JWT安全问题
情感分析SowNLP词库
编解码(seq2seq)+注意机制(attention) 详细讲解
随机推荐
15.8 the semaphore Unix environment programming chapter 15
Unix Environment Programming Chapter 14 14.8 Memory Mapped I/O
Unix System Programming Chapter 15 15.2 Pipes
Unix Environment Programming Chapter 15 15.9 Shared Storage
tensor.eq() tensor.item() tensor.argmax()
乘积量化(PQ)
Probably 95% of the people are still making PyTorch mistakes
AQS同步组件-ForkJoin、BlockingQueue阻塞队列解析和用例
TensorFlow:NameError: name ‘input_data’ is not defined
分类预测 | MATLAB实现CNN-LSTM(卷积长短期记忆神经网络)多特征分类预测
常用语言图表库总结
Unix Environment Programming Chapter 14 14.4 I/O Multiplexing
tensorflow实现线性方程的参数调整
Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
UNIX Environment Programming Chapter 15 15.6 XSI IPC
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
通过Doc在MySQL数据库中建表
Win32控件--------------------WM_DRAWITEM消息测试程序
【原创】VMware Workstation实现Openwrt软路由功能,非ESXI,内容非常详细!
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 1 Find programs in the PATH