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

Added node_shuffle #429

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/GNNGraphs/GNNGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export add_nodes,
to_unidirected,
random_walk_pe,
remove_nodes,
node_shuffle,
# from Flux
batch,
unbatch,
Expand Down
22 changes: 22 additions & 0 deletions src/GNNGraphs/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ function remove_nodes(g::GNNGraph{<:COO_T}, nodes_to_remove::AbstractVector)
ndata, edata, g.gdata)
end

"""
node_shuffle(g::GNNGraph{<:COO_T})

Shuffle the node features in the given GNNGraph as shown in paper [On Node Features for Graph Neural Networks](https://arxiv.org/ftp/arxiv/papers/1911/1911.08795.pdf)

# Arguments
- `g::GNNGraph`: The input graph represented as a GNNGraph.

# Returns
- `g::GNNGraph`: The GNNGraph with shuffled node features.
"""
function node_shuffle(g::GNNGraph{<:COO_T})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is mutating, so it should be called node_shuffle!

features = g.ndata.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can have no guarantess that there exists features named x

num_nodes = size(features, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the node dimension is the second one


shuffled_indices = randperm(num_nodes)
shuffled_features = features[shuffled_indices, :]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is shuffling feature dimensions, not nodes


g.ndata.x = shuffled_features
return g
end

"""
add_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
add_edges(g::GNNGraph, (s, t); [edata])
Expand Down
8 changes: 8 additions & 0 deletions test/GNNGraphs/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ end end
@test all(gnew.ndata.x[:, 7:11] .== 1)
end end

@testset "node_shuffle" begin if GRAPH_T == :coo
g = rand_graph(5, 4, ndata = rand(2, 5), graph_type = GRAPH_T)
g_new = node_shuffle(g)

@test g_new.num_edges == 4
@test g_new.num_nodes == 5
end end

@testset "remove_self_loops" begin if GRAPH_T == :coo # add_edges and set_edge_weight only implemented for coo
g = rand_graph(10, 20, graph_type = GRAPH_T)
g1 = add_edges(g, [1:5;], [1:5;])
Expand Down
Loading