PyTorch torch.cat Fonction
Manuel de référence Pytorch torch
torch.catest une fonction de PyTorch utilisée pour concaténer plusieurs tenseurs le long d'une dimension spécifiée. Elle concatène plusieurs tenseurs le long de la dimension spécifiée en un tenseur concaténé.
Il s'agit d'une opération très courante en apprentissage profond, par exemple dans des scénarios tels que la concaténation de cartes de caractéristiques et la fusion de lots de données.
Définition de fonction
torch.cat(tensors, dim=0, out=None)
Parameters:
tensors(Séquence de Tensor) : La séquence de tenseurs à concaténer. Tous les tenseurs doivent avoir la même forme dans toutes les dimensions sauf la dimension de concaténation.dim(int, optionnel) : La dimension le long de laquelle concaténer. Par défaut : 0.out(Tensor, optionnel) : Le tenseur de sortie.
Valeur de retour:
torch.Tensor: Renvoie le tenseur concaténé.
Exemples d'utilisation
Exemple 1 : Concaténer le long de la première dimension
Exemple
import torch
# Créer deux tenseurs
a = torch.randn(2, 3)
b = torch.randn(2, 3)
# Concaténer selon la première dimension (lignes)
c = torch.cat([a, b], dim=0)
print("forme de a :", a.shape)
print("forme de b :", b.shape)
print("forme de c :", c.shape)
print(c)
# Créer deux tenseurs
a = torch.randn(2, 3)
b = torch.randn(2, 3)
# Concaténer selon la première dimension (lignes)
c = torch.cat([a, b], dim=0)
print("forme de a :", a.shape)
print("forme de b :", b.shape)
print("forme de c :", c.shape)
print(c)
Le résultat de sortie est:
a 的形状: torch.Size([2, 3])
b 的形状: torch.Size([2, 3])
c 的形状: torch.Size([4, 3])
tensor([[ 0.2532, 0.3643, 0.5341],
[ 0.9578, 0.9086, -0.2847],
[-0.7108, -0.0142, 0.7168],
[-0.1542, -0.9841, -1.4945]])
Exemple 2 : Concaténer selon la deuxième dimension
Exemple
import torch
# Créer deux tenseurs
a = torch.randn(2, 3)
b = torch.randn(2, 4)
# Concaténer selon la deuxième dimension (colonnes)
c = torch.cat([a, b], dim=1)
print("forme de a :", a.shape)
print("forme de b :", b.shape)
print("forme de c :", c.shape)
# Créer deux tenseurs
a = torch.randn(2, 3)
b = torch.randn(2, 4)
# Concaténer selon la deuxième dimension (colonnes)
c = torch.cat([a, b], dim=1)
print("forme de a :", a.shape)
print("forme de b :", b.shape)
print("forme de c :", c.shape)
Le résultat de sortie est:
a 的形状: torch.Size([2, 3]) b 的形状: torch.Size([2, 4]) c 的形状: torch.Size([2, 7])
Exemple 3 : Concaténer plusieurs tenseurs
Exemple
import torch
# Créer plusieurs tenseurs
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])
c = torch.tensor([5, 6])
# Concaténer plusieurs tenseurs
result = torch.cat([a, b, c])
print(result)
# Créer plusieurs tenseurs
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])
c = torch.tensor([5, 6])
# Concaténer plusieurs tenseurs
result = torch.cat([a, b, c])
print(result)
Le résultat de sortie est:
tensor([1, 2, 3, 4, 5, 6]) </p> <h3>示例 4: 在神经网络中拼接特征</h3> <div class="example"> <h2 class="example">实例</h2> <div class="example_code"> <span style="color: Green;font-weight:bold;">import</span> torch<br /> <br /> <span style="color: #a50"># 模拟来自不同层的特征图</span><br /> feature1 <span style="color: Gray;">=</span> torch.<span style="color: #05a;">randn</span><span style="color: Olive;">(</span><span style="color: Maroon;">1</span><span style="color: Gray;">,</span> <span style="color: Maroon;">64</span><span style="color: Gray;">,</span> <span style="color: Maroon;">32</span><span style="color: Gray;">,</span> <span style="color: Maroon;">32</span><span style="color: Olive;">)</span> <span style="color: #a50"># 来自第一层的特征</span><br /> feature2 <span style="color: Gray;">=</span> torch.<span style="color: #05a;">randn</span><span style="color: Olive;">(</span><span style="color: Maroon;">1</span><span style="color: Gray;">,</span> <span style="color: Maroon;">128</span><span style="color: Gray;">,</span> <span style="color: Maroon;">32</span><span style="color: Gray;">,</span> <span style="color: Maroon;">32</span><span style="color: Olive;">)</span> <span style="color: #a50"># 来自第二层的特征</span><br /> <br /> <span style="color: #a50"># 在通道维度(dim=1)拼接特征</span><br /> combined <span style="color: Gray;">=</span> torch.<span style="color: #05a;">cat</span><span style="color: Olive;">(</span><span style="color: Olive;">[</span>feature1<span style="color: Gray;">,</span> feature2<span style="color: Olive;">]</span><span style="color: Gray;">,</span> dim<span style="color: Gray;">=</span><span style="color: Maroon;">1</span><span style="color: Olive;">)</span><br /> <br /> <span style="color: Green;font-weight:bold;">print</span><span style="color: Olive;">(</span><span style="color: #a11;">"特征1 形状:"</span><span style="color: Gray;">,</span> feature1.<span style="color: #05a;">shape</span><span style="color: Olive;">)</span><br /> <span style="color: Green;font-weight:bold;">print</span><span style="color: Olive;">(</span><span style="color: #a11;">"特征2 形状:"</span><span style="color: Gray;">,</span> feature2.<span style="color: #05a;">shape</span><span style="color: Olive;">)</span><br /> <span style="color: Green;font-weight:bold;">print</span><span style="color: Olive;">(</span><span style="color: #a11;">"拼接后形状:"</span><span style="color: Gray;">,</span> combined.<span style="color: #05a;">shape</span><span style="color: Olive;">)</span><br /> </div> </div> <p>输出结果为:</p> <pre> 特征1 形状: torch.Size([1, 64, 32, 32]) 特征2 形状: torch.Size([1, 128, 32, 32]) 拼接后形状: torch.Size([1, 192, 32, 32])
Dans les réseaux de neurones,torch.catil est couramment utilisé dans des structures telles que les Feature Pyramid Networks (FPN) pour fusionner des caractéristiques de différentes couches.
Différence entre torch.cat et torch.stack
torch.cat: Concatène selon les dimensions existantes ; les formes des tenseurs s'additionnent selon la dimension de concaténation.torch.stack: Empile selon une nouvelle dimension, ajoutant une nouvelle dimension.
Exemple
import torch
a = torch.randn(2, 3)
b = torch.randn(2, 3)
# Différence entre cat et stack
cat_result = torch.cat([a, b], dim=0)
stack_result = torch.stack([a, b], dim=0)
print("forme du résultat de cat :", cat_result.shape) # (4, 3)
print("forme du résultat de stack :", stack_result.shape) # (2, 2, 3)
a = torch.randn(2, 3)
b = torch.randn(2, 3)
# Différence entre cat et stack
cat_result = torch.cat([a, b], dim=0)
stack_result = torch.stack([a, b], dim=0)
print("forme du résultat de cat :", cat_result.shape) # (4, 3)
print("forme du résultat de stack :", stack_result.shape) # (2, 2, 3)
Autres extensions