当前位置:网站首页>The torch. The stack () official explanation, explanation and example
The torch. The stack () official explanation, explanation and example
2022-08-09 10:46:00 【Fuzzy Pack】
可以直接看最下面的【3.例子】,再回头看前面的解释
在pytorch
中,常见的拼接函数主要是两个,分别是:
stack()
cat()
实际使用中,These two functions complement each other,使用场景不同:关于cat()
参考torch.cat(),但是本文主要说stack()
.
函数的意义:使用stack
可以保留两个信息:[1. 序列] 和 [2. 张量矩阵] 信息,属于【扩张再拼接】的函数.
形象的理解:假如数据都是二维矩阵(平面),它可以把这些一个个平面按第三维(例如:时间序列)压成一个三维的立方体,而立方体的长度就是时间序列长度.
This function often appears in natural language processing(NLP
)和图像卷积神经网络(CV
)中.
1. stack()
官方解释:沿着一个新维度对输入张量序列进行连接. 序列中所有的张量都应该为相同形状.
浅显说法:把多个2维的张量凑成一个3维的张量;多个3维的凑成一个4维的张量…以此类推,也就是在增加新的维度进行堆叠.
outputs = torch.stack(inputs, dim=?) → Tensor
参数
inputs : 待连接的张量序列.
注:python
的序列数据只有list
和tuple
.dim : 新的维度, 必须在
0
到len(outputs)
之间.
注:len(outputs)
是生成数据的维度大小,也就是outputs
的维度值.
2. 重点
- 函数中的输入
inputs
只允许是序列;且序列内部的张量元素,必须shape
相等
----举例:[tensor_1, tensor_2,..]
或者(tensor_1, tensor_2,..)
,且必须tensor_1.shape == tensor_2.shape
dim
是选择生成的维度,必须满足0<=dim<len(outputs)
;len(outputs)
是输出后的tensor
的维度大小
See the example if you don't understand,Looking back, you will understand.
3. 例子
1.准备2个tensor
数据,每个的shape
都是[3,3]
# 假设是时间步T1的输出
T1 = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# 假设是时间步T2的输出
T2 = torch.tensor([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
2.测试stack函数
print(torch.stack((T1,T2),dim=0).shape)
print(torch.stack((T1,T2),dim=1).shape)
print(torch.stack((T1,T2),dim=2).shape)
print(torch.stack((T1,T2),dim=3).shape)
# outputs:
torch.Size([2, 3, 3])
torch.Size([3, 2, 3])
torch.Size([3, 3, 2])
'选择的dim>len(outputs),所以报错'
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)
You can copy the code and try it out:拼接后的tensor
形状,会根据不同的dim
发生变化.
dim | shape |
---|---|
0 | [2, 3, 3] |
1 | [3, 2, 3] |
2 | [3, 3, 2] |
3 | 溢出报错 |
4. 总结
函数作用:
函数stack()
对序列数据Internal tensors are performed扩维拼接,The specified dimension is chosen by the programmer、Size is the dimension interval of the generated data.存在意义:
In Natural Language Processing and Convolutional and Neural Networks, Usually for reservations–[序列(先后)信息] 和 [Matrix information for the tensor] 才会使用stack
.
函数存在意义?》》》
手写过RNN的同学,Know that the output data in a recurrent neural network is:一个list
,The list is insertedseq_len
个形状是[batch_size, output_size]
的tensor
,不利于计算,需要使用stack
进行拼接,保留–[1.seq_lenthis time step]和–[2.张量属性[batch_size, output_size]
].
边栏推荐
- shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
- 关于anaconda中conda下载包或者pip下载包很慢的原因,加速下载包的方法(无视anaconda版本和环境)
- torch.stack()的官方解释,详解以及例子
- 山东招远通报星童幼儿园食品安全问题最新调查情况
- TensorFlow:NameError: name ‘input_data’ is not defined
- BERT预训练模型(Bidirectional Encoder Representations from Transformers)-原理详解
- jmeter BeanShell 后置处理器
- 上传张最近做的E2用的xmms的界面的截图
- AQS同步组件-ForkJoin、BlockingQueue阻塞队列解析和用例
- shap库源码和代码实现
猜你喜欢
Cpolar内网穿透的面板功能介绍
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
[Error record] Solve the problem that ASRock J3455-ITX cannot be turned on without a monitor plugged in
相关系数计算,热力图绘制,代码实现
强化学习 (Reinforcement Learning)
自从我使用HiFlow场景连接器后,在也不用担心成为“落汤鸡”了
深度学习--自编码器(AutoEncoder)
jmeter BeanShell 后置处理器
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
2022年台湾省矢量数据(点线面)及数字高程数据下载
随机推荐
学习NET-SNMP之二-----------VisualStudio编译Net-SNMP
Win7 远程桌面限制IP
tensorflow和numpy对应的版本,报FutureWarning: Passing (type, 1) or ‘1type‘ as a synonym of type is deprecate
2022年台湾省矢量数据(点线面)及数字高程数据下载
AQS同步组件-ForkJoin、BlockingQueue阻塞队列解析和用例
关于页面初始化
faster-rcnn中的RPN原理
1001 害死人不偿命的(3n+1)猜想 (15 分)
Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
Probably 95% of the people are still making PyTorch mistakes
[Error record] Solve the problem that ASRock J3455-ITX cannot be turned on without a monitor plugged in
TensorFlow:NameError: name ‘input_data’ is not defined
上传张最近做的E2用的xmms的界面的截图
PoseNet: A Convolutional Network for Real-Time 6-DOF Camera Relocalization论文阅读
乘积量化(PQ)
jmeter BeanShell 后置处理器
Unix Environment Programming Chapter 14 14.8 Memory Mapped I/O
torch.stack()的官方解释,详解以及例子
unix系统编程 第十五章 15.2管道
【报错记录】解决华擎J3455-ITX不插显示器无法开机的问题