You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 64904d3
Browse filesBrowse the repository at this point in the historyBrowse files
Add pad_graph and restore mini-batches in the graph classification tutorial
pad_graph pads a graph to fixed numbers of nodes, edges and graphs, so
Reactant can compile a model or training step once and reuse it for every
mini-batch. The graph classification tutorial goes back to master's
mini-batch training, padding each batch and masking the padding graphs out
of the loss.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Entries link to the pull request that introduced them.
14
14
15
15
**Added**
16
16
- Graphs can be compiled with Reactant.jl: a `GNNGraph` can now be moved to a Reactant device (e.g. `g |> reactant_device()`) and traced. The COO index vectors and the `graph_indicator` of a `GNNGraph` are no longer required to have an `Integer` eltype, so that traced arrays (eltype `TracedRNumber{Int}`) can be stored in them, and a new `Reactant` package extension makes `degree` work inside a trace ([#730]).
17
+
- Added `pad_graph`, which pads a graph to given numbers of nodes, edges and graphs, putting the padding in extra graphs. Padding every mini-batch to the same size lets Reactant.jl compile a model or a training step once and reuse it for all batches ([#730]).
Here, we again make use of the `GCNConv` with $\mathrm{ReLU}(x) = \max(x, 0)$ activation for obtaining localized node embeddings, before we apply our final classifier on top of a graph readout layer.
221
221
222
-
### Compiling with fixed shapes
222
+
### Padding the mini-batches
223
223
224
224
Reactant compiles a program for fixed array shapes, and `Lux.Training.single_train_step!` compiles the training step once, on its first call, and reuses it for all subsequent calls.
225
-
A mini-batch of graphs, however, has a different number of nodes and edges every time, so it cannot be fed to a training step compiled for another mini-batch.
226
-
The general solution is to pad every batch to the same size. Since MUTAG is small, here we simply batch all training graphs (and, separately, all test graphs) into one giant graph and perform full-batch training:
225
+
A mini-batch of graphs, however, has a different number of nodes and edges every time.
226
+
We therefore pad every mini-batch with [`pad_graph`](@ref) to the same number of nodes, edges and graphs: the padding nodes and edges are put in an extra graph, and empty graphs are added until there are `batchsize + 1` graphs.
227
+
Our node budget fits a batch of the largest graphs, plus one padding node:
227
228
228
229
````julia
229
-
g_train = MLUtils.batch(train_data[1]) |> xdev
230
-
y_train = train_data[2] |> xdev
231
-
g_test = MLUtils.batch(test_data[1]) |> xdev
232
-
y_test = test_data[2] |> xdev;
230
+
batchsize = train_loader.batchsize
231
+
max_nodes = batchsize *maximum(g.num_nodes for g in graphs) +1
232
+
max_edges = batchsize *maximum(g.num_edges for g in graphs)
233
+
max_graphs = batchsize +1
234
+
235
+
functionpad_batch(gs, y)
236
+
g = MLUtils.batch(gs)
237
+
n = g.num_graphs
238
+
g =pad_graph(g, max_nodes, max_edges, max_graphs)
239
+
y = [Float32.(y) zeros(Float32, size(y, 1), max_graphs - n)]
240
+
mask =Float32.((1:max_graphs) .<= n)'# the padding graphs are excluded from the loss
241
+
return g, y, mask, n
242
+
end;
233
243
````
234
244
235
-
Let's train our network to see how well it performs on the training as well as test set.
236
-
For evaluation, we compile the forward pass in inference mode once for each of the two giant graphs:
245
+
The labels are padded too, and the `mask` restricts the loss to the original graphs.
246
+
Note that the mask is passed to the training step as data, i.e. moved to the device with the other arrays: values that are not device arrays would be fixed when the training step is compiled.
247
+
248
+
Let's train our network for a few epochs to see how well it performs on the training as well as test set:
As one can see, our model reaches around **75% test accuracy**.
316
+
As one can see, our model reaches around **70% test accuracy**.
291
317
Reasons for the fluctuations in accuracy can be explained by the rather small dataset (only 38 test graphs), and usually disappear once one applies GNNs to larger datasets.
0 commit comments