This is an updated PyTorch implementation of the KGCN model, building upon two previous works: TensorFlow KGCN and PyTorch KGCN. The former is the offical one while the latter is a PyTorch version implemented by @zzaebok. This repo modifies directly on his PyTorch KGCN.
- KGCN-pytorch-updated
KGCN is Knowledge Graph Convolutional Networks for recommender systems, which uses the technique of graph convolutional networks (GCN) to proces knowledge graphs for the purpose of recommendation.
Figure 1: KGCN Framework
Reference:
Knowledge Graph Convolutional Networks for Recommender Systems Hongwei Wang, Miao Zhao, Xing Xie, Wenjie Li, Minyi Guo. In Proceedings of The 2019 Web Conference (WWW 2019)
- ACM: https://dl.acm.org/citation.cfm?id=3313417
- arXiv: https://arxiv.org/abs/1904.12575
- Paper With Code: https://paperswithcode.com/paper/190412575
For showing result under one specific hyper parameter setting, use KGCN.ipynb
or KGCN.py
.
For batch experiments, use batch_experiments.ipynb
.
p.s. KGCN.ipynb
and KGCN.py
have the same functionality, but the latter is modularized for easy debugging and reuse.
Raw rating file for movie is too large to be contained in this repo.
Downlad the rating data first
$ wget http://files.grouplens.org/datasets/movielens/ml-20m.zip
$ unzip ml-20m.zip
$ mv ml-20m/ratings.csv data/movie/
Nothing to do
This dataset is built upon the Rec-Tmall dataset. Check ./data/product/preprocessing.ipynb for more information.
If you want to use your own dataset, you need to prepare 3 files.
- Rating Data
- Each row should contain (user-item-rating)
- In this repo, it is pandas dataframe structure. (look at
data_loader.py
)
- Knowledge Graph
- Each triple(head-relation-tail) consists of knowledge graph
- In this repo, it is dictionary type. (look at
data_loader.py
)
- Item Index to Entity Index Mapping
- Check ./data/product/preprocessing.ipynb to see my solutions.
Core files:
data_loader.py
- data loader class for movie / music dataset
- you don't need it if you make custom dataset
aggregator.py
- aggregator class which implements 3 aggregation functions
- and 2 mixers
model.py
- KGCN model network
Figure 2: Dependency Graph
1.5. Comparison with PyTorch KGCN
- Dataset source: Rec-Tmall
- Preprocessing script: preprocessing.ipynb
transe
mixer has a better performace on divergence speed, time complexity, divergence loss value and AUC value, final-epoch loss value and AUC value in most cases.
See _mix_neighbor_vectors_TransE()
in aggregator.py
Automatically conduct multiple experiments.
- Config many parameter sets once
- Run
- Check results in a well-formatted print-out
1.5.4. Fix RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (CPU)
Before
After
neighbor_entities = torch.LongTensor(self.adj_ent[entities[h].cpu()]).view((self.batch_size, -1)).to(self.device)
neighbor_relations = torch.LongTensor(self.adj_rel[entities[h].cpu()]).view((self.batch_size, -1)).to(self.device)
Before
After
self.adj_ent = torch.zeros(self.num_ent, self.n_neighbor, dtype=torch.long)
self.adj_rel = torch.zeros(self.num_ent, self.n_neighbor, dtype=torch.long)