Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GATConv support for HeteroGraphConv #400

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,25 +409,37 @@ end

(l::GATConv)(g::GNNGraph) = GNNGraph(g, ndata = l(g, node_features(g), edge_features(g)))

function (l::GATConv)(g::GNNGraph, x::AbstractMatrix,
function (l::GATConv)(g::AbstractGNNGraph, x,
e::Union{Nothing, AbstractMatrix} = nothing)
check_num_nodes(g, x)
@assert !((e === nothing) && (l.dense_e !== nothing)) "Input edge features required for this layer"
@assert !((e !== nothing) && (l.dense_e === nothing)) "Input edge features were not specified in the layer constructor"

xj, xi = expand_srcdst(g, x)
edge_t = g isa GNNHeteroGraph ? g.etypes[1] : nothing
rbSparky marked this conversation as resolved.
Show resolved Hide resolved

if l.add_self_loops
@assert e===nothing "Using edge features and setting add_self_loops=true at the same time is not yet supported."
g = add_self_loops(g)
if g isa GNNHeteroGraph
g = add_self_loops(g, g.etypes[1])
else
g = add_self_loops(g)
end
end

_, chout = l.channel
heads = l.heads

Wx = l.dense_x(x)
Wx = reshape(Wx, chout, heads, :) # chout × nheads × nnodes
Wxi = Wxj = l.dense_x(xj)
Wxi = Wxj = reshape(Wxj, chout, heads, :)

if xi !== xj
Wxi = l.dense_x(xi)
Wxi = reshape(Wxi, chout, heads, :)
end

# a hand-written message passing
m = apply_edges((xi, xj, e) -> message(l, xi, xj, e), g, Wx, Wx, e)
m = apply_edges((xi, xj, e) -> message(l, xi, xj, e), g, Wxi, Wxj, e)
α = softmax_edge_neighbors(g, m.logα)
β = α .* m.Wxj
x = aggregate_neighbors(g, +, β)
Expand Down
10 changes: 8 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ Softmax over each node's neighborhood of the edge features `e`.
{\sum_{j'\in N(i)} e^{\mathbf{e}_{j'\to i}}}.
```
"""
function softmax_edge_neighbors(g::GNNGraph, e)
@assert size(e)[end] == g.num_edges
function softmax_edge_neighbors(g::AbstractGNNGraph, e)
if g isa GNNHeteroGraph
for (key, value) in g.num_edges
@assert size(e)[end] == value
end
else
@assert size(e)[end] == g.num_edges
end
s, t = edge_index(g)
max_ = gather(scatter(max, e, t), t)
num = exp.(e .- max_)
Expand Down
8 changes: 8 additions & 0 deletions test/layers/heteroconv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@
@test size(y.A) == (2, 2) && size(y.B) == (2, 3)
end

@testset "GATConv" begin
x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3))
layers = HeteroGraphConv((:A, :to, :B) => GATConv(4 => 2),
(:B, :to, :A) => GATConv(4 => 2));
y = layers(hg, x);
@test size(y.A) == (2, 2) && size(y.B) == (2, 3)
end

@testset "GINConv" begin
x = (A = rand(4, 2), B = rand(4, 3))
layers = HeteroGraphConv((:A, :to, :B) => GINConv(Dense(4, 2), 0.4),
Expand Down
Loading