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
168794cd
"vscode:/vscode.git/clone" did not exist on "f2600c2e6ac0d3f0dee2345c821bade90b2d9328"
Unverified
Commit
168794cd
authored
Dec 01, 2018
by
Lingfan Yu
Committed by
GitHub
Dec 01, 2018
Browse files
remove text using code line numbers (#217)
parent
7156c716
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
15 deletions
+4
-15
tutorials/models/4_rgcn.py
tutorials/models/4_rgcn.py
+4
-15
No files found.
tutorials/models/4_rgcn.py
View file @
168794cd
...
...
@@ -125,6 +125,7 @@ base. This tutorial shows how to implement R-GCN with DGL.
# Each relation type is associated with a different weight. Therefore,
# the full weight matrix has three dimensions: relation, input_feature,
# output_feature.
#
import
torch
import
torch.nn
as
nn
...
...
@@ -149,10 +150,11 @@ class RGCNLayer(nn.Module):
if
self
.
num_bases
<=
0
or
self
.
num_bases
>
self
.
num_rels
:
self
.
num_bases
=
self
.
num_rels
#
add
weight
s
# weight
bases in equation (3)
self
.
weight
=
nn
.
Parameter
(
torch
.
Tensor
(
self
.
num_bases
,
self
.
in_feat
,
self
.
out_feat
))
if
self
.
num_bases
<
self
.
num_rels
:
# linear combination coefficients in equation (3)
self
.
w_comp
=
nn
.
Parameter
(
torch
.
Tensor
(
self
.
num_rels
,
self
.
num_bases
))
# add bias
...
...
@@ -171,7 +173,7 @@ class RGCNLayer(nn.Module):
def
forward
(
self
,
g
):
if
self
.
num_bases
<
self
.
num_rels
:
# generate all weights from bas
i
s (equation (3))
# generate all weights from bas
e
s (equation (3))
weight
=
self
.
weight
.
view
(
self
.
in_feat
,
self
.
num_bases
,
self
.
out_feat
)
weight
=
torch
.
matmul
(
self
.
w_comp
,
weight
).
view
(
self
.
num_rels
,
self
.
in_feat
,
self
.
out_feat
)
...
...
@@ -204,19 +206,6 @@ class RGCNLayer(nn.Module):
###############################################################################
# As mentioned above, R-GCN uses decomposition of reduce parameter size
# (equation (3)). So line 18-19 defines the weight bases (:math:`V_b^{(l)}`),
# and line 20-21 defines the linear combination coefficients
# (:math:`a_{rb}^{(l)}`). The forward function of R-GCN layer is similar to
# GCN, except that at the beginning of forward phase, weights for each
# relation type is generated from bases (line 41-44).
#
# The message function for R-GCN replicates weights onto edges and then
# generates messages (line 55-59). But for the first layer, since the node
# feature is the node id, the transformation from node feature to messages
# can be computed more efficiently by performing an embedding lookup (line
# 49-53).
#
# Define full R-GCN model
# ~~~~~~~~~~~~~~~~~~~~~~~
...
...
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