将张量列表转换为张量的张量 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 标量


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


繁星点点滴滴
浏览 186回答 2
2回答

互换的青春

您还可以将火炬张量类型转换为 NumPy 数组,然后将它们转换为张量list_of_tensors = [torch.randn(3).numpy(),torch.randn(3).numpy(),torch.randn(3).numpy()]tensor_of_tensors = torch.tensor(list_of_tensors)

慕村225694

这是一个解决方案:tensor_of_tensors = torch.stack((list_of_tensors)) print(tensor_of_tensors) #shape (3,3)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python