Skip to content

Commit

Permalink
Add LightGCN Model (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrylong authored Sep 24, 2023
1 parent 5049bc3 commit e52d933
Show file tree
Hide file tree
Showing 9 changed files with 479 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ The recommender models supported by Cornac are listed below. Why don't you join
| | [Explainable Recommendation with Comparative Constraints on Product Aspects (ComparER)](cornac/models/comparer), [paper](https://dl.acm.org/doi/pdf/10.1145/3437963.3441754) | N/A | [PreferredAI/ComparER](https://github.com/PreferredAI/ComparER)
| 2020 | [Adversarial Training Towards Robust Multimedia Recommender System (AMR)](cornac/models/amr), [paper](https://ieeexplore.ieee.org/document/8618394) | [requirements.txt](cornac/models/amr/requirements.txt) | [amr_clothing.py](examples/amr_clothing.py)
| | [Hybrid neural recommendation with joint deep representation learning of ratings and reviews (HRDR)](cornac/models/hrdr), [paper](https://www.sciencedirect.com/science/article/abs/pii/S0925231219313207) | [requirements.txt](cornac/models/hrdr/requirements.txt) | [hrdr_example.py](examples/hrdr_example.py)
| | [LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation](cornac/models/lightgcn), [paper](https://arxiv.org/pdf/2002.02126.pdf) | [requirements.txt](cornac/models/lightgcn/requirements.txt) | [lightgcn_example.py](examples/lightgcn_example.py)
| 2019 | [Embarrassingly Shallow Autoencoders for Sparse Data (EASEᴿ)](cornac/models/ease), [paper](https://arxiv.org/pdf/1905.03375.pdf) | N/A | [ease_movielens.py](examples/ease_movielens.py)
| 2018 | [Collaborative Context Poisson Factorization (C2PF)](cornac/models/c2pf), [paper](https://www.ijcai.org/proceedings/2018/0370.pdf) | N/A | [c2pf_exp.py](examples/c2pf_example.py)
| | [Graph Convolutional Matrix Completion (GCMC)](cornac/models/gcmc), [paper](https://www.kdd.org/kdd2018/files/deep-learning-day/DLDay18_paper_32.pdf) | [requirements.txt](cornac/models/gcmc/requirements.txt) | [gcmc_example.py](examples/gcmc_example.py)
Expand Down
1 change: 1 addition & 0 deletions cornac/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .ibpr import IBPR
from .knn import ItemKNN
from .knn import UserKNN
from .lightgcn import LightGCN
from .mcf import MCF
from .mf import MF
from .mmmf import MMMF
Expand Down
16 changes: 16 additions & 0 deletions cornac/models/lightgcn/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2018 The Cornac Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

from .recom_lightgcn import LightGCN
102 changes: 102 additions & 0 deletions cornac/models/lightgcn/lightgcn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import torch
import torch.nn as nn
import dgl
import dgl.function as fn


def construct_graph(data_set):
"""
Generates graph given a cornac data set
Parameters
----------
data_set : cornac.data.dataset.Dataset
The data set as provided by cornac
"""
user_indices, item_indices, _ = data_set.uir_tuple
user_nodes, item_nodes = (
torch.from_numpy(user_indices),
torch.from_numpy(
item_indices + data_set.total_users
), # increment item node idx by num users
)

u = torch.cat([user_nodes, item_nodes], dim=0)
v = torch.cat([item_nodes, user_nodes], dim=0)

g = dgl.graph((u, v), num_nodes=(data_set.total_users + data_set.total_items))
return g


class GCNLayer(nn.Module):
def __init__(self):
super(GCNLayer, self).__init__()

def forward(self, graph, src_embedding, dst_embedding):
with graph.local_scope():
inner_product = torch.cat((src_embedding, dst_embedding), dim=0)

out_degs = graph.out_degrees().to(src_embedding.device).float().clamp(min=1)
norm_out_degs = torch.pow(out_degs, -0.5).view(-1, 1) # D^-1/2

inner_product = inner_product * norm_out_degs

graph.ndata["h"] = inner_product
graph.update_all(
message_func=fn.copy_u("h", "m"), reduce_func=fn.sum("m", "h")
)

res = graph.ndata["h"]

in_degs = graph.in_degrees().to(src_embedding.device).float().clamp(min=1)
norm_in_degs = torch.pow(in_degs, -0.5).view(-1, 1) # D^-1/2

res = res * norm_in_degs
return res


class Model(nn.Module):
def __init__(self, user_size, item_size, hidden_size, num_layers=3, device=None):
super(Model, self).__init__()
self.user_size = user_size
self.item_size = item_size
self.hidden_size = hidden_size
self.embedding_weights = self._init_weights()
self.layers = nn.ModuleList([GCNLayer() for _ in range(num_layers)])
self.device = device

def forward(self, graph):
user_embedding = self.embedding_weights["user_embedding"]
item_embedding = self.embedding_weights["item_embedding"]

for i, layer in enumerate(self.layers, start=1):
if i == 1:
embeddings = layer(graph, user_embedding, item_embedding)
else:
embeddings = layer(
graph, embeddings[: self.user_size], embeddings[self.user_size:]
)

user_embedding = user_embedding + embeddings[: self.user_size] * (
1 / (i + 1)
)
item_embedding = item_embedding + embeddings[self.user_size:] * (
1 / (i + 1)
)

return user_embedding, item_embedding

def _init_weights(self):
initializer = nn.init.xavier_uniform_

weights_dict = nn.ParameterDict(
{
"user_embedding": nn.Parameter(
initializer(torch.empty(self.user_size, self.hidden_size))
),
"item_embedding": nn.Parameter(
initializer(torch.empty(self.item_size, self.hidden_size))
),
}
)
return weights_dict
Loading

0 comments on commit e52d933

Please sign in to comment.