-
Notifications
You must be signed in to change notification settings - Fork 51
Add topk features #260
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
Open
aurorarossi
wants to merge
19
commits into
JuliaGraphs:master
Choose a base branch
from
aurorarossi:add_topk_GPU
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add topk features #260
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f72cacb
Add functions
aurorarossi 92e7314
Add test
aurorarossi cc0a015
Fix functions
aurorarossi 6c1abb0
Export functions
aurorarossi 4d788f2
Fix
aurorarossi 9de994f
Simplify test
aurorarossi e69402e
Add docstrings
aurorarossi 6d2579f
Remove comments
aurorarossi e10c4a9
Add topk_edges tests
aurorarossi eec3a46
Fix batch case and reorder
aurorarossi b02dcaa
Modify test arbitrary node number case
aurorarossi ae88974
Merge branch 'add_topk_GPU' of https://github.com/aurorarossi/GraphNe…
aurorarossi 75b0b8f
Add tests like to DGL
aurorarossi 226b07d
Fix to return permutations
aurorarossi 24faa9a
Change name
aurorarossi 87f0430
Improve docs
aurorarossi da04648
Add example
aurorarossi 9c29257
Merge branch 'master' into add_topk_GPU
aurorarossi caa3b6e
Fix function signature
aurorarossi 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
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 |
---|---|---|
|
@@ -120,6 +120,81 @@ function broadcast_edges(g::GNNGraph, x) | |
return gather(x, gi) | ||
end | ||
|
||
function _sort_col(matrix::AbstractArray; rev::Bool = true, sortby::Int = 1) | ||
index = sortperm(view(matrix, sortby, :); rev) | ||
return matrix[:, index], index | ||
end | ||
|
||
function _topk_matrix(matrix::AbstractArray, k::Int; rev::Bool = true, sortby::Union{Nothing, Int} = nothing) | ||
if sortby === nothing | ||
sorted_matrix = sort(matrix, dims = 2; rev)[:, 1:k] | ||
vector_indices = map(x -> sortperm(x; rev), eachrow(matrix)) | ||
indices = reduce(vcat, vector_indices')[:, 1:k] | ||
return sorted_matrix, indices | ||
else | ||
sorted_matrix, indices = _sort_col(matrix; rev, sortby) | ||
return sorted_matrix[:, 1:k], indices[1:k] | ||
end | ||
end | ||
|
||
function _topk_batch(matrices::AbstractArray, k::Int; rev::Bool = true, | ||
sortby::Union{Nothing, Int} = nothing) | ||
num_graphs = length(matrices) | ||
num_feat = size(matrices[1], 1) | ||
sorted_matrix = map(x -> _topk_matrix(x, k; rev, sortby)[1], matrices) | ||
output_matrix = reshape(reduce(hcat, sorted_matrix), num_feat, k, num_graphs) | ||
indices = map(x -> _topk_matrix(x, k; rev, sortby)[2], matrices) | ||
if sortby === nothing | ||
output_indices = reshape(reduce(hcat, indices), num_feat, k, num_graphs) | ||
else | ||
output_indices = reshape(reduce(hcat, indices), k, 1, num_graphs) | ||
end | ||
return output_matrix, output_indices | ||
end | ||
|
||
""" | ||
topk_feature(g, feat, k; rev = true, sortby = nothing) | ||
|
||
Graph-wise top-`k` on feature array `x` according to the `sortby` index. | ||
Returns a tuple of the top-`k` features and their indices. | ||
|
||
# Arguments | ||
|
||
- `g`: a `GNNGraph``. | ||
- `feat`: a feature array of size `(number_features, g.num_nodes)` or `(number_features, g.num_edges)` of the graph `g`. | ||
- `k`: the number of top features to return. | ||
- `rev`: if `true`, sort in descending order otherwise returns the `k` smallest elements. | ||
- `sortby`: the index of the feature to sort by. If `nothing`, every row independently. | ||
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. this sentence is not clear |
||
|
||
# Examples | ||
|
||
```julia | ||
julia> g = rand_graph(5, 4, ndata = rand(3,5)); | ||
|
||
julia> g.ndata.x | ||
3×5 Matrix{Float64}: | ||
0.333661 0.683551 0.315145 0.794089 0.840085 | ||
0.263023 0.726028 0.626617 0.412247 0.0914052 | ||
0.296433 0.186584 0.960758 0.0999844 0.813808 | ||
|
||
julia> topk_feature(g, g.ndata.x, 2) | ||
([0.8400845757074524 0.7940891040468462; 0.7260276789396128 0.6266174187625888; 0.9607582005024967 0.8138081223752274], [5 4; 2 3; 3 5]) | ||
|
||
julia> topk_feature(g, g.ndata.x, 2; sortby=3) | ||
([0.3151452763177829 0.8400845757074524; 0.6266174187625888 0.09140519108918477; 0.9607582005024967 0.8138081223752274], [3, 5]) | ||
|
||
``` | ||
|
||
""" | ||
function topk_feature(g::GNNGraph, feat::AbstractArray, k::Int; rev::Bool = true, | ||
sortby::Union{Nothing, Int} = nothing) | ||
if g.num_graphs == 1 | ||
return _topk_matrix(feat, k; rev, sortby) | ||
else | ||
matrices = [feat[:, g.graph_indicator .== i] for i in 1:(g.num_graphs)] | ||
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. the masking would be different for edge feature |
||
return _topk_batch(matrices, k; rev, sortby) | ||
end | ||
end | ||
|
||
expand_srcdst(g::AbstractGNNGraph, x) = throw(ArgumentError("Invalid input type, expected matrix or tuple of matrices.")) | ||
expand_srcdst(g::AbstractGNNGraph, x::AbstractMatrix) = (x, x) | ||
|
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
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.
instead of sorting the whole matrix, it would be more efficient to use
partialsortperm
. I'm not sure is supported by CUDA.jl though