The Knowledge Graph Policy Network (KGPolicy) is a cutting-edge technique designed to enhance personalized recommendations by utilizing the power of knowledge graphs. This framework focuses on negative sampling to improve the quality of recommendations. In this article, we will guide you through the steps required to set up, run, and troubleshoot your KGPolicy implementation in PyTorch.
Introduction
KGPolicy exploits the rich relationships inherent in knowledge graphs to discover high-quality negative samples that improve recommendation accuracy. This technique was discussed in detail in the research paper by Xiang Wang et al. titled “Reinforced Negative Sampling over Knowledge Graph for Recommendation.”
Reproducibility Steps
Here’s how to reproduce the best performance reported in the KGPolicy research:
1. Data and Source Code
- Create a new directory for this repository:
bash mkdir KG-Policy
cd KG-Policy
bash wget https://github.com/xiangwang1223/kgpolicy/releases/download/v1.0/Data.zip
unzip Data.zip
bash git clone https://github.com/xiangwang1223/kgpolicy.git
2. Set Up Environment
Utilize conda for managing your environment:
- Switch to the source code directory:
cd kgpolicy
bash conda create -n geo python=3.6
conda activate geo
bash setup.sh
Note: A mismatch between torch_geometric and CUDA versions may arise. If you encounter this, ensure the correct CUDA version is installed.
3. Training the Model
To train the KGPolicy model on your dataset, use the following commands:
- Training on Last.fm:
bash python main.py
bash python main.py --regs 1e-4 --dataset yelp2018 --model_path modelbest_yelp.ckpt
python main.py --regs 1e-4 --dataset amazon-book --model_path modelbest_ab.ckpt
By default, the regularization term (regs) is set to 1e-5, but 1e-4 is recommended for the latter datasets.
4. Experiment Results
To evaluate the performance of the model, we use metrics like Recall@K and NDCG@K. The results show the model’s efficiency:
- Last.fm:
Model Recall@20 NDCG@20
RNS 0.0687 0.0584
DNS 0.0874 0.0746
IRGAN 0.0755 0.0627
KG-Policy 0.0957 0.0837
Model Recall@20 NDCG@20
RNS 0.0465 0.0298
DNS 0.0666 0.0429
IRGAN 0.0538 0.0342
KG-Policy 0.0746 0.0489
Model Recall@20 NDCG@20
RNS 0.1239 0.0647
DNS 0.1460 0.0775
IRGAN 0.1330 0.0693
KG-Policy 0.1609 0.0890
Troubleshooting
While setting up your KGPolicy, you may encounter a few issues. Here are some common problems and their solutions:
- CUDA Compatibility Issues: Make sure that the CUDA version aligns with the version of the PyTorch library you are using.
- Missing Dependencies: Ensure that all required packages are correctly installed. Refer back to the setup instructions.
- Dataset Download Issues: If the dataset download fails due to network issues, reattempt the download command.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

