Skip to content

InnerProductDecoder

Bases: Module

The inner product decoder from the "Variational Graph Auto-Encoders" paper.

\[ \sigma(\mathbf{Z}\mathbf{Z}^{\top}) \]

where \(\mathbf{Z} \in \mathbb{R}^{N \times d}\) denotes the latent space produced by the encoder.

Example:

import mlx.core as mx
from mlx_graphs.nn import InnerProductDecoder

decoder = InnerProductDecoder()
z = mx.random.normal((5, 16))
edge_index = mx.array([[0, 1, 2], [1, 2, 3]])
>>> decoder(edge_index, z).shape
(3,)
>>> decoder.forward_all(z).shape
(5, 5)

Methods:

Name Description
__call__

Decodes the latent variables z into edge probabilities for the

forward_all

Decodes the latent variables z into a dense adjacency matrix of

__call__

__call__(edge_index: array, z: array, sigmoid: bool = True) -> array

Decodes the latent variables z into edge probabilities for the node pairs of edge_index.

Parameters:

Name Type Description Default
edge_index array

Node pairs to score, of shape [2, num_pairs]

required
z array

The latent space \(\mathbf{Z}\), shape [num_nodes, dim]

required
sigmoid bool

If False, returns the logits instead of the probabilities. Default: True

True

Returns:

Type Description
array

One probability (or logit) per pair, shape [num_pairs].

Source code in mlx_graphs/nn/models/autoencoder.py
def __call__(
    self, edge_index: mx.array, z: mx.array, sigmoid: bool = True
) -> mx.array:
    """Decodes the latent variables ``z`` into edge probabilities for the
    node pairs of ``edge_index``.

    Args:
        edge_index: Node pairs to score, of shape `[2, num_pairs]`
        z: The latent space :math:`\\mathbf{Z}`, shape `[num_nodes, dim]`
        sigmoid: If ``False``, returns the logits instead of the
            probabilities. Default: ``True``

    Returns:
        One probability (or logit) per pair, shape `[num_pairs]`.
    """
    value = (z[edge_index[0]] * z[edge_index[1]]).sum(axis=1)
    return mx.sigmoid(value) if sigmoid else value

forward_all

forward_all(z: array, sigmoid: bool = True) -> array

Decodes the latent variables z into a dense adjacency matrix of probabilities.

Parameters:

Name Type Description Default
z array

The latent space \(\mathbf{Z}\), shape [num_nodes, dim]

required
sigmoid bool

If False, returns the logits instead of the probabilities. Default: True

True

Returns:

Type Description
array

An array of shape [num_nodes, num_nodes].

Source code in mlx_graphs/nn/models/autoencoder.py
def forward_all(self, z: mx.array, sigmoid: bool = True) -> mx.array:
    """Decodes the latent variables ``z`` into a dense adjacency matrix of
    probabilities.

    Args:
        z: The latent space :math:`\\mathbf{Z}`, shape `[num_nodes, dim]`
        sigmoid: If ``False``, returns the logits instead of the
            probabilities. Default: ``True``

    Returns:
        An array of shape `[num_nodes, num_nodes]`.
    """
    adj = z @ z.T
    return mx.sigmoid(adj) if sigmoid else adj

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