- Python 3.6+
- PyTorch 1.0+
- NumPy
- Pandas
- SciPy
- scikit-learn
python P2F/train_lightgcn.pyThe main parameters are set in the main function:
# Parameter settings
data_path = "dataset/ml-100k.inter" # Dataset path
embedding_size = 64 # Embedding dimension
n_layers = 3 # Number of graph convolution layers
reg_weight = 1e-4 # Regularization weight
batch_size = 2048 # Batch size
lr = 0.001 # Learning rate
epochs = 100 # Number of training epochs
eval_freq = 5 # Evaluation frequency
k_list = [10, 20] # k values for evaluationBefore performing unlearning, the base LightGCN model needs to be trained first. Please run the following command:
python P2F/train_lightgcn.pyAfter training is complete, the pretrained model weights will be saved to the path specified by LIGHTGCN_CONFIG['save_path'], such as ./saved/lightgcn.pth.
data_path: Dataset path, such asdataset/ml-100k.interembedding_size: Embedding dimensionn_layers: Number of GCN layersreg_weight: L2 regularization weightbatch_size: Training batch sizelr: Learning rateepochs: Number of training epochssave_path: Model save path
In config.py, set the parameters related to unlearning UNLEARNING_CONFIG, such as:
UNLEARNING_CONFIG = {
'embedding_size': 64,
'n_layers': 3,
'reg_weight': 1e-4,
'batch_size': 2048,
'lr': 0.001,
'epochs': 30,
'forget_ratio': 0.1, # Ratio of the forget set
'remain_ratio': 1.0, # Sampling ratio of the retain set
'prompt_type': 'attention', # Prompt type
'p_num': 50, # Number of prompts
'KL_temperature': 1.0,
'loss_type': 'WRD', # Loss type, such as 'KL', 'WRD', 'DAD', etc.
'alpha': 0.5,
'lamda': 10.0,
'mu': 5.0,
'K': 5,
'patience': 5,
'validation_interval': 1,
'prompt_save_path': './saved/prompt.pth'
}Make sure that train_lightgcn.py has trained and saved the base model, then run:
python P2F/unlearning.pyThis script will automatically:
- Load the data and pretrained model
- Split the forget set and retain set according to
forget_ratio - Train only the prompt parameters, while keeping the base model parameters unchanged
- Automatically evaluate the performance on the forget set and retain set during training
- Save the trained prompt parameters to
prompt_save_path
During inference, simply load the base model and the trained prompt parameters:
from unlearning import load_prompt_for_inference
prompted_model = load_prompt_for_inference(
base_model, prompt_path, dataset, n_layers, reg_weight, prompt_type, embedding_size, p_num
)The evaluate_unlearning function can be used to evaluate the recommendation performance on the forget set and retain set separately.
-
Data Processing:
- Convert the raw data into a user-item interaction matrix
- Split the training set and test set by user
- Generate negative samples for each positive sample
-
Model Training:
- Use the Adam optimizer
- Use the BPR loss function
- Periodically evaluate model performance
-
Model Evaluation:
- For each test user, predict scores for all items
- Exclude items already interacted with in the training set
- Calculate various metrics for TopK recommendations
-
Pretrained model not found? Please run
train_lightgcn.pyfirst and make sure that a model weight file exists under the path specified byLIGHTGCN_CONFIG['save_path']. -
How to adjust the forget ratio? Modify
UNLEARNING_CONFIG['forget_ratio'], for example,0.1means that 10% of samples are used as the forget set. -
How to switch the loss function? Modify
UNLEARNING_CONFIG['loss_type'], which supportsKL,WRD,DAD, etc.
Xiangnan He et al. "LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation." in SIGIR 2020.