索引 Pytorch 张量

Indexing Pytorch tensor

我有一个 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 实现它。

您可以使用 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