Skip to content

SVDFeatureReduction

SVDFeatureReduction(out_channels: int)

Bases: BaseTransform

Reduces the dimension of the node features with a Singular Value Decomposition, as PyG's SVDFeatureReduction.

If the node features \(\mathbf{X}\) have more than out_channels columns, they are replaced by \(\mathbf{U}_{:, :k} \mathrm{diag}(\mathbf{S}_{:k})\), where \(\mathbf{X} = \mathbf{U} \mathrm{diag}(\mathbf{S}) \mathbf{V}^\top\) and \(k\) is out_channels. The SVD is computed on the host with NumPy. Singular vectors are only defined up to their sign, so columns may have the opposite sign of PyG's.

Parameters:

Name Type Description Default
out_channels int

Number of node features after the reduction.

required

Example:

graph = GraphData(
    edge_index=mx.array([[0, 1], [1, 2]]),
    node_features=mx.random.normal((3, 8)),
)
graph = SVDFeatureReduction(2)(graph)
graph.node_features.shape
# (3, 2)

Methods:

Name Description
process

Reduces the node features of data.

Source code in mlx_graphs/transforms/spectral.py
def __init__(self, out_channels: int):
    self.out_channels = out_channels

process

process(data: GraphData) -> GraphData

Reduces the node features of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the reduced node_features.

Source code in mlx_graphs/transforms/spectral.py
def process(self, data: GraphData) -> GraphData:
    """Reduces the node features of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the reduced ``node_features``.
    """
    features = data.node_features
    if features is None:
        raise ValueError("SVDFeatureReduction requires node features")
    if features.shape[-1] > self.out_channels:
        matrix = np.asarray(features.astype(mx.float32)).astype(np.float64)
        u, s, _ = np.linalg.svd(matrix, full_matrices=False)
        k = self.out_channels
        reduced = u[:, :k] * s[:k]
        data.node_features = mx.array(reduced.astype(np.float32))
    return data

from mlx_graphs.transforms import SVDFeatureReduction · mlx_graphs/transforms/spectral.py