为了账号安全,请及时绑定邮箱和手机立即绑定

将张量列表转换为张量的张量 pytorch

将张量列表转换为张量的张量 pytorch

繁星点点滴滴 2022-10-18 19:49:33
我有这个代码:import torchlist_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]tensor_of_tensors = torch.tensor(list_of_tensors)我收到错误消息:ValueError:只有一个元素张量可以转换为 Python 标量如何将张量列表转换为 pytorch 中的张量张量?
查看完整描述

2 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

您还可以将火炬张量类型转换为 NumPy 数组,然后将它们转换为张量


list_of_tensors = [torch.randn(3).numpy(),torch.randn(3).numpy(),torch.randn(3).numpy()]

tensor_of_tensors = torch.tensor(list_of_tensors)


查看完整回答
反对 回复 2022-10-18
?
慕村225694

TA贡献1880条经验 获得超4个赞

这是一个解决方案:

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


查看完整回答
反对 回复 2022-10-18
  • 2 回答
  • 0 关注
  • 185 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号