LightGCN pytorch 原始碼筆記
研究所社群媒體探勘的期末作業,分析 PTT 的推薦系統,使用 LightGCN 作為主網路
keywords: LightGCN
程式簡介
使用 MovieLens (small) 資料集。由 9,000 個電影及 600 個使用者建立出 100,000 個評價 (edge)
import data
這一步在把所有會用到的套件 import 進來,一共四個部份。雜七雜八套件、sklearn 分資料集、Pytorch、Pytorch Geometric 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21# import required modules
import random
from tqdm import tqdm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# import one line split data set
from sklearn.model_selection import train_test_split
import torch
from torch import nn, optim, Tensor
from torch_sparse import SparseTensor, matmul
# import Pytorch Geometric
from torch_geometric.utils import structured_negative_sampling
from torch_geometric.data import download_url, extract_zip
from torch_geometric.nn.conv.gcn_conv import gcn_norm
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.typing import Adj
資料集前處理
一共有四個處理:下載資料、讀取 node 資料、讀取 edge 資料、分 Training、Testing、Validation 資料、轉換為 SparseTensor
1 | # download the dataset |
1 | # load user and movie nodes |
1 | # load edges between users and movies |
1 | # split the edges of the graph using a 80/10/10 train/validation/test split |
1 | # convert edge indices into Sparse Tensors: https://pytorch-geometric.readthedocs.io/en/latest/notes/sparse_tensor.html |
轉換為最右的表示法
1 | # function which random samples a mini-batch of positive and negative samples |

LightGCN Model
1 | # defines LightGCN model |

1 | def bpr_loss(users_emb_final, users_emb_0, pos_items_emb_final, pos_items_emb_0, neg_items_emb_final, neg_items_emb_0, lambda_val): |
\[ \begin{equation} L_{BPR} = -\sum_{u = 1}^M \sum_{i \in N_u} \sum_{j \notin N_u} \ln{\sigma(\hat{y}_{ui} - \hat{y}_{uj})} + \lambda ||E^{(0)}||^2 \end{equation} \] ### Reference