繁体   English   中英

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

[英]Convert a list of tensors to tensors of tensors 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 中的张量张量?

这是一个解决方案:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM