Vue d'ensemble

PyTorch 1.12, publié le 29 juin 2022, améliore l'API fonctionnelle et étend le support de functorch.

Fonctionnalités principales

API fonctionnelle améliorée

L'API fonctionnelle de PyTorch est enrichie avec de nouvelles transformations et une meilleure intégration avec torch.nn.functional.

python
import torch
import torch.nn.functional as F

# API fonctionnelle
x = torch.randn(2, 3, 4)
weights = torch.randn(5, 4)
bias = torch.randn(5)

output = F.linear(x, weights, bias)
print(output.shape)  # torch.Size([2, 3, 5])

# Activation et normalisation fonctionnelles
activated = F.gelu(output)
normalized = F.layer_norm(activated, [5])

functorch étendu

functorch est étendu avec jacrev et jacfwd pour le calcul de jacobiennes, et hessian pour les matrices hessiennes.

python
import torch
from functorch import jacrev, vmap

# Calcul de jacobienne
def f(x):
    return torch.stack([x.sum(), x.prod()])

x = torch.tensor([1.0, 2.0, 3.0])
jacobian = jacrev(f)(x)
print(jacobian.shape)  # (2, 3)

# Jacobienne par batch
batch_x = torch.randn(5, 3)
batch_jac = vmap(jacrev(f))(batch_x)
print(batch_jac.shape)  # (5, 2, 3)

Sources