lightgcn-pytorch

repository·master·Indexed 22 days ago

https://github.com/gusye1234/lightgcn-pytorch

A PyTorch implementation of LightGCN, a simplified Graph Convolutional Network designed for recommendation tasks via collaborative filtering. The project includes a registry system for extending datasets and models, support for BPR loss, and integration with TensorBoard for monitoring. It provides examples for running the model on datasets such as Gowalla and Amazon Books.

Tokens
1.3K
Snippets
6
Records
9
Agent score
77%

What's inside lightgcn-pytorch

  1. Optimize training and testing performance

    master

    Use these tips to improve execution speed:

    • Avoid Matrix Splitting: Do not enable the code that splits the user-item matrix for matrix multiplication, as it significantly slows down training.
    • Speed up Testing: Increase the testbatch size and enable the multicore option.
      • Warning: The multicore option may cause issues on Windows systems.
    • Visualization: Use the tensorboard option for better monitoring.
  2. Extend LightGCN with custom datasets, models, or sampling

    master

    The project is designed to be extensible through inheritance and registration:

    • Custom Datasets: Implement a dataloader by inheriting from BasicDataset in dataloader.py, then register it in register.py.
    • Custom Models: Implement a model by inheriting from BasicModel in model.py, then register it in register.py.
    • Custom Sampling: Implement a new function in Procedure.py, then modify the corresponding logic in main.py to use it.
  3. Train the LightGCN model via main.py

    master

    The main.py script serves as the primary entrypoint for training recommendation models (such as LightGCN) using the BPR (Bayesian Personalized Ranking) loss. It orchestrates model initialization, weight loading, TensorBoard logging, and the training loop.

    Key behaviors:

    • Model Initialization: Models are instantiated using a registry system (register.MODELS) based on the world.model_name configuration.
    • Weight Management: The script automatically determines a filename via utils.getFileName(). If world.LOAD is enabled, it attempts to load existing weights from that file; otherwise, it starts training from scratch.
    • Training Loop: For each epoch, the script performs a test evaluation (if epoch % 10 == 0) using Procedure.Test and then executes the training step via Procedure.BPR_train_original.
    • Logging: If world.tensorboard is enabled, it initializes a SummaryWriter with a timestamped directory under world.BOARD_PATH.
    # The execution flow follows this pattern:
    # 1. Initialize model from register
    # 2. Load weights if world.LOAD is True
    # 3. Initialize TensorBoard if world.tensorboard is True
    # 4. Loop through world.TRAIN_epochs:
    #    a. Test every 10 epochs
    #    b. Run BPR training
    #    c. Save model state
  4. Run a 3-layer LightGCN example

    master

    To run the LightGCN model on the Gowalla dataset with 3 layers, navigate to the code directory and execute main.py with the following arguments. The project uses a fixed seed (--seed=2020) for numpy and torch to ensure reproducible results.

    cd code && python main.py --decay=1e-4 --lr=0.001 --layer=3 --seed=2020 --dataset="gowalla" --topks="[20]" --recdim=64
  5. Configure training via the world module

    master

    The training process is controlled by a world module (imported as world), which acts as a global configuration object. The following keys/attributes are used by main.py to drive execution:

    • world.seed: Random seed for reproducibility.
    • world.model_name: The name of the model to instantiate from the register module.
    • world.config: A dictionary containing hyperparameters (e.g., world.config['multicore']).
    • world.device: The torch device (CPU/GPU) to run the model on.
    • world.LOAD: Boolean flag indicating whether to load existing model weights.
    • world.tensorboard: Boolean flag to enable/disable TensorBoard logging.
    • world.BOARD_PATH: The base directory for TensorBoard logs.
    • world.comment: A string used to label TensorBoard log directories.
    • world.TRAIN_epochs: Total number of training epochs.
  6. Reference: main.py CLI arguments

    master

    The following command-line arguments are used to configure the LightGCN training process:

    • --decay: Weight decay value.
    • --lr: Learning rate.
    • --layer: Number of GCN layers.
    • --seed: Random seed for reproducibility.
    • --dataset: Name of the dataset (e.g., "gowalla").
    • --topks: List of top-K values for evaluation (e.g., "[20]").
    • --recdim: Dimension of the recommendation embeddings.
    # Example usage
    python main.py --decay=1e-4 --lr=0.001 --layer=3 --seed=2020 --dataset="gowalla" --topks="[20]" --recdim=64