torch_geometric.nn.models.LightGCN
- class LightGCN(num_nodes: int, embedding_dim: int, num_layers: int, alpha: Optional[Union[float, Tensor]] = None, **kwargs)[source]
Bases:
Module来自“LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation”论文的LightGCN模型。
LightGCN通过在底层图上线性传播来学习嵌入,并使用所有层学习到的嵌入的加权和作为最终嵌入\[\textbf{x}_i = \sum_{l=0}^{L} \alpha_l \textbf{x}^{(l)}_i,\]其中每一层的嵌入计算为
\[\mathbf{x}^{(l+1)}_i = \sum_{j \in \mathcal{N}(i)} \frac{1}{\sqrt{\deg(i)\deg(j)}}\mathbf{x}^{(l)}_j.\]提供了两个预测头和训练目标: 链接预测(通过
link_pred_loss()和predict_link())和 推荐(通过recommendation_loss()和recommend())。注意
嵌入根据
edge_index指定的图连接性传播,而排名或链接概率根据edge_label_index指定的边计算。注意
有关使用
LightGCN的示例,请参见 examples/lightgcn.py。- Parameters:
- forward(edge_index: Union[Tensor, SparseTensor], edge_label_index: Optional[Tensor] = None, edge_weight: Optional[Tensor] = None) Tensor[source]
计算节点对的排名。
- Parameters:
edge_index (torch.Tensor 或 SparseTensor) – 指定图连接性的边张量。
edge_label_index (torch.Tensor, optional) – 用于指定要计算排名或概率的节点对的边张量。 如果
edge_label_index设置为None,则将使用edge_index中的所有边。(默认值:None)edge_weight (torch.Tensor, optional) –
edge_index中每条边的权重。(默认值:None)
- Return type:
- get_embedding(edge_index: Union[Tensor, SparseTensor], edge_weight: Optional[Tensor] = None) Tensor[source]
返回图中节点的嵌入。
- Return type:
- predict_link(edge_index: Union[Tensor, SparseTensor], edge_label_index: Optional[Tensor] = None, edge_weight: Optional[Tensor] = None, prob: bool = False) Tensor[source]
预测在
edge_label_index中指定的节点之间的链接。- Parameters:
edge_index (torch.Tensor 或 SparseTensor) – 指定图连接性的边张量。
edge_label_index (torch.Tensor, optional) – 用于指定要计算概率的节点对的边张量。 如果
edge_label_index设置为None,则将使用edge_index中的所有边。(默认值:None)edge_weight (torch.Tensor, optional) –
edge_index中每条边的权重。(默认值:None)
- Return type:
- recommend(edge_index: Union[Tensor, SparseTensor], edge_weight: Optional[Tensor] = None, src_index: Optional[Tensor] = None, dst_index: Optional[Tensor] = None, k: int = 1, sorted: bool = True) Tensor[source]
获取
src_index中节点的前\(k\)个推荐。- Parameters:
edge_index (torch.Tensor 或 SparseTensor) – 指定图连接性的边张量。
edge_weight (torch.Tensor, optional) –
edge_index中每条边的权重。(默认值:None)src_index (torch.Tensor, optional) – 应为其生成推荐的节点索引。 如果设置为
None,则将使用所有节点。 (默认值:None)dst_index (torch.Tensor, optional) – 表示可能的推荐选择的节点索引。 如果设置为
None,将使用所有节点。 (默认值:None)k (int, optional) – 推荐数量。 (默认:
1)
- Return type:
- link_pred_loss(pred: Tensor, edge_label: Tensor, **kwargs) Tensor[source]
通过
torch.nn.BCEWithLogitsLoss计算链接预测目标的模型损失。- Parameters:
pred (torch.Tensor) – 预测结果。
edge_label (torch.Tensor) – 真实边缘标签。
**kwargs (可选) – 底层
torch.nn.BCEWithLogitsLoss损失函数的附加参数。
- Return type:
- recommendation_loss(pos_edge_rank: Tensor, neg_edge_rank: Tensor, node_id: Optional[Tensor] = None, lambda_reg: float = 0.0001, **kwargs) Tensor[source]
通过贝叶斯个性化排序(BPR)损失计算排名目标的模型损失。
注意
pos_edge_rank向量中的第 i 个条目和neg_edge_rank条目中的第 i 个条目必须对应于同一实体(例如,用户)的正边和负边的排名。- Parameters:
pos_edge_rank (torch.Tensor) – 正边缘排名。
neg_edge_rank (torch.Tensor) – 负边排名。
node_id (torch.Tensor) – 用于推导正负边预测的节点索引。 如果设置为
None,将使用所有节点。lambda_reg (int, optional) – 贝叶斯个性化排序(BPR)损失的\(L_2\)正则化强度。 (默认值:
1e-4)**kwargs (可选) – 底层
torch_geometric.nn.models.lightgcn.BPRLoss损失 函数的额外参数。
- Return type: