ITADN
dmlc/dgl/Issues

[Bugfix][MetaPath2Vec]Fix MetaPath2Vec sample function bug (Issue #5514)

#7875Pull Requestpkrai7326 创建于 2025-03-13
P
pkrai7326commented
## 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 by `random_walk`, the issue of non-existent paths is resolved. However, this introduces an edge case where certain nodes (e.g., nodes `13` and `14` in the example graph) have no valid neighbors for the first edge. In such cases, the path length is `1`, meaning the `sample` function does not generate any valid node pairs. To handle this case, the model's `forward` function is modified to return a loss of `0.0` when there are no valid positive samples. This prevents errors and ensures the backward pass remains unaffected. ![metapath2vec_bug](https://github.com/user-attachments/assets/115e98fe-5249-45f6-9e7d-4755f05e4107) ## Checklist Please feel free to remove inapplicable items for your PR. - [x] The PR title starts with [$CATEGORY] (such as [NN], [Model], [Doc], [Feature]]) - [x] I've leverage the [tools](https://docs.google.com/document/d/1iHyj7zlmygKSk5gBPsqIqL5ASPzJSPREaNT_QdsiYA4/edit) to beautify the python and c++ code. - [x] The PR is complete and small, read the [Google eng practice (CL equals to PR)](https://google.github.io/eng-practices/review/developer/small-cls.html) to understand more about small PR. In DGL, we consider PRs with less than 200 lines of core code change are small (example, test and documentation could be exempted). - [ ] All changes have test coverage - [ ] Code is well-documented - [ ] To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change - [x] Related issue is referred in this PR <!-- You could use following template ## Changes - [ ] Feature1, tests, (and when applicable, API doc) - [ ] Feature2, tests, (and when applicable, API doc) -->
合并状态:未合并 0 条评论