当前位置:网站首页>Pytorch 张量列表转换为张量 List of Tensor to Tensor 使用 torch.stack()
Pytorch 张量列表转换为张量 List of Tensor to Tensor 使用 torch.stack()
2022-07-25 09:27:00 【Haulyn5】
比如我现在有一个 List 每个元素是一个 shape 相同的 Tensor,我想将它们连接成一个统一的 Tensor。
a=b=c= torch.rand([4,5])
list_of_tensor = [a,b,c]
print(list_of_tensor)输出:
[tensor([[2.1911e-01, 4.8939e-01, 5.1264e-01, 4.2860e-01, 4.2832e-01],
[5.5072e-01, 9.0650e-01, 9.4573e-01, 2.9587e-01, 3.8711e-01],
[8.3788e-01, 1.6358e-01, 3.9210e-01, 4.1913e-01, 4.8324e-01],
[8.8101e-01, 8.8954e-04, 9.5448e-01, 9.0539e-01, 3.2410e-01]]), tensor([[2.1911e-01, 4.8939e-01, 5.1264e-01, 4.2860e-01, 4.2832e-01],
[5.5072e-01, 9.0650e-01, 9.4573e-01, 2.9587e-01, 3.8711e-01],
[8.3788e-01, 1.6358e-01, 3.9210e-01, 4.1913e-01, 4.8324e-01],
[8.8101e-01, 8.8954e-04, 9.5448e-01, 9.0539e-01, 3.2410e-01]]), tensor([[2.1911e-01, 4.8939e-01, 5.1264e-01, 4.2860e-01, 4.2832e-01],
[5.5072e-01, 9.0650e-01, 9.4573e-01, 2.9587e-01, 3.8711e-01],
[8.3788e-01, 1.6358e-01, 3.9210e-01, 4.1913e-01, 4.8324e-01],
[8.8101e-01, 8.8954e-04, 9.5448e-01, 9.0539e-01, 3.2410e-01]])]
使用 torch.stack() 来将它们堆叠为一个 Tensor。
tensor_all = torch.stack(list_of_tensor)
print(tensor_all)
print(tensor_all.shape)输出:
tensor([[[2.1911e-01, 4.8939e-01, 5.1264e-01, 4.2860e-01, 4.2832e-01],
[5.5072e-01, 9.0650e-01, 9.4573e-01, 2.9587e-01, 3.8711e-01],
[8.3788e-01, 1.6358e-01, 3.9210e-01, 4.1913e-01, 4.8324e-01],
[8.8101e-01, 8.8954e-04, 9.5448e-01, 9.0539e-01, 3.2410e-01]],
[[2.1911e-01, 4.8939e-01, 5.1264e-01, 4.2860e-01, 4.2832e-01],
[5.5072e-01, 9.0650e-01, 9.4573e-01, 2.9587e-01, 3.8711e-01],
[8.3788e-01, 1.6358e-01, 3.9210e-01, 4.1913e-01, 4.8324e-01],
[8.8101e-01, 8.8954e-04, 9.5448e-01, 9.0539e-01, 3.2410e-01]],
[[2.1911e-01, 4.8939e-01, 5.1264e-01, 4.2860e-01, 4.2832e-01],
[5.5072e-01, 9.0650e-01, 9.4573e-01, 2.9587e-01, 3.8711e-01],
[8.3788e-01, 1.6358e-01, 3.9210e-01, 4.1913e-01, 4.8324e-01],
[8.8101e-01, 8.8954e-04, 9.5448e-01, 9.0539e-01, 3.2410e-01]]])
torch.Size([3, 4, 5])参考:
torch.stack — PyTorch 1.12 documentation
https://pytorch.org/docs/stable/generated/torch.stack.html?highlight=torch%20stack#torch.stack
How to turn a list of tensor to tensor? - #4 by dmadeka1 - PyTorch Forums
https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/4
边栏推荐
- C3D模型pytorch源码逐句详析(一)
- T5 paper summary
- CCF 201509-2 date calculation
- Introduction to arm GIC
- UE4 框架介绍
- Detailed explanation of MySQL database
- 记录一些JS工具函数
- JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
- Common methods of nodejs version upgrade or switching
- Nodejs初体验
猜你喜欢

概率机器人学习笔记第二章

CCF 201503-4 network delay
![[RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)](/img/6e/da80133e05b18c87d7167c023b6c93.gif)
[RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)

Advanced introduction to digital IC Design SOC

js加密参数定位

mysql历史数据补充新数据

GCD详解

Use and principle of rest

线程池的死锁事件
![[nearly 10000 words dry goods] don't let your resume don't match your talent -- teach you to make the most suitable resume by hand](/img/2d/e3a326175f04826b9d9c96baedc3a5.png)
[nearly 10000 words dry goods] don't let your resume don't match your talent -- teach you to make the most suitable resume by hand
随机推荐
小程序分享功能
Pnpm Brief
Introduction to armv8 general timer
JSP details
看一个双非二本(0实习)大三学生如何拿到阿里、腾讯的offer
nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE
ThreadLocal&Fork/Join
Detailed explanation of JDBC operation database
Introduction to arm GIC
Summary of most consistency problems
framework打包合并脚本
JSONObject解析json格式的终极总结
Advanced introduction to digital IC Design SOC
File upload function
SSM integration (simple library management system to integrate SSM)
[deployment of deep learning model] deploy the deep learning model using tensorflow serving + tornado
mysql 解决不支持中文的问题
@Import,Conditional和@ImportResourse注解
yarn速查手册
一文学会,三款黑客必备的抓包工具教学