Loss Laplacian#
laplacian
#
| CLASS | DESCRIPTION |
|---|---|
LaplacianLoss |
Graph Laplacian regularization loss. |
| FUNCTION | DESCRIPTION |
|---|---|
laplacian_loss |
Computes regularization based on the graph Laplacian to encourage |
Classes#
LaplacianLoss(reduce: Literal['sum', 'mean', 'trace'] = 'trace', sign: float = 1.0, normalize: bool = True, context_prefix: str | None = None, **kwargs)
#
Bases: Loss
Graph Laplacian regularization loss.
Computes regularization based on the graph Laplacian to encourage smoothness of embeddings over the graph structure: \(L_{lap} = \frac{1}{2} \sum_{kl} K_{kl} \| z_k - z_l \|^2_2 = z^\top L z\), where \(K\) is the adjacency matrix, \(L\) is the graph Laplacian, and \(z\) is the embedding vector.
For multiple dimensions: \(L_{lap} = \text{tr}(Z^T L Z)\), where \(Z\) is the embedding matrix.
| PARAMETER | DESCRIPTION |
|---|---|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
normalize
|
Whether to normalize by the number of nodes.
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
Examples:
>>> loss_fn = LaplacianLoss(reduce="trace", sign=1.0)
>>> # Laplacian matrix
>>> L = torch.tensor([[1.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 1.0]])
>>> # Embedding matrix
>>> Z = torch.randn(3, 2)
>>> context = {"laplacian": L, "embedding": Z}
>>> loss = loss_fn(context)
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute Laplacian regularization loss. |
Source code in spectre/loss/laplacian.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute Laplacian regularization loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Dictionary containing the graph Laplacian and embedding.
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Laplacian regularization loss. |
Source code in spectre/loss/laplacian.py
Functions#
laplacian_loss(K: torch.Tensor, Z: torch.Tensor, reduce: Literal['sum', 'mean', 'trace'] = 'trace', sign: float = 1.0, normalize: bool = True) -> torch.Tensor
#
Computes regularization based on the graph Laplacian to encourage smoothness of embeddings over the graph structure: \(L_{lap} = \frac{1}{2} \sum_{kl} K_{kl} \| z_k - z_l \|^2_2 = z^\top L z\), where \(K\) is the adjacency matrix, \(L\) is the graph Laplacian, and \(z\) is the embedding vector.
For multiple dimensions: \(L_{lap} = \text{tr}(Z^T L Z)\), where \(Z\) is the embedding matrix.
| PARAMETER | DESCRIPTION |
|---|---|
K
|
Graph Laplacian matrix (n_nodes, n_nodes).
TYPE:
|
Z
|
Node embedding matrix (n_nodes, n_features).
TYPE:
|
reduce
|
Reduction method, by default "trace".
TYPE:
|
sign
|
Loss sign multiplier, by default 1.0.
TYPE:
|
normalize
|
Whether to normalize by number of nodes, by default True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Laplacian regularization loss. |