[Bugfix][MetaPath2Vec]Fix MetaPath2Vec sample function bug (Issue #5514) #7875
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
In the
MetaPath2Vec
sample
function,dgl.sampling.random_walk
generates node ID-1
when there are no neighbors following the given meta-path. For example, a sampled path may look like:[user_0, company_1, product_-1, company_-1, user_-1]
When mapping these local node IDs to global node IDs, the path becomes:
[10, 1, 9, 4, 14]
However, the sub-path
[9, 14]
does not actually exist in the graph, leading to incorrect edge relationships.Fix
By filtering out
-1
nodes generated byrandom_walk
, the issue of non-existent paths is resolved. However, this introduces an edge case where certain nodes (e.g., nodes13
and14
in the example graph) have no valid neighbors for the first edge. In such cases, the path length is1
, meaning thesample
function does not generate any valid node pairs.To handle this case, the model's
forward
function is modified to return a loss of0.0
when there are no valid positive samples. This prevents errors and ensures the backward pass remains unaffected.Checklist
Please feel free to remove inapplicable items for your PR.