Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dgl
Commits
1b152bf5
Unverified
Commit
1b152bf5
authored
Apr 04, 2020
by
Mufei Li
Committed by
GitHub
Apr 04, 2020
Browse files
[Doc & Tutorial] Fix dgl.batch and GAT (#1418)
* Update doc and tutorials * Fix
parent
24dc71fc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
0 deletions
+22
-0
python/dgl/graph.py
python/dgl/graph.py
+15
-0
tutorials/models/1_gnn/9_gat.py
tutorials/models/1_gnn/9_gat.py
+7
-0
No files found.
python/dgl/graph.py
View file @
1b152bf5
...
...
@@ -4009,6 +4009,7 @@ def batch(graph_list, node_attrs=ALL, edge_attrs=ALL):
The nodes and edges are re-indexed with a new id in the batched graph with the
rule below:
====== ========== ======================== === ==========================
item Graph 1 Graph 2 ... Graph k
====== ========== ======================== === ==========================
...
...
@@ -4040,6 +4041,7 @@ def batch(graph_list, node_attrs=ALL, edge_attrs=ALL):
--------
Create two :class:`~dgl.DGLGraph` objects.
**Instantiation:**
>>> import dgl
>>> import torch as th
>>> g1 = dgl.DGLGraph()
...
...
@@ -4052,13 +4054,17 @@ def batch(graph_list, node_attrs=ALL, edge_attrs=ALL):
>>> g2.add_edges([0, 2], [1, 1]) # Add edges 0 -> 1, 2 -> 1
>>> g2.ndata['hv'] = th.tensor([[2.], [3.], [4.]]) # Initialize node features
>>> g2.edata['he'] = th.tensor([[1.], [2.]]) # Initialize edge features
Merge two :class:`~dgl.DGLGraph` objects into one :class:`DGLGraph` object.
When merging a list of graphs, we can choose to include only a subset of the attributes.
>>> bg = dgl.batch([g1, g2], edge_attrs=None)
>>> bg.edata
{}
Below one can see that the nodes are re-indexed. The edges are re-indexed in
the same way.
>>> bg.nodes()
tensor([0, 1, 2, 3, 4])
>>> bg.ndata['hv']
...
...
@@ -4067,14 +4073,17 @@ def batch(graph_list, node_attrs=ALL, edge_attrs=ALL):
[2.],
[3.],
[4.]])
**Property:**
We can still get a brief summary of the graphs that constitute the batched graph.
>>> bg.batch_size
2
>>> bg.batch_num_nodes
[2, 3]
>>> bg.batch_num_edges
[1, 2]
**Readout:**
Another common demand for graph neural networks is graph readout, which is a
function that takes in the node attributes and/or edge attributes for a graph
...
...
@@ -4082,20 +4091,26 @@ def batch(graph_list, node_attrs=ALL, edge_attrs=ALL):
DGL also supports performing readout for a batch of graphs at once.
Below we take the built-in readout function :func:`sum_nodes` as an example, which
sums over a particular kind of node attribute for each graph.
>>> dgl.sum_nodes(bg, 'hv') # Sum the node attribute 'hv' for each graph.
tensor([[1.], # 0 + 1
[9.]]) # 2 + 3 + 4
**Message passing:**
For message passing and related operations, batched :class:`DGLGraph` acts exactly
the same as a single :class:`~dgl.DGLGraph` with batch size 1.
**Update Attributes:**
Updating the attributes of the batched graph has no effect on the original graphs.
>>> bg.edata['he'] = th.zeros(3, 2)
>>> g2.edata['he']
tensor([[1.],
[2.]])}
Instead, we can decompose the batched graph back into a list of graphs and use them
to replace the original graphs.
>>> g1, g2 = dgl.unbatch(bg) # returns a list of DGLGraph objects
>>> g2.edata['he']
tensor([[0., 0.],
...
...
tutorials/models/1_gnn/9_gat.py
View file @
1b152bf5
...
...
@@ -120,6 +120,13 @@ class GATLayer(nn.Module):
self
.
fc
=
nn
.
Linear
(
in_dim
,
out_dim
,
bias
=
False
)
# equation (2)
self
.
attn_fc
=
nn
.
Linear
(
2
*
out_dim
,
1
,
bias
=
False
)
self
.
reset_parameters
()
def
reset_parameters
(
self
):
"""Reinitialize learnable parameters."""
gain
=
nn
.
init
.
calculate_gain
(
'relu'
)
nn
.
init
.
xavier_normal_
(
self
.
fc
.
weight
,
gain
=
gain
)
nn
.
init
.
xavier_normal_
(
self
.
attn_fc
.
weight
,
gain
=
gain
)
def
edge_attention
(
self
,
edges
):
# edge UDF for equation (2)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment