diff torch.stack et torch.cat
- User
diff torch.stack et torch.cat
- Assistant
En PyTorch,
torch.stackettorch.catsont deux opérations utilisées pour combiner plusieurs tenseurs, mais elles fonctionnent différemment :torch.cat- Concaténation de tenseurs le long d'une dimension existante.
- Les tenseurs doivent avoir la même forme sauf sur la dimension le long de laquelle on concatène.
- User
c = torch.cat((a, b), dim=0) ou c = torch.cat([a, b)], dim=0) ?
- Assistant
La bonne syntaxe pour appeler
torch.catest :pythonc = torch.cat((a, b), dim=0)ou
pythonc = torch.cat([a, b], dim=0)En effet, tu peux passer un tuple
(a, b)ou une liste[a, b]des tenseurs à concaténer.Par contre, la syntaxe que tu as écrite ici :
pythonc = torch.cat([a, b)], dim=0)contient une parenthèse fermante en trop après le crochet
]qui provoquera une erreur de syntaxe.Résumé
-
Correct :
torch.cat((a, b), dim=0)→ tupletorch.cat([a, b], dim=0)→ liste
-
Incorrect :
torch.cat([a, b)], dim=0)→ parenthèse fermante en trop
Donc, utilise plutôt :
pythonc = torch.cat([a, b], dim=0)ou
python -