Skip to content

VGAE

VGAE(encoder: Module, decoder: Optional[Module] = None)

Bases: GAE

The Variational Graph Auto-Encoder model from the "Variational Graph Auto-Encoders" paper.

The encoder returns a tuple (mu, logstd). In training mode, encode() samples \(\mathbf{Z} = \mu + \epsilon \cdot \exp(\log\sigma)\); in evaluation mode it returns \(\mu\).

Parameters:

Name Type Description Default
encoder Module

The encoder module computing \(\mu\) and \(\log\sigma\).

required
decoder Optional[Module]

The decoder module. Default: InnerProductDecoder

None

Example:

def loss_fn(model, edge_index, node_features):
    z = model.encode(edge_index, node_features)
    loss = model.recon_loss(z, edge_index)
    return loss + model.kl_loss() / num_nodes

Methods:

Name Description
reparametrize

Samples latent variables in training mode, returns mu otherwise.

encode

Runs the encoder and samples the latent node embeddings.

kl_loss

KL divergence between the latent distribution and a unit Gaussian.

Source code in mlx_graphs/nn/models/autoencoder.py
def __init__(self, encoder: nn.Module, decoder: Optional[nn.Module] = None):
    super().__init__()
    self.encoder = encoder
    self.decoder = InnerProductDecoder() if decoder is None else decoder

reparametrize

reparametrize(mu: array, logstd: array) -> array

Samples latent variables in training mode, returns mu otherwise.

Source code in mlx_graphs/nn/models/autoencoder.py
def reparametrize(self, mu: mx.array, logstd: mx.array) -> mx.array:
    """Samples latent variables in training mode, returns ``mu`` otherwise."""
    if self.training:
        return mu + mx.random.normal(logstd.shape) * mx.exp(logstd)
    return mu

encode

encode(*args: Any, **kwargs: Any) -> array

Runs the encoder and samples the latent node embeddings.

Source code in mlx_graphs/nn/models/autoencoder.py
def encode(self, *args: Any, **kwargs: Any) -> mx.array:
    """Runs the encoder and samples the latent node embeddings."""
    mu, logstd = self.encoder(*args, **kwargs)
    logstd = mx.minimum(logstd, MAX_LOGSTD)
    self._mu, self._logstd = mu, logstd
    return self.reparametrize(mu, logstd)

kl_loss

kl_loss(mu: Optional[array] = None, logstd: Optional[array] = None) -> array

KL divergence between the latent distribution and a unit Gaussian.

Parameters:

Name Type Description Default
mu Optional[array]

\(\mu\). If None, uses the one of the last call to encode(). Default: None

None
logstd Optional[array]

\(\log\sigma\). If None, uses the one of the last call to encode(). Default: None

None

Returns:

Type Description
array

The scalar loss, averaged over the nodes.

Source code in mlx_graphs/nn/models/autoencoder.py
def kl_loss(
    self, mu: Optional[mx.array] = None, logstd: Optional[mx.array] = None
) -> mx.array:
    """KL divergence between the latent distribution and a unit Gaussian.

    Args:
        mu: :math:`\\mu`. If ``None``, uses the one of the last call to
            :meth:`encode`. Default: ``None``
        logstd: :math:`\\log\\sigma`. If ``None``, uses the one of the last
            call to :meth:`encode`. Default: ``None``

    Returns:
        The scalar loss, averaged over the nodes.
    """
    mu = self._mu if mu is None else mu
    logstd = self._logstd if logstd is None else mx.minimum(logstd, MAX_LOGSTD)
    return -0.5 * mx.mean(
        mx.sum(1 + 2 * logstd - mu**2 - mx.exp(logstd) ** 2, axis=1)
    )

from mlx_graphs.nn import VGAE · mlx_graphs/nn/models/autoencoder.py