Skip to content

Commit 6bc9206

Browse files
authoredSep 27, 2020
[Doc] fix minibatch user guide broken code (dmlc#2233)
1 parent 6b0d42d commit 6bc9206

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed
 

‎docs/source/guide/minibatch-edge.rst

+14-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ classification/regression.
188188
.. code:: python
189189
190190
class StochasticTwoLayerRGCN(nn.Module):
191-
def __init__(self, in_feat, hidden_feat, out_feat):
191+
def __init__(self, in_feat, hidden_feat, out_feat, rel_names):
192192
super().__init__()
193193
self.conv1 = dglnn.HeteroGraphConv({
194194
rel : dglnn.GraphConv(in_feat, hidden_feat, norm='right')
@@ -226,6 +226,18 @@ over the edge types for :meth:`~dgl.DGLHeteroGraph.apply_edges`.
226226
edge_subgraph.apply_edges(self.apply_edges, etype=etype)
227227
return edge_subgraph.edata['score']
228228
229+
class Model(nn.Module):
230+
def __init__(self, in_features, hidden_features, out_features, num_classes,
231+
etypes):
232+
super().__init__()
233+
self.rgcn = StochasticTwoLayerRGCN(
234+
in_features, hidden_features, out_features, etypes)
235+
self.pred = ScorePredictor(num_classes, out_features)
236+
237+
def forward(self, edge_subgraph, blocks, x):
238+
x = self.rgcn(blocks, x)
239+
return self.pred(edge_subgraph, x)
240+
229241
Data loader definition is also very similar to that of node
230242
classification. The only difference is that you need
231243
:class:`~dgl.dataloading.pytorch.EdgeDataLoader` instead of
@@ -279,7 +291,7 @@ dictionaries of node types and predictions here.
279291

280292
.. code:: python
281293
282-
model = Model(in_features, hidden_features, out_features, num_classes)
294+
model = Model(in_features, hidden_features, out_features, num_classes, etypes)
283295
model = model.cuda()
284296
opt = torch.optim.Adam(model.parameters())
285297

‎docs/source/guide/minibatch-link.rst

+19-6
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ above.
146146
positive_graph = positive_graph.to(torch.device('cuda'))
147147
negative_graph = negative_graph.to(torch.device('cuda'))
148148
input_features = blocks[0].srcdata['features']
149-
pos_score, neg_score = model(positive_graph, blocks, input_features)
149+
pos_score, neg_score = model(positive_graph, negative_graph, blocks, input_features)
150150
loss = compute_loss(pos_score, neg_score)
151151
opt.zero_grad()
152152
loss.backward()
@@ -166,7 +166,7 @@ classification/regression.
166166
.. code:: python
167167
168168
class StochasticTwoLayerRGCN(nn.Module):
169-
def __init__(self, in_feat, hidden_feat, out_feat):
169+
def __init__(self, in_feat, hidden_feat, out_feat, rel_names):
170170
super().__init__()
171171
self.conv1 = dglnn.HeteroGraphConv({
172172
rel : dglnn.GraphConv(in_feat, hidden_feat, norm='right')
@@ -197,6 +197,20 @@ over the edge types for :meth:`dgl.DGLHeteroGraph.apply_edges`.
197197
dgl.function.u_dot_v('x', 'x', 'score'), etype=etype)
198198
return edge_subgraph.edata['score']
199199
200+
class Model(nn.Module):
201+
def __init__(self, in_features, hidden_features, out_features, num_classes,
202+
etypes):
203+
super().__init__()
204+
self.rgcn = StochasticTwoLayerRGCN(
205+
in_features, hidden_features, out_features, etypes)
206+
self.pred = ScorePredictor()
207+
208+
def forward(self, positive_graph, negative_graph, blocks, x):
209+
x = self.rgcn(blocks, x)
210+
pos_score = self.pred(positive_graph, x)
211+
neg_score = self.pred(negative_graph, x)
212+
return pos_score, neg_score
213+
200214
Data loader definition is also very similar to that of edge
201215
classification/regression. The only difference is that you need to give
202216
the negative sampler and you will be supplying a dictionary of edge
@@ -252,7 +266,7 @@ dictionaries of node types and predictions here.
252266

253267
.. code:: python
254268
255-
model = Model(in_features, hidden_features, out_features, num_classes)
269+
model = Model(in_features, hidden_features, out_features, num_classes, etypes)
256270
model = model.cuda()
257271
opt = torch.optim.Adam(model.parameters())
258272
@@ -261,9 +275,8 @@ dictionaries of node types and predictions here.
261275
positive_graph = positive_graph.to(torch.device('cuda'))
262276
negative_graph = negative_graph.to(torch.device('cuda'))
263277
input_features = blocks[0].srcdata['features']
264-
edge_labels = edge_subgraph.edata['labels']
265-
edge_predictions = model(edge_subgraph, blocks, input_features)
266-
loss = compute_loss(edge_labels, edge_predictions)
278+
pos_score, neg_score = model(positive_graph, negative_graph, blocks, input_features)
279+
loss = compute_loss(pos_score, neg_score)
267280
opt.zero_grad()
268281
loss.backward()
269282
opt.step()

‎docs/source/guide/minibatch-node.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ removed for simplicity):
184184
.. code:: python
185185
186186
class StochasticTwoLayerRGCN(nn.Module):
187-
def __init__(self, in_feat, hidden_feat, out_feat):
187+
def __init__(self, in_feat, hidden_feat, out_feat, rel_names):
188188
super().__init__()
189189
self.conv1 = dglnn.HeteroGraphConv({
190190
rel : dglnn.GraphConv(in_feat, hidden_feat, norm='right')
@@ -224,7 +224,7 @@ dictionaries of node types and predictions here.
224224

225225
.. code:: python
226226
227-
model = StochasticTwoLayerRGCN(in_features, hidden_features, out_features)
227+
model = StochasticTwoLayerRGCN(in_features, hidden_features, out_features, etypes)
228228
model = model.cuda()
229229
opt = torch.optim.Adam(model.parameters())
230230

0 commit comments

Comments
 (0)
Please sign in to comment.