LightGCN
¶
LightGCN(
num_nodes: int,
embedding_dim: int,
num_layers: int,
alpha: Optional[Union[float, array, List[float]]] = None,
normalize: bool = True,
)
Bases: Module
The LightGCN model from the "LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation" paper.
LightGCN learns node embeddings by linearly propagating them on the graph, and uses the weighted sum of the embeddings of all layers as the final embedding
Two prediction heads and training objectives are provided: link
prediction (link_pred_loss() and predict_link()) and
recommendation (recommendation_loss() and recommend()).
Embeddings are propagated along edge_index, while scores are computed
for the node pairs of edge_label_index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
The number of nodes in the graph. |
required |
embedding_dim
|
int
|
The size of the node embeddings. |
required |
num_layers
|
int
|
The number of propagation layers. |
required |
alpha
|
Optional[Union[float, array, List[float]]]
|
The weight of each layer's embedding in the final embedding: a
scalar used for every layer, or an array of shape
|
None
|
normalize
|
bool
|
Whether to apply the symmetric normalization
\(1 / \sqrt{\deg(i)\deg(j)}\) when propagating. Default:
|
True
|
Example:
import mlx.core as mx
from mlx_graphs.nn import LightGCN
# users are nodes 0..num_users-1, items the following nodes
model = LightGCN(num_users + num_items, embedding_dim=64, num_layers=3)
def loss_fn(model, edge_index, pos_edges, neg_edges):
label_index = mx.concatenate([pos_edges, neg_edges], axis=1)
rank = model(edge_index, label_index)
pos_rank, neg_rank = mx.split(rank, 2)
return model.recommendation_loss(pos_rank, neg_rank)
top_items = model.recommend(edge_index, src_index=users, dst_index=items, k=10)
References
[1] He et al. LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation
Methods:
| Name | Description |
|---|---|
get_embedding |
Returns the final embedding of every node. |
__call__ |
Computes the scores (dot products of the final embeddings) of |
predict_link |
Predicts links between the node pairs of |
recommend |
Returns the top- |
link_pred_loss |
Binary cross entropy loss of link predictions. |
recommendation_loss |
Bayesian Personalized Ranking (BPR) loss. |
Source code in mlx_graphs/nn/models/lightgcn.py
get_embedding
¶
get_embedding(edge_index: array, edge_weights: Optional[array] = None) -> array
Returns the final embedding of every node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Graph connectivity of shape |
required |
edge_weights
|
Optional[array]
|
Optional weight of each edge. Default: |
None
|
Returns:
| Type | Description |
|---|---|
array
|
An array of shape |
Source code in mlx_graphs/nn/models/lightgcn.py
__call__
¶
__call__(
edge_index: array,
edge_label_index: Optional[array] = None,
edge_weights: Optional[array] = None,
) -> array
Computes the scores (dot products of the final embeddings) of pairs of nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Graph connectivity of shape |
required |
edge_label_index
|
Optional[array]
|
The node pairs to score, of shape
|
None
|
edge_weights
|
Optional[array]
|
Optional weight of each edge of |
None
|
Returns:
| Type | Description |
|---|---|
array
|
One score per pair, shape |
Source code in mlx_graphs/nn/models/lightgcn.py
predict_link
¶
predict_link(
edge_index: array,
edge_label_index: Optional[array] = None,
edge_weights: Optional[array] = None,
prob: bool = False,
) -> array
Predicts links between the node pairs of edge_label_index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Graph connectivity of shape |
required |
edge_label_index
|
Optional[array]
|
The node pairs to score. Default: |
None
|
edge_weights
|
Optional[array]
|
Optional weight of each edge. Default: |
None
|
prob
|
bool
|
Whether to return probabilities instead of 0/1 predictions.
Default: |
False
|
Source code in mlx_graphs/nn/models/lightgcn.py
recommend
¶
recommend(
edge_index: array,
edge_weights: Optional[array] = None,
src_index: Optional[array] = None,
dst_index: Optional[array] = None,
k: int = 1,
sorted: bool = True,
) -> array
Returns the top-k recommendations of the nodes of
src_index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Graph connectivity of shape |
required |
edge_weights
|
Optional[array]
|
Optional weight of each edge. Default: |
None
|
src_index
|
Optional[array]
|
The nodes to recommend for. Default: |
None
|
dst_index
|
Optional[array]
|
The candidate recommendations. Default: |
None
|
k
|
int
|
Number of recommendations. Default: |
1
|
sorted
|
bool
|
Whether to sort the recommendations by decreasing score.
Default: |
True
|
Returns:
| Type | Description |
|---|---|
array
|
The indices of the recommended nodes, shape |
Source code in mlx_graphs/nn/models/lightgcn.py
link_pred_loss
¶
link_pred_loss(pred: array, edge_label: array, **kwargs: Any) -> array
Binary cross entropy loss of link predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
array
|
The predicted scores (logits) |
required |
edge_label
|
array
|
The ground-truth edge labels (0 or 1) |
required |
**kwargs
|
Any
|
Additional arguments of
|
{}
|
Source code in mlx_graphs/nn/models/lightgcn.py
recommendation_loss
¶
recommendation_loss(
pos_edge_rank: array,
neg_edge_rank: array,
node_id: Optional[array] = None,
lambda_reg: float = 0.0001,
) -> array
Bayesian Personalized Ranking (BPR) loss.
The i-th entries of pos_edge_rank and neg_edge_rank must be
the scores of a positive and a negative edge of the same node (e.g.
user).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_edge_rank
|
array
|
Scores of the positive edges, shape |
required |
neg_edge_rank
|
array
|
Scores of the negative edges, shape |
required |
node_id
|
Optional[array]
|
The nodes whose initial embeddings are regularized.
Default: |
None
|
lambda_reg
|
float
|
The \(L_2\) regularization strength
\(\lambda\). Default: |
0.0001
|
Source code in mlx_graphs/nn/models/lightgcn.py
from mlx_graphs.nn import LightGCN · mlx_graphs/nn/models/lightgcn.py