Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SparseTensor label in LightGCN #5046

Merged
merged 9 commits into from
Jul 30, 2022
17 changes: 16 additions & 1 deletion torch_geometric/nn/models/lightgcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from torch import Tensor
from torch.nn import Embedding, ModuleList
from torch.nn.modules.loss import _Loss
from torch_sparse import SparseTensor

from torch_geometric.nn.conv import LGConv
from torch_geometric.typing import Adj, OptTensor
Expand Down Expand Up @@ -110,7 +111,21 @@ def forward(self, edge_index: Adj,
:obj:`edge_index` will be used instead. (default: :obj:`None`)
"""
if edge_label_index is None:
edge_label_index = edge_index
if isinstance(edge_index, SparseTensor):
edge_label_index = edge_index.to_torch_sparse_coo_tensor(
hzg0601 marked this conversation as resolved.
Show resolved Hide resolved
).coalesce().indices()
elif isinstance(edge_index, torch.Tensor) and edge_index.is_sparse(
hzg0601 marked this conversation as resolved.
Show resolved Hide resolved
) and not edge_index.is_coalesced():
edge_label_index = edge_index.coalesce().indices()
elif isinstance(edge_index, torch.Tensor) and edge_index.is_sparse(
) and edge_index.is_coalesced():
edge_label_index = edge_index.indices()
elif isinstance(edge_index,
torch.Tensor) and not edge_index.is_sparse():
assert edge_index.shape == 2
edge_label_index = edge_index
else:
raise TypeError

out = self.get_embedding(edge_index)

Expand Down