【问题标题】:Convert a list of tensors to tensors of tensors pytorch将张量列表转换为张量的张量 pytorch
【发布时间】:2020-08-05 02:55:34
【问题描述】:

我有这个代码:

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 中的张量张量?

【问题讨论】:

  • 这能回答你的问题吗? converting list of tensors to tensors pytorch
  • 感谢您的评论,我看到了这篇文章,但它没有回答我的问题我不想要 1 个大张量,我想要张量的张量。
  • 您认为“张量的张量”与张量中的附加维度有何不同?
  • 使用 tensor.cat,我会得到一个大小为 9 的张量。我想得到一个大小为 (3,3) 的张量
  • 对不起,我草率地链接到错误的答案,我深表歉意。你要找的函数是torch.stack(),这个问题之前在 Stackoverflow 上也有回答,虽然我不能改变我的重复投票。参见例如here。如果您需要更多详细信息,请随时回复。

标签: python python-3.x pytorch


【解决方案1】:

这里有一个解决方案:

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

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 2021-10-11
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 2019-05-23
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多