将张量列表转换为张量的张量 pytorch [英] Convert a list of tensors to tensors of tensors pytorch

查看:86
本文介绍了将张量列表转换为张量的张量 pytorch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

import torch

list_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]
tensor_of_tensors = torch.tensor(list_of_tensors)

我收到错误:

ValueError: 只有一个元素张量可以转换为 Python 标量

ValueError: only one element tensors can be converted to Python scalars

如何将张量列表转换为 pytorch 中的张量张量?

How can I convert the list of tensors to a tensor of tensors in pytorch?

推荐答案

这里有一个解决方案:

tensor_of_tensors = torch.stack((list_of_tensors))
print(tensor_of_tensors) #shape (3,3)

这篇关于将张量列表转换为张量的张量 pytorch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆