1
votes

I have a Pytorch code which generates a Pytorch tensor in each iteration of for loop, all of the same size. I want to assign each of those tensors to a row of new tensor, which will include all the tensors at the end. In other works something like this

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

I wonder how I can implement this with Pytorch.

1

1 Answers

0
votes

You can concatenate the tensors using 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