Skip to content

Commit 5abdcc2

Browse files
Merge pull request #72 from CarloLucibello/cl/docs
improve docs
2 parents 3f12f0a + 51d79ea commit 5abdcc2

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

docs/src/gnngraph.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,33 @@ See also the related methods [`Graphs.adjacency_matrix`](@ref), [`edge_index`](@
4141
## Basic Queries
4242

4343
```julia
44-
source = [1,1,2,2,3,3,3,4]
45-
target = [2,3,1,3,1,2,4,3]
46-
g = GNNGraph(source, target)
44+
julia> source = [1,1,2,2,3,3,3,4];
45+
46+
julia> target = [2,3,1,3,1,2,4,3];
47+
48+
julia> g = GNNGraph(source, target)
49+
GNNGraph:
50+
num_nodes = 4
51+
num_edges = 8
52+
53+
54+
julia> @assert g.num_nodes == 4 # number of nodes
55+
56+
julia> @assert g.num_edges == 8 # number of edges
57+
58+
julia> @assert g.num_graphs == 1 # number of subgraphs (a GNNGraph can batch many graphs together)
59+
60+
julia> is_directed(g) # a GNNGraph is always directed
61+
true
62+
63+
julia> is_bidirected(g) # for each edge, also the reverse edge is present
64+
true
65+
66+
julia> has_self_loops(g)
67+
false
4768

48-
@assert g.num_nodes == 4 # number of nodes
49-
@assert g.num_edges == 8 # number of edges
50-
@assert g.num_graphs == 1 # number of subgraphs (a GNNGraph can batch many graphs together)
51-
is_directed(g) # a GGNGraph is always directed
69+
julia> has_multi_edges(g)
70+
false
5271
```
5372

5473
## Data Features

docs/src/messagepassing.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,65 @@ and [`NNlib.scatter`](@ref) methods.
3434

3535
## Examples
3636

37-
### Basic use of propagate and apply_edges
37+
### Basic use of apply_edges and propagate
3838

39-
TODO
39+
The function [`apply_edges`](@ref) can be used to broadcast node data
40+
on each edge and produce new edge data.
41+
```julia
42+
julia> using GraphNeuralNetworks, Graphs, Statistics
43+
44+
julia> g = rand_graph(10, 20)
45+
GNNGraph:
46+
num_nodes = 10
47+
num_edges = 20
48+
49+
50+
julia> x = ones(2,10)
51+
2×10 Matrix{Float64}:
52+
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
53+
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
54+
55+
julia> z = 2ones(2,10)
56+
2×10 Matrix{Float64}:
57+
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
58+
2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0
59+
60+
julia> apply_edges((xi, xj, e) -> xi .+ xj, g, xi=x, xj=z)
61+
2×20 Matrix{Float64}:
62+
3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0
63+
3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0
64+
65+
# now returning a named tuple
66+
julia> apply_edges((xi, xj, e) -> (a=xi .+ xj, b=xi .- xj), g, xi=x, xj=z)
67+
(a = [3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0], b = [-1.0 -1.0 -1.0 -1.0; -1.0 -1.0 -1.0 -1.0])
68+
69+
# Here we provide a named tuple input
70+
julia> apply_edges((xi, xj, e) -> xi.a + xi.b .* xj, g, xi=(a=x,b=z), xj=z)
71+
2×20 Matrix{Float64}:
72+
5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0
73+
5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0
74+
```
75+
The function [@propagate](@ref) instead performs also the the [`apply_edges`](@ref) operation
76+
but then applies a reduction over each node's neighborhood.
77+
```julia
78+
julia> propagate((xi, xj, e) -> xi .+ xj, g, +, xi=x, xj=z)
79+
2×10 Matrix{Float64}:
80+
3.0 6.0 9.0 9.0 0.0 6.0 6.0 3.0 15.0 3.0
81+
3.0 6.0 9.0 9.0 0.0 6.0 6.0 3.0 15.0 3.0
82+
83+
julia> degree(g)
84+
10-element Vector{Int64}:
85+
1
86+
2
87+
3
88+
3
89+
0
90+
2
91+
2
92+
1
93+
5
94+
1
95+
```
4096

4197
### Implementing a custom Graph Convolutional Layer
4298

src/GNNGraphs/GNNGraphs.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using SparseArrays
44
using Functors: @functor
55
using CUDA
66
import Graphs
7-
using Graphs: AbstractGraph, outneighbors, inneighbors, adjacency_matrix, degree, has_self_loops
7+
using Graphs: AbstractGraph, outneighbors, inneighbors, adjacency_matrix, degree, has_self_loops, is_directed
88
import Flux
99
using Flux: batch
1010
import NNlib
@@ -26,6 +26,7 @@ export adjacency_list,
2626
edge_index,
2727
graph_indicator,
2828
has_multi_edges,
29+
is_directed,
2930
is_bidirected,
3031
normalized_laplacian,
3132
scaled_laplacian,

0 commit comments

Comments
 (0)