-
Notifications
You must be signed in to change notification settings - Fork 47
/
pool.jl
173 lines (126 loc) · 4.24 KB
/
pool.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
@doc raw"""
GlobalPool(aggr)
Global pooling layer for graph neural networks.
Takes a graph and feature nodes as inputs
and performs the operation
```math
\mathbf{u}_V = \square_{i \in V} \mathbf{x}_i
```
where ``V`` is the set of nodes of the input graph and
the type of aggregation represented by ``\square`` is selected by the `aggr` argument.
Commonly used aggregations are `mean`, `max`, and `+`.
See also [`reduce_nodes`](@ref).
# Examples
```julia
using Flux, GraphNeuralNetworks, Graphs
pool = GlobalPool(mean)
g = GNNGraph(erdos_renyi(10, 4))
X = rand(32, 10)
pool(g, X) # => 32x1 matrix
g = Flux.batch([GNNGraph(erdos_renyi(10, 4)) for _ in 1:5])
X = rand(32, 50)
pool(g, X) # => 32x5 matrix
```
"""
struct GlobalPool{F} <: GNNLayer
aggr::F
end
(l::GlobalPool)(g::GNNGraph, x::AbstractArray) = GNNlib.global_pool(l, g, x)
(l::GlobalPool)(g::GNNGraph) = GNNGraph(g, gdata = l(g, node_features(g)))
@doc raw"""
GlobalAttentionPool(fgate, ffeat=identity)
Global soft attention layer from the [Gated Graph Sequence Neural
Networks](https://arxiv.org/abs/1511.05493) paper
```math
\mathbf{u}_V = \sum_{i\in V} \alpha_i\, f_{feat}(\mathbf{x}_i)
```
where the coefficients ``\alpha_i`` are given by a [`softmax_nodes`](@ref)
operation:
```math
\alpha_i = \frac{e^{f_{gate}(\mathbf{x}_i)}}
{\sum_{i'\in V} e^{f_{gate}(\mathbf{x}_{i'})}}.
```
# Arguments
- `fgate`: The function ``f_{gate}: \mathbb{R}^{D_{in}} \to \mathbb{R}``.
It is tipically expressed by a neural network.
- `ffeat`: The function ``f_{feat}: \mathbb{R}^{D_{in}} \to \mathbb{R}^{D_{out}}``.
It is tipically expressed by a neural network.
# Examples
```julia
chin = 6
chout = 5
fgate = Dense(chin, 1)
ffeat = Dense(chin, chout)
pool = GlobalAttentionPool(fgate, ffeat)
g = Flux.batch([GNNGraph(random_regular_graph(10, 4),
ndata=rand(Float32, chin, 10))
for i=1:3])
u = pool(g, g.ndata.x)
@assert size(u) == (chout, g.num_graphs)
```
"""
struct GlobalAttentionPool{G, F}
fgate::G
ffeat::F
end
Flux.@layer GlobalAttentionPool
GlobalAttentionPool(fgate) = GlobalAttentionPool(fgate, identity)
(l::GlobalAttentionPool)(g, x) = GNNlib.global_attention_pool(l, g, x)
(l::GlobalAttentionPool)(g::GNNGraph) = GNNGraph(g, gdata = l(g, node_features(g)))
"""
TopKPool(adj, k, in_channel)
Top-k pooling layer.
# Arguments
- `adj`: Adjacency matrix of a graph.
- `k`: Top-k nodes are selected to pool together.
- `in_channel`: The dimension of input channel.
"""
struct TopKPool{T, S}
A::AbstractMatrix{T}
k::Int
p::AbstractVector{S}
Ã::AbstractMatrix{T}
end
function TopKPool(adj::AbstractMatrix, k::Int, in_channel::Int; init = glorot_uniform)
TopKPool(adj, k, init(in_channel), similar(adj, k, k))
end
(t::TopKPool)(x::AbstractArray) = topk_pool(t, x)
@doc raw"""
Set2Set(n_in, n_iters, n_layers = 1)
Set2Set layer from the paper [Order Matters: Sequence to sequence for sets](https://arxiv.org/abs/1511.06391).
For each graph in the batch, the layer computes an output vector of size `2*n_in` by iterating the following steps `n_iters` times:
```math
\mathbf{q} = \mathrm{LSTM}(\mathbf{q}_{t-1}^*)
\alpha_{i} = \frac{\exp(\mathbf{q}^T \mathbf{x}_i)}{\sum_{j=1}^N \exp(\mathbf{q}^T \mathbf{x}_j)}
\mathbf{r} = \sum_{i=1}^N \alpha_{i} \mathbf{x}_i
\mathbf{q}^*_t = [\mathbf{q}; \mathbf{r}]
```
where `N` is the number of nodes in the graph, `LSTM` is a Long-Short-Term-Memory network with `n_layers` layers,
input size `2*n_in` and output size `n_in`.
Given a batch of graphs `g` and node features `x`, the layer returns a matrix of size `(2*n_in, n_graphs)`.
```
"""
struct Set2Set{L} <: GNNLayer
lstm::L
num_iters::Int
end
Flux.@layer Set2Set
function Set2Set(n_in::Int, n_iters::Int, n_layers::Int = 1)
@assert n_layers >= 1
n_out = 2 * n_in
if n_layers == 1
lstm = LSTM(n_out => n_in)
else
layers = [LSTM(n_out => n_in)]
for _ in 2:n_layers
push!(layers, LSTM(n_in => n_in))
end
lstm = Chain(layers...)
end
return Set2Set(lstm, n_iters)
end
function (l::Set2Set)(g, x)
Flux.reset!(l.lstm)
return GNNlib.set2set_pool(l, g, x)
end
(l::Set2Set)(g::GNNGraph) = GNNGraph(g, gdata = l(g, node_features(g)))