Unverified Commit 535aa3d3 authored by Quan (Andy) Gan's avatar Quan (Andy) Gan Committed by GitHub
Browse files

[Doc]modify the wrong code in initialize the hetero_graph negative sampler (#2382) (#2411)



* modify the wrong code in initialize the hetero_graph negative sampler

the etype in hetero graph should be name of edges
self.weights = {
            etype: g.in_degrees(etype=etype).float() ** 0.75
            for _, etype, _ in g.canonical_etypes
        }
the original code will give etype a Tuple format, it cannot apply to the next processing:
self.weights[etype].multinomial(len(src), replacement=True)
and I add the part which confusing me to generate eid_dict

* fix
Co-authored-by: default avatarQuan Gan <coin2028@hotmail.com>
Co-authored-by: default avatarShaow <coco11563@yeah.net>
parent 35f27c73
...@@ -158,7 +158,7 @@ that shows an example of link prediction on homogeneous graphs. ...@@ -158,7 +158,7 @@ that shows an example of link prediction on homogeneous graphs.
For heterogeneous graphs For heterogeneous graphs
~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~
The models computing the node representations on heterogeneous graphs The models computing the node representations on heterogeneous graphs
can also be used for computing incident node representations for edge can also be used for computing incident node representations for edge
classification/regression. classification/regression.
...@@ -235,23 +235,32 @@ source-destination array pairs. An example is given as follows: ...@@ -235,23 +235,32 @@ source-destination array pairs. An example is given as follows:
.. code:: python .. code:: python
class NegativeSampler(object): class NegativeSampler(object):
def __init__(self, g, k): def __init__(self, g, k):
# caches the probability distribution # caches the probability distribution
self.weights = { self.weights = {
etype: g.in_degrees(etype=etype).float() ** 0.75 etype: g.in_degrees(etype=etype).float() ** 0.75
for etype in g.canonical_etypes} for _, etype, _ in g.canonical_etypes
self.k = k }
self.k = k
def __call__(self, g, eids_dict):
result_dict = {} def __call__(self, g, eids_dict):
for etype, eids in eids_dict.items(): result_dict = {}
src, _ = g.find_edges(eids, etype=etype) for etype, eids in eids_dict.items():
src = src.repeat_interleave(self.k) src, _ = g.find_edges(eids, etype=etype)
dst = self.weights.multinomial(len(src), replacement=True) src = src.repeat_interleave(self.k)
result_dict[etype] = (src, dst) dst = self.weights[etype].multinomial(len(src), replacement=True)
return result_dict result_dict[etype] = (src, dst)
return result_dict
Then you can give the dataloader a dictionary of edge types and edge IDs as well as the negative
sampler. For instance, the following iterates over all edges of the heterogeneous graph.
.. code:: python
train_eid_dict = {
g.edges(etype=etype, form='eid')
for etype in g.etypes}
dataloader = dgl.dataloading.EdgeDataLoader( dataloader = dgl.dataloading.EdgeDataLoader(
g, train_eid_dict, sampler, g, train_eid_dict, sampler,
negative_sampler=NegativeSampler(g, 5), negative_sampler=NegativeSampler(g, 5),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment