【问题标题】:* (tuple of Tensors tensors, name dim, Tensor out)*(Tensors 张量的元组,名称 dim,Tensor out)
【发布时间】:2020-07-17 14:22:34
【问题描述】:

假设我有内存列表list_of_tensors = [tensor1, tensor2, tensor3, tensor4]。每个元素都是一个形状为(1, 1, 84, 84) 的pytorch 张量。

我想连接张量列表以获得形状为(4, 1, 84, 84) 的张量。 torch.cat(TT, dim=0) 肯定会允许我这样做。 TT 必须是张量的元组,所以 torch.cat(*list_of_tensors, dim=0)torch.cat((*list_of_tensors), dim=0) 将不起作用。

如何使用list_of_tensorstorch.cat(???, dim=0) 创建形状为(4, 1, 84, 84) 的新张量

【问题讨论】:

  • 请阅读How to Ask。特别是,请在阅读该页面后重写您的标题。目前的一点都不清楚。
  • 在张量列表上使用 torch.cat 也可以。

标签: python pytorch


【解决方案1】:

您可以使用 stack,并使用 squeeze

删除多余的维度
c = (torch.stack(list_of_tensors,dim=1)).squeeze(0)

现在 c.shape 是 (4, 1, 84, 84)

你可以在这里找到解释:https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多