【问题标题】:Indexing Pytorch tensor索引 Pytorch 张量
【发布时间】:2020-03-28 00:15:00
【问题描述】:

我有一个 Pytorch 代码,它在 for 循环的每次迭代中生成一个 Pytorch 张量,大小相同。我想将这些张量中的每一个分配给一行新张量,其中将包括最后的所有张量。在其他作品中是这样的

for i=1:N:
  X = torch.Tensor([[1,2,3], [3,2,5]])
  #Y is a pytorch tensor
  Y[i] = X

我想知道如何使用 Pytorch 实现这一点。

【问题讨论】:

    标签: pytorch tensor


    【解决方案1】:

    您可以使用torch.cat 连接张量:

    tensors = []
    for i in range(N):
      X = torch.tensor([[1,2,3], [3,2,5]])
      tensors.append(X)
    Y = torch.cat(tensors, dim=0)  # dim 0 is the rows of the tensor
    

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2021-11-04
      • 2020-07-20
      • 2019-09-15
      • 2020-09-26
      • 2021-06-27
      • 2020-12-06
      • 2019-02-05
      • 1970-01-01
      相关资源
      最近更新 更多