Fonction torch.unbind de PyTorch
Manuel de référence Pytorch torch
torch.unbindIl s'agit d'une fonction PyTorch utilisée pour diviser un tenseur en un tuple selon une dimension spécifiée.
Définition de fonction
torch.unbind(input, dim=0)
Exemple d'utilisation
Exemple
import torch
x = torch.arange(12).reshape(3, 4)
print(Tenseur original :)
print(x)
result = torch.unbind(x, dim=0)
print(Découpage selon dim=0 :)
for i, t in enumerate(result):
print(fBloc {i} : {t})
result = torch.unbind(x, dim=1)
print(Découpage selon dim=1 :)
for i, t in enumerate(result):
print(fBloc {i} : {t})
x = torch.arange(12).reshape(3, 4)
print(Tenseur original :)
print(x)
result = torch.unbind(x, dim=0)
print(Découpage selon dim=0 :)
for i, t in enumerate(result):
print(fBloc {i} : {t})
result = torch.unbind(x, dim=1)
print(Découpage selon dim=1 :)
for i, t in enumerate(result):
print(fBloc {i} : {t})
Le résultat de sortie est:
原始张量:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
沿 dim=0 分割:
块 0: tensor([0, 1, 2, 3])
Chunk 1: tensor([4, 5, 6, 7])
Chunk 2: tensor([ 8, 9, 10, 11])
沿 dim=1 分割:
块 0: tensor([0, 4, 8])
Chunk 1: tensor([1, 5, 9])
Chunk 2: tensor([ 2, 6, 10])
Chunk 3: tensor([ 3, 7, 11])
Autres extensions