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

Update metapath2vec.py #9651

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions torch_geometric/nn/models/metapath2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,22 @@ def forward(self, node_type: str, batch: OptTensor = None) -> Tensor:
:obj:`node_type`.
"""
emb = self.embedding.weight[self.start[node_type]:self.end[node_type]]
return emb if batch is None else emb.index_select(0, batch)
if batch is not None:
if not isinstance(
batch, torch.Tensor): # Convert to tensor if not already
batch = torch.tensor(batch, dtype=torch.long).to(emb.device)

if batch.dim(
) == 0: # If batch is a scalar, convert it to a 1D tensor
batch = batch.unsqueeze(0)

if batch.dim(
) > 1: # If batch is multi-dimensional, flatten it to a 1D vector
batch = batch.view(-1)

return emb.index_select(0, batch)
else:
return emb

def loader(self, **kwargs):
r"""Returns the data loader that creates both positive and negative
Expand Down Expand Up @@ -257,6 +272,10 @@ def sample(rowptr: Tensor, col: Tensor, rowcount: Tensor, subset: Tensor,
rand = rand.to(torch.long) + rowptr[subset].view(-1, 1)
rand = rand.clamp(max=col.numel() - 1) # If last node is isolated.

col = col[rand] if col.numel() > 0 else rand
if col.numel() > 0:
rand = torch.min(rand, torch.tensor(col.numel() - 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already clamp the value in line 273. What does this do here?

col = col[rand]
else:
col = rand
col[mask | (count == 0)] = dummy_idx
return col
Loading