"git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "39a3c77e0d4a22de189b02398cf2d003d299b4ae"
Commit 70dc7cd1 authored by Haibin Lin's avatar Haibin Lin Committed by Minjie Wang
Browse files

[Doc] Dgl glance tutorial improvement (#197)

* update dgl at a glance

* update title

* Update 1_first.py
parent bcf8e119
""" """
.. currentmodule:: dgl .. currentmodule:: dgl
DGL at a glance DGL at a Glance
========================= =========================
**Author**: `Minjie Wang <https://jermainewang.github.io/>`_, Quan Gan, `Jake **Author**: `Minjie Wang <https://jermainewang.github.io/>`_, Quan Gan, `Jake
...@@ -112,33 +112,40 @@ plt.show() ...@@ -112,33 +112,40 @@ plt.show()
def super_useful_comp(g): def super_useful_comp(g):
def send_source(edges): def send_source(edges):
# 1. pass the source node feature 'x' weighted by edge feature 'w'
return {'msg': edges.src['x'] * edges.data['w']} return {'msg': edges.src['x'] * edges.data['w']}
def simple_reduce(nodes): def simple_reduce(nodes):
# 2. perform reduction on received messages and update feature 'x'
msgs = nodes.mailbox['msg'] msgs = nodes.mailbox['msg']
return {'x': msgs.sum(1) + nodes.data['x']} return {'x': msgs.sum(1) + nodes.data['x']}
def readout(g):
return th.sum(g.ndata['x'], dim=0)
g.register_message_func(send_source) g.register_message_func(send_source)
g.register_reduce_func(simple_reduce) g.register_reduce_func(simple_reduce)
g.send(g.edges()) g.send(g.edges())
g.recv(g.nodes()) g.recv(g.nodes())
return readout(g) def readout(g):
# 3. read the aggregated node feature 'x' on graph
return th.sum(g.ndata['x'], dim=0)
############################################################################### ###############################################################################
# See the python wrapper: # See the python wrapper:
g_boring = a_boring_graph() g_boring = a_boring_graph()
graph_sum = super_useful_comp(g_boring) graph_sum = readout(g_boring)
print("graph sum is: ", graph_sum) print("graph sum before send() and recv() is: ", graph_sum)
super_useful_comp(g_boring)
graph_sum = readout(g_boring)
print("graph sum after send() and recv() is: ", graph_sum)
g_better = an_interesting_graph() g_better = an_interesting_graph()
graph_sum = super_useful_comp(g_better) graph_sum = readout(g_better)
print("graph sum is: ", graph_sum) print("graph sum before send() and recv() is: ", graph_sum)
super_useful_comp(g_better)
graph_sum = readout(g_better)
print("graph sum after send() and recv() is: ", graph_sum)
############################################################################### ###############################################################################
# Next steps # Next steps
......
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