-
Notifications
You must be signed in to change notification settings - Fork 53
Adding Graphormer layer #275
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3a85359
added Graphormer.jl
5hv5hvnk a48e210
used struct for graphomer
5hv5hvnk 89b7193
added docstring
5hv5hvnk 53efa7d
changed struct to GraphormerLayer
5hv5hvnk 6aa987d
added tests
5hv5hvnk 6424a0f
resolving conflicts
5hv5hvnk 99813a6
Update src/layers/conv.jl
5hv5hvnk fac37ca
updated eq 5
5hv5hvnk ba9333c
added message function
5hv5hvnk 1c242ce
Merge branch 'CarloLucibello:master' into master
5hv5hvnk 7387752
updates on eq7
5hv5hvnk eab5896
Merge branch 'CarloLucibello:master' into master
5hv5hvnk 4517144
Merge branch 'CarloLucibello:master' into master
5hv5hvnk b8b0af7
added tests+improved on the code
5hv5hvnk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1688,3 +1688,83 @@ function Base.show(io::IO, l::TransformerConv) | |
(in, ein), out = l.channels | ||
print(io, "TransformerConv(($in, $ein) => $out, heads=$(l.heads))") | ||
end | ||
|
||
@doc raw""" | ||
GraphormerLayer constructor. | ||
|
||
Parameters: | ||
- `ch`: A `Pair` object representing the input and output channels of the layer. The input channel should be a tuple of the form `(in_channels, num_edge_features)`, where `in_channels` is the number of input node features and `num_edge_features` is the number of input edge features. The output channel should be an integer representing the number of output features for each node. | ||
- `σ`: The activation function to apply to the node features after the linear transformation. Defaults to `identity`. | ||
- `heads`: The number of attention heads to use. Defaults to 1. | ||
- `concat`: Whether to concatenate the output of each head or average them. Defaults to `true`. | ||
- `negative_slope`: The slope of the negative part of the LeakyReLU activation function. Defaults to 0.2. | ||
- `init`: The initialization function to use for the attention weights. Defaults to `glorot_uniform`. | ||
- `bias`: Whether to include a bias term in the linear transformation. Defaults to `true`. | ||
- `add_self_loops`: Whether to add self-loops to the graph. Defaults to `true`. | ||
|
||
Example: | ||
layer = GraphormerLayer((64, 32) => 128, σ = relu, heads = 4, concat = true, negative_slope = 0.1, init = xavier_uniform, bias = true, add_self_loops = false) | ||
""" | ||
struct GraphormerLayer{DX <: Dense, DE <: Union{Dense, Nothing}, T, A <: AbstractMatrix, F, B} <: GNNLayer | ||
dense_x::DX | ||
dense_e::DE | ||
bias::B | ||
a::A | ||
σ::F | ||
negative_slope::T | ||
channel::Pair{NTuple{2, Int}, Int} | ||
heads::Int | ||
concat::Bool | ||
add_self_loops::Bool | ||
end | ||
|
||
@functor GraphormerLayer | ||
|
||
Flux.trainable(l::GraphormerLayer) = (l.dense_x, l.dense_e, l.bias, l.a) | ||
5hv5hvnk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GraphormerLayer(ch::Pair{Int, Int}, args...; kws...) = GraphormerLayer((ch[1], 0) => ch[2], args...; kws...) | ||
|
||
function GraphormerLayer(ch::Pair{NTuple{2, Int}, Int}, σ = identity; | ||
heads::Int = 1, concat::Bool = true, negative_slope = 0.2, | ||
init = glorot_uniform, bias::Bool = true, add_self_loops = true) | ||
(in, ein), out = ch | ||
if add_self_loops | ||
@assert ein==0 "Using edge features and setting add_self_loops=true at the same time is not yet supported." | ||
end | ||
|
||
dense_x = Dense(in, out * heads, bias = false) | ||
dense_e = ein > 0 ? Dense(ein, out * heads, bias = false) : nothing | ||
b = bias ? Flux.create_bias(dense_x.weight, true, concat ? out * heads : out) : false | ||
a = init(ein > 0 ? 3out : 2out, heads) | ||
negative_slope = convert(Float32, negative_slope) | ||
GraphormerLayer(dense_x, dense_e, b, a, σ, negative_slope, ch, heads, concat, add_self_loops) | ||
end | ||
|
||
(l::GraphormerLayer)(g::GNNGraph) = GNNGraph(g, ndata = l(g, node_features(g), edge_features(g))) | ||
|
||
function (l::GraphormerLayer)(g::GNNGraph, x::AbstractMatrix, | ||
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" | ||
|
||
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) | ||
end | ||
|
||
_, chout = l.channel | ||
heads = l.heads | ||
|
||
Wx = l.dense_x(x) | ||
Wx = reshape(Wx, chout, heads, :) | ||
|
||
# a hand-written message passing | ||
m = apply_edges((xi, xj, e) -> message(l, xi, xj, e), g, Wx, Wx, e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is the message function defined? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies I missed writing it I added the comment to go back and add that let me get on to this |
||
α = softmax_edge_neighbors(g, m.logα) | ||
β = α .* m.Wxj | ||
x = aggregate_neighbors(g, +, β) | ||
|
||
if !l.concat | ||
x = mean(x, dims = 2) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make the docstring style consistent with the other layers