1. ホーム
  2. pytorch

torch.catとtorch.stackの違いについて

2022-02-17 23:11:03
torch.cat(seq, dim=0, out=None) # concatenate the tensors in seq along dim, all tensors must have the same size or be empty, the opposite operations are torch.split() and torch.chunk()
torch.stack(seq, dim=0, out=None) #same as above

#Note: The difference between .cat and .stack is that cat adds the value of an existing dimension, which can be interpreted as a continuation, and stack adds a new dimension, which can be interpreted as a
understand as superposition
>>> a=torch.Tensor([1,2,3])
>>> torch.stack((a,a)).size()
torch.size(2,3)
>>> torch.cat((a,a)).size()
torch.size(6)