# Minibatches of graphs in GeometricFlux.jl

**URL:** https://discourse.julialang.org/t/minibatches-of-graphs-in-geometricflux-jl/70170
**Category:** Machine Learning
**Tags:** flux, geometricflux
**Created:** [October 21, 2021, 6:18pm UTC](https://discourse.julialang.org/t/minibatches-of-graphs-in-geometricflux-jl/70170 "2021-10-21T18:18:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)
#### Post date: [October 21, 2021, 6:18pm UTC](https://discourse.julialang.org/t/minibatches-of-graphs-in-geometricflux-jl/70170/1 "2021-10-21T18:18:08Z")

</div>

In the supervised graph problem, you train a neural network that maps entire graph instances to target values. The GeometricFlux.jl package has great functionality for training graph neural networks; however, all of the examples ([https://github.com/FluxML/GeometricFlux.jl/tree/master/examples](https://github.com/FluxML/GeometricFlux.jl/tree/master/examples)) focus on the problem of making node-level predictions (rather than graph-level predictions).

Is there a preferred syntax for using GeometricFlux.jl models to make predictions over minibatches of graphs? A MWE below gives how I’ve been handling this, but I’m not sure if I’m making some dumb or inefficient design decision. My key design detail is probably the definition of a `(f::Chain)(x::AbstractVector{FeaturedGraph}) ` method.

I compare this to the `dgl.batch` function in Python’s DGL ([Training a GNN for Graph Classification — DGL 0.9 documentation](https://docs.dgl.ai/en/latest/tutorials/blitz/5_graph_classification.html#a-batched-graph-in-dgl)), which explicitly constructs a graph minibatch.

Thanks!

```julia
using Flux, GeometricFlux, LightGraphs

# generate a graph with node features
function get_graph()
    mg = SimpleGraph()
    add_vertices!(mg, 3)
    add_edge!(mg, 1, 2)
    add_edge!(mg, 2, 3)
    adj_mat = Float32.(adjacency_matrix(mg))
    FeaturedGraph(adj_mat; nf=ones(Float32, 2, nv(mg)))
end

# a method for making predictions over a minibatch of `FeaturedGraph` datapoints
(f::Chain)(x::AbstractVector{FeaturedGraph}) = Flux.stack(f.(x), 1) |> vec

# define a simple graph neural network
function get_model(input_dim=2, hidden_dim=3, output_dim=1)
    Chain(GraphConv(input_dim => hidden_dim, tanh), # x_i = tanh( Θ_1 x_i + ∑_{j in N(i)} Θ_2 x_j )
          node_feature,
          x -> sum(x, dims=2), # sum aggregation
          Dense(hidden_dim, output_dim))
end

model = get_model()
# standard loss function for graph regression
loss(x, y) = Flux.Losses.mse(model(x), y)

# a single data point
graph = get_graph()
target = 0.5f0

# minibatch of data points
graphs = FeaturedGraph[graph, graph, graph]
targets = [target, target, target]

# evaluate model on minibatch
loss(graphs, targets)

```

---

<div class="post-metadata">

### Author: ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)
#### Post date: [October 22, 2021, 12:31pm UTC](https://discourse.julialang.org/t/minibatches-of-graphs-in-geometricflux-jl/70170/2 "2021-10-22T12:31:01Z")

</div>

GeometricFlux doesn’t support graph batching yet ([https://github.com/FluxML/GeometricFlux.jl/issues/218](https://github.com/FluxML/GeometricFlux.jl/issues/218)) and graph batch predictions.  
You can find these features in GraphNeuralNetworks.jl, e.g. see  
this example of graph classification  
[https://github.com/CarloLucibello/GraphNeuralNetworks.jl/blob/master/examples/graph\_classification\_tudataset.jl](https://github.com/CarloLucibello/GraphNeuralNetworks.jl/blob/master/examples/graph_classification_tudataset.jl)

---

<div class="post-metadata">

### Author: ![bad\_at\_math](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bad\_at\_math](https://discourse.julialang.org/u/bad_at_math)
#### Post date: [October 22, 2021, 1:04pm UTC](https://discourse.julialang.org/t/minibatches-of-graphs-in-geometricflux-jl/70170/3 "2021-10-22T13:04:55Z")

</div>

Thank you! I had not seen GraphNeuralNetworks.jl before, so I’ll take a look at it.
