@@ -34,9 +34,65 @@ and [`NNlib.scatter`](@ref) methods.
34
34
35
35
## Examples
36
36
37
- ### Basic use of propagate and apply_edges
37
+ ### Basic use of apply_edges and propagate
38
38
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 = 2 ones (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
+ ```
40
96
41
97
### Implementing a custom Graph Convolutional Layer
42
98
0 commit comments