GNNGraph
Documentation page for the graph type GNNGraph
provided by GNNGraphs.jl and related methods.
Besides the methods documented here, one can rely on the large set of functionalities given by Graphs.jl thanks to the fact that GNNGraph
inherits from Graphs.AbstractGraph
.
Index
GNNGraphs.DataStore
GNNGraphs.GNNGraph
Base.copy
Base.intersect
GNNGraphs.add_edges
GNNGraphs.add_edges
GNNGraphs.add_nodes
GNNGraphs.add_self_loops
GNNGraphs.add_self_loops
GNNGraphs.adjacency_list
GNNGraphs.color_refinement
GNNGraphs.edge_index
GNNGraphs.edge_index
GNNGraphs.getgraph
GNNGraphs.graph_indicator
GNNGraphs.graph_indicator
GNNGraphs.has_isolated_nodes
GNNGraphs.has_multi_edges
GNNGraphs.is_bidirected
GNNGraphs.khop_adj
GNNGraphs.knn_graph
GNNGraphs.laplacian_lambda_max
GNNGraphs.negative_sample
GNNGraphs.normalized_laplacian
GNNGraphs.perturb_edges
GNNGraphs.ppr_diffusion
GNNGraphs.radius_graph
GNNGraphs.rand_bipartite_heterograph
GNNGraphs.rand_edge_split
GNNGraphs.rand_graph
GNNGraphs.rand_heterograph
GNNGraphs.random_walk_pe
GNNGraphs.remove_edges
GNNGraphs.remove_multi_edges
GNNGraphs.remove_nodes
GNNGraphs.remove_nodes
GNNGraphs.remove_self_loops
GNNGraphs.sample_neighbors
GNNGraphs.scaled_laplacian
GNNGraphs.set_edge_weight
GNNGraphs.sort_edge_index
GNNGraphs.to_bidirected
GNNGraphs.to_unidirected
Graphs.LinAlg.adjacency_matrix
Graphs.degree
Graphs.degree
Graphs.has_self_loops
Graphs.induced_subgraph
Graphs.inneighbors
Graphs.neighbors
Graphs.outneighbors
MLUtils.batch
MLUtils.unbatch
SparseArrays.blockdiag
GNNGraph type
GNNGraphs.GNNGraph
— TypeGNNGraph(data; [graph_type, ndata, edata, gdata, num_nodes, graph_indicator, dir])
+GNNGraph · GNNGraphs.jl GNNGraph
Documentation page for the graph type GNNGraph
provided by GNNGraphs.jl and related methods.
Besides the methods documented here, one can rely on the large set of functionalities given by Graphs.jl thanks to the fact that GNNGraph
inherits from Graphs.AbstractGraph
.
Index
GNNGraphs.DataStore
GNNGraphs.GNNGraph
Base.copy
Base.intersect
GNNGraphs.add_edges
GNNGraphs.add_edges
GNNGraphs.add_nodes
GNNGraphs.add_self_loops
GNNGraphs.add_self_loops
GNNGraphs.adjacency_list
GNNGraphs.color_refinement
GNNGraphs.edge_index
GNNGraphs.edge_index
GNNGraphs.getgraph
GNNGraphs.graph_indicator
GNNGraphs.graph_indicator
GNNGraphs.has_isolated_nodes
GNNGraphs.has_multi_edges
GNNGraphs.is_bidirected
GNNGraphs.khop_adj
GNNGraphs.knn_graph
GNNGraphs.laplacian_lambda_max
GNNGraphs.negative_sample
GNNGraphs.normalized_laplacian
GNNGraphs.perturb_edges
GNNGraphs.ppr_diffusion
GNNGraphs.radius_graph
GNNGraphs.rand_bipartite_heterograph
GNNGraphs.rand_edge_split
GNNGraphs.rand_graph
GNNGraphs.rand_heterograph
GNNGraphs.random_walk_pe
GNNGraphs.remove_edges
GNNGraphs.remove_multi_edges
GNNGraphs.remove_nodes
GNNGraphs.remove_nodes
GNNGraphs.remove_self_loops
GNNGraphs.sample_neighbors
GNNGraphs.scaled_laplacian
GNNGraphs.set_edge_weight
GNNGraphs.sort_edge_index
GNNGraphs.to_bidirected
GNNGraphs.to_unidirected
Graphs.LinAlg.adjacency_matrix
Graphs.degree
Graphs.degree
Graphs.has_self_loops
Graphs.induced_subgraph
Graphs.inneighbors
Graphs.neighbors
Graphs.outneighbors
MLUtils.batch
MLUtils.unbatch
SparseArrays.blockdiag
GNNGraph type
GNNGraphs.GNNGraph
— TypeGNNGraph(data; [graph_type, ndata, edata, gdata, num_nodes, graph_indicator, dir])
GNNGraph(g::GNNGraph; [ndata, edata, gdata])
A type representing a graph structure that also stores feature arrays associated to nodes, edges, and the graph itself.
The feature arrays are stored in the fields ndata
, edata
, and gdata
as DataStore
objects offering a convenient dictionary-like and namedtuple-like interface. The features can be passed at construction time or added later.
A GNNGraph
can be constructed out of different data
objects expressing the connections inside the graph. The internal representation type is determined by graph_type
.
When constructed from another GNNGraph
, the internal graph representation is preserved and shared. The node/edge/graph features are retained as well, unless explicitely set by the keyword arguments ndata
, edata
, and gdata
.
A GNNGraph
can also represent multiple graphs batched togheter (see MLUtils.batch
or SparseArrays.blockdiag
). The field g.graph_indicator
contains the graph membership of each node.
GNNGraph
s are always directed graphs, therefore each edge is defined by a source node and a target node (see edge_index
). Self loops (edges connecting a node to itself) and multiple edges (more than one edge between the same pair of nodes) are supported.
A GNNGraph
is a Graphs.jl's AbstractGraph
, therefore it supports most functionality from that library.
Arguments
data
: Some data representing the graph topology. Possible type are- An adjacency matrix
- An adjacency list.
- A tuple containing the source and target vectors (COO representation)
- A Graphs.jl' graph.
graph_type
: A keyword argument that specifies the underlying representation used by the GNNGraph. Currently supported values are:coo
. Graph represented as a tuple (source, target)
, such that the k
-th edge connects the node source[k]
to node target[k]
. Optionally, also edge weights can be given: (source, target, weights)
.:sparse
. A sparse adjacency matrix representation.:dense
. A dense adjacency matrix representation.
Defaults to :coo
, currently the most supported type.dir
: The assumed edge direction when given adjacency matrix or adjacency list input data g
. Possible values are :out
and :in
. Default :out
.num_nodes
: The number of nodes. If not specified, inferred from g
. Default nothing
.graph_indicator
: For batched graphs, a vector containing the graph assignment of each node. Default nothing
.ndata
: Node features. An array or named tuple of arrays whose last dimension has size num_nodes
.edata
: Edge features. An array or named tuple of arrays whose last dimension has size num_edges
.gdata
: Graph features. An array or named tuple of arrays whose last dimension has size num_graphs
.
Examples
using GraphNeuralNetworks
# Construct from adjacency list representation
@@ -34,7 +34,7 @@
# Both source and target are vectors of length num_edges
source, target = edge_index(g)
A GNNGraph
can be sent to the GPU using e.g. Flux's gpu
function:
# Send to gpu
using Flux, CUDA
-g = g |> Flux.gpu
sourceBase.copy
— Functioncopy(g::GNNGraph; deep=false)
Create a copy of g
. If deep
is true
, then copy will be a deep copy (equivalent to deepcopy(g)
), otherwise it will be a shallow copy with the same underlying graph data.
sourceDataStore
GNNGraphs.DataStore
— TypeDataStore([n, data])
+g = g |> Flux.gpu
sourceBase.copy
— Functioncopy(g::GNNGraph; deep=false)
Create a copy of g
. If deep
is true
, then copy will be a deep copy (equivalent to deepcopy(g)
), otherwise it will be a shallow copy with the same underlying graph data.
sourceDataStore
GNNGraphs.DataStore
— TypeDataStore([n, data])
DataStore([n,] k1 = x1, k2 = x2, ...)
A container for feature arrays. The optional argument n
enforces that numobs(x) == n
for each array contained in the datastore.
At construction time, the data
can be provided as any iterables of pairs of symbols and arrays or as keyword arguments:
julia> ds = DataStore(3, x = rand(Float32, 2, 3), y = rand(Float32, 3))
DataStore(3) with 2 elements:
y = 3-element Vector{Float32}
@@ -78,8 +78,8 @@
julia> ds2.a
2-element Vector{Float64}:
1.0
- 1.0
sourceQuery
GNNGraphs.adjacency_list
— Methodadjacency_list(g; dir=:out)
-adjacency_list(g, nodes; dir=:out)
Return the adjacency list representation (a vector of vectors) of the graph g
.
Calling a
the adjacency list, if dir=:out
than a[i]
will contain the neighbors of node i
through outgoing edges. If dir=:in
, it will contain neighbors from incoming edges instead.
If nodes
is given, return the neighborhood of the nodes in nodes
only.
sourceGNNGraphs.edge_index
— Methodedge_index(g::GNNGraph)
Return a tuple containing two vectors, respectively storing the source and target nodes for each edges in g
.
s, t = edge_index(g)
sourceGNNGraphs.edge_index
— Methodedge_index(g::GNNHeteroGraph, [edge_t])
Return a tuple containing two vectors, respectively storing the source and target nodes for each edges in g
of type edge_t = (src_t, rel_t, trg_t)
.
If edge_t
is not provided, it will error if g
has more than one edge type.
sourceGNNGraphs.graph_indicator
— Methodgraph_indicator(g::GNNGraph; edges=false)
Return a vector containing the graph membership (an integer from 1
to g.num_graphs
) of each node in the graph. If edges=true
, return the graph membership of each edge instead.
sourceGNNGraphs.graph_indicator
— Methodgraph_indicator(g::GNNHeteroGraph, [node_t])
Return a Dict of vectors containing the graph membership (an integer from 1
to g.num_graphs
) of each node in the graph for each node type. If node_t
is provided, return the graph membership of each node of type node_t
instead.
See also batch
.
sourceGNNGraphs.has_isolated_nodes
— Methodhas_isolated_nodes(g::GNNGraph; dir=:out)
Return true if the graph g
contains nodes with out-degree (if dir=:out
) or in-degree (if dir = :in
) equal to zero.
sourceGNNGraphs.has_multi_edges
— Methodhas_multi_edges(g::GNNGraph)
Return true
if g
has any multiple edges.
sourceGNNGraphs.is_bidirected
— Methodis_bidirected(g::GNNGraph)
Check if the directed graph g
essentially corresponds to an undirected graph, i.e. if for each edge it also contains the reverse edge.
sourceGNNGraphs.khop_adj
— Functionkhop_adj(g::GNNGraph,k::Int,T::DataType=eltype(g); dir=:out, weighted=true)
Return $A^k$ where $A$ is the adjacency matrix of the graph 'g'.
sourceGNNGraphs.laplacian_lambda_max
— Functionlaplacian_lambda_max(g::GNNGraph, T=Float32; add_self_loops=false, dir=:out)
Return the largest eigenvalue of the normalized symmetric Laplacian of the graph g
.
If the graph is batched from multiple graphs, return the list of the largest eigenvalue for each graph.
sourceGNNGraphs.normalized_laplacian
— Functionnormalized_laplacian(g, T=Float32; add_self_loops=false, dir=:out)
Normalized Laplacian matrix of graph g
.
Arguments
g
: A GNNGraph
.T
: result element type.add_self_loops
: add self-loops while calculating the matrix.dir
: the edge directionality considered (:out, :in, :both).
sourceGNNGraphs.scaled_laplacian
— Functionscaled_laplacian(g, T=Float32; dir=:out)
Scaled Laplacian matrix of graph g
, defined as $\hat{L} = \frac{2}{\lambda_{max}} L - I$ where $L$ is the normalized Laplacian matrix.
Arguments
g
: A GNNGraph
.T
: result element type.dir
: the edge directionality considered (:out, :in, :both).
sourceGraphs.LinAlg.adjacency_matrix
— Functionadjacency_matrix(g::GNNGraph, T=eltype(g); dir=:out, weighted=true)
Return the adjacency matrix A
for the graph g
.
If dir=:out
, A[i,j] > 0
denotes the presence of an edge from node i
to node j
. If dir=:in
instead, A[i,j] > 0
denotes the presence of an edge from node j
to node i
.
User may specify the eltype T
of the returned matrix.
If weighted=true
, the A
will contain the edge weights if any, otherwise the elements of A
will be either 0 or 1.
sourceGraphs.degree
— Methoddegree(g::GNNGraph, T=nothing; dir=:out, edge_weight=true)
Return a vector containing the degrees of the nodes in g
.
The gradient is propagated through this function only if edge_weight
is true
or a vector.
Arguments
g
: A graph.T
: Element type of the returned vector. If nothing
, is chosen based on the graph type and will be an integer if edge_weight = false
. Default nothing
.dir
: For dir = :out
the degree of a node is counted based on the outgoing edges. For dir = :in
, the ingoing edges are used. If dir = :both
we have the sum of the two.edge_weight
: If true
and the graph contains weighted edges, the degree will be weighted. Set to false
instead to just count the number of outgoing/ingoing edges. Finally, you can also pass a vector of weights to be used instead of the graph's own weights. Default true
.
sourceGraphs.degree
— Methoddegree(g::GNNHeteroGraph, edge_type::EType; dir = :in)
Return a vector containing the degrees of the nodes in g
GNNHeteroGraph given edge_type
.
Arguments
g
: A graph.edge_type
: A tuple of symbols (source_t, edge_t, target_t)
representing the edge type.T
: Element type of the returned vector. If nothing
, is chosen based on the graph type. Default nothing
.dir
: For dir = :out
the degree of a node is counted based on the outgoing edges. For dir = :in
, the ingoing edges are used. If dir = :both
we have the sum of the two. Default dir = :out
.
sourceGraphs.has_self_loops
— Methodhas_self_loops(g::GNNGraph)
Return true
if g
has any self loops.
sourceGraphs.inneighbors
— Methodinneighbors(g::GNNGraph, i::Integer)
Return the neighbors of node i
in the graph g
through incoming edges.
See also neighbors
and outneighbors
.
sourceGraphs.outneighbors
— Methodoutneighbors(g::GNNGraph, i::Integer)
Return the neighbors of node i
in the graph g
through outgoing edges.
See also neighbors
and inneighbors
.
sourceGraphs.neighbors
— Methodneighbors(g::GNNGraph, i::Integer; dir=:out)
Return the neighbors of node i
in the graph g
. If dir=:out
, return the neighbors through outgoing edges. If dir=:in
, return the neighbors through incoming edges.
See also outneighbors
, inneighbors
.
sourceTransform
GNNGraphs.add_edges
— Methodadd_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
+ 1.0
sourceQuery
GNNGraphs.adjacency_list
— Methodadjacency_list(g; dir=:out)
+adjacency_list(g, nodes; dir=:out)
Return the adjacency list representation (a vector of vectors) of the graph g
.
Calling a
the adjacency list, if dir=:out
than a[i]
will contain the neighbors of node i
through outgoing edges. If dir=:in
, it will contain neighbors from incoming edges instead.
If nodes
is given, return the neighborhood of the nodes in nodes
only.
sourceGNNGraphs.edge_index
— Methodedge_index(g::GNNGraph)
Return a tuple containing two vectors, respectively storing the source and target nodes for each edges in g
.
s, t = edge_index(g)
sourceGNNGraphs.edge_index
— Methodedge_index(g::GNNHeteroGraph, [edge_t])
Return a tuple containing two vectors, respectively storing the source and target nodes for each edges in g
of type edge_t = (src_t, rel_t, trg_t)
.
If edge_t
is not provided, it will error if g
has more than one edge type.
sourceGNNGraphs.graph_indicator
— Methodgraph_indicator(g::GNNGraph; edges=false)
Return a vector containing the graph membership (an integer from 1
to g.num_graphs
) of each node in the graph. If edges=true
, return the graph membership of each edge instead.
sourceGNNGraphs.graph_indicator
— Methodgraph_indicator(g::GNNHeteroGraph, [node_t])
Return a Dict of vectors containing the graph membership (an integer from 1
to g.num_graphs
) of each node in the graph for each node type. If node_t
is provided, return the graph membership of each node of type node_t
instead.
See also batch
.
sourceGNNGraphs.has_isolated_nodes
— Methodhas_isolated_nodes(g::GNNGraph; dir=:out)
Return true if the graph g
contains nodes with out-degree (if dir=:out
) or in-degree (if dir = :in
) equal to zero.
sourceGNNGraphs.has_multi_edges
— Methodhas_multi_edges(g::GNNGraph)
Return true
if g
has any multiple edges.
sourceGNNGraphs.is_bidirected
— Methodis_bidirected(g::GNNGraph)
Check if the directed graph g
essentially corresponds to an undirected graph, i.e. if for each edge it also contains the reverse edge.
sourceGNNGraphs.khop_adj
— Functionkhop_adj(g::GNNGraph,k::Int,T::DataType=eltype(g); dir=:out, weighted=true)
Return $A^k$ where $A$ is the adjacency matrix of the graph 'g'.
sourceGNNGraphs.laplacian_lambda_max
— Functionlaplacian_lambda_max(g::GNNGraph, T=Float32; add_self_loops=false, dir=:out)
Return the largest eigenvalue of the normalized symmetric Laplacian of the graph g
.
If the graph is batched from multiple graphs, return the list of the largest eigenvalue for each graph.
sourceGNNGraphs.normalized_laplacian
— Functionnormalized_laplacian(g, T=Float32; add_self_loops=false, dir=:out)
Normalized Laplacian matrix of graph g
.
Arguments
g
: A GNNGraph
.T
: result element type.add_self_loops
: add self-loops while calculating the matrix.dir
: the edge directionality considered (:out, :in, :both).
sourceGNNGraphs.scaled_laplacian
— Functionscaled_laplacian(g, T=Float32; dir=:out)
Scaled Laplacian matrix of graph g
, defined as $\hat{L} = \frac{2}{\lambda_{max}} L - I$ where $L$ is the normalized Laplacian matrix.
Arguments
g
: A GNNGraph
.T
: result element type.dir
: the edge directionality considered (:out, :in, :both).
sourceGraphs.LinAlg.adjacency_matrix
— Functionadjacency_matrix(g::GNNGraph, T=eltype(g); dir=:out, weighted=true)
Return the adjacency matrix A
for the graph g
.
If dir=:out
, A[i,j] > 0
denotes the presence of an edge from node i
to node j
. If dir=:in
instead, A[i,j] > 0
denotes the presence of an edge from node j
to node i
.
User may specify the eltype T
of the returned matrix.
If weighted=true
, the A
will contain the edge weights if any, otherwise the elements of A
will be either 0 or 1.
sourceGraphs.degree
— Methoddegree(g::GNNGraph, T=nothing; dir=:out, edge_weight=true)
Return a vector containing the degrees of the nodes in g
.
The gradient is propagated through this function only if edge_weight
is true
or a vector.
Arguments
g
: A graph.T
: Element type of the returned vector. If nothing
, is chosen based on the graph type and will be an integer if edge_weight = false
. Default nothing
.dir
: For dir = :out
the degree of a node is counted based on the outgoing edges. For dir = :in
, the ingoing edges are used. If dir = :both
we have the sum of the two.edge_weight
: If true
and the graph contains weighted edges, the degree will be weighted. Set to false
instead to just count the number of outgoing/ingoing edges. Finally, you can also pass a vector of weights to be used instead of the graph's own weights. Default true
.
sourceGraphs.degree
— Methoddegree(g::GNNHeteroGraph, edge_type::EType; dir = :in)
Return a vector containing the degrees of the nodes in g
GNNHeteroGraph given edge_type
.
Arguments
g
: A graph.edge_type
: A tuple of symbols (source_t, edge_t, target_t)
representing the edge type.T
: Element type of the returned vector. If nothing
, is chosen based on the graph type. Default nothing
.dir
: For dir = :out
the degree of a node is counted based on the outgoing edges. For dir = :in
, the ingoing edges are used. If dir = :both
we have the sum of the two. Default dir = :out
.
sourceGraphs.has_self_loops
— Methodhas_self_loops(g::GNNGraph)
Return true
if g
has any self loops.
sourceGraphs.inneighbors
— Methodinneighbors(g::GNNGraph, i::Integer)
Return the neighbors of node i
in the graph g
through incoming edges.
See also neighbors
and outneighbors
.
sourceGraphs.outneighbors
— Methodoutneighbors(g::GNNGraph, i::Integer)
Return the neighbors of node i
in the graph g
through outgoing edges.
See also neighbors
and inneighbors
.
sourceGraphs.neighbors
— Methodneighbors(g::GNNGraph, i::Integer; dir=:out)
Return the neighbors of node i
in the graph g
. If dir=:out
, return the neighbors through outgoing edges. If dir=:in
, return the neighbors through incoming edges.
See also outneighbors
, inneighbors
.
sourceTransform
GNNGraphs.add_edges
— Methodadd_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
add_edges(g::GNNGraph, (s, t); [edata])
add_edges(g::GNNGraph, (s, t, w); [edata])
Add to graph g
the edges with source nodes s
and target nodes t
. Optionally, pass the edge weight w
and the features edata
for the new edges. Returns a new graph sharing part of the underlying data with g
.
If the s
or t
contain nodes that are not already present in the graph, they are added to the graph as well.
Examples
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
@@ -101,12 +101,12 @@
julia> add_edges(g, [1,2], [2,3])
GNNGraph:
num_nodes: 3
- num_edges: 2
sourceGNNGraphs.add_edges
— Methodadd_edges(g::GNNHeteroGraph, edge_t, s, t; [edata, num_nodes])
+ num_edges: 2
sourceGNNGraphs.add_edges
— Methodadd_edges(g::GNNHeteroGraph, edge_t, s, t; [edata, num_nodes])
add_edges(g::GNNHeteroGraph, edge_t => (s, t); [edata, num_nodes])
-add_edges(g::GNNHeteroGraph, edge_t => (s, t, w); [edata, num_nodes])
Add to heterograph g
edges of type edge_t
with source node vector s
and target node vector t
. Optionally, pass the edge weights w
or the features edata
for the new edges. edge_t
is a triplet of symbols (src_t, rel_t, dst_t)
.
If the edge type is not already present in the graph, it is added. If it involves new node types, they are added to the graph as well. In this case, a dictionary or named tuple of num_nodes
can be passed to specify the number of nodes of the new types, otherwise the number of nodes is inferred from the maximum node id in s
and t
.
sourceGNNGraphs.add_nodes
— Methodadd_nodes(g::GNNGraph, n; [ndata])
Add n
new nodes to graph g
. In the new graph, these nodes will have indexes from g.num_nodes + 1
to g.num_nodes + n
.
sourceGNNGraphs.add_self_loops
— Methodadd_self_loops(g::GNNGraph)
Return a graph with the same features as g
but also adding edges connecting the nodes to themselves.
Nodes with already existing self-loops will obtain a second self-loop.
If the graphs has edge weights, the new edges will have weight 1.
sourceGNNGraphs.add_self_loops
— Methodadd_self_loops(g::GNNHeteroGraph, edge_t::EType)
-add_self_loops(g::GNNHeteroGraph)
If the source node type is the same as the destination node type in edge_t
, return a graph with the same features as g
but also add self-loops of the specified type, edge_t
. Otherwise, it returns g
unchanged.
Nodes with already existing self-loops of type edge_t
will obtain a second set of self-loops of the same type.
If the graph has edge weights for edges of type edge_t
, the new edges will have weight 1.
If no edges of type edge_t
exist, or all existing edges have no weight, then all new self loops will have no weight.
If edge_t
is not passed as argument, for the entire graph self-loop is added to each node for every edge type in the graph where the source and destination node types are the same. This iterates over all edge types present in the graph, applying the self-loop addition logic to each applicable edge type.
sourceGNNGraphs.getgraph
— Methodgetgraph(g::GNNGraph, i; nmap=false)
Return the subgraph of g
induced by those nodes j
for which g.graph_indicator[j] == i
or, if i
is a collection, g.graph_indicator[j] ∈ i
. In other words, it extract the component graphs from a batched graph.
If nmap=true
, return also a vector v
mapping the new nodes to the old ones. The node i
in the subgraph will correspond to the node v[i]
in g
.
sourceGNNGraphs.negative_sample
— Methodnegative_sample(g::GNNGraph;
+add_edges(g::GNNHeteroGraph, edge_t => (s, t, w); [edata, num_nodes])
Add to heterograph g
edges of type edge_t
with source node vector s
and target node vector t
. Optionally, pass the edge weights w
or the features edata
for the new edges. edge_t
is a triplet of symbols (src_t, rel_t, dst_t)
.
If the edge type is not already present in the graph, it is added. If it involves new node types, they are added to the graph as well. In this case, a dictionary or named tuple of num_nodes
can be passed to specify the number of nodes of the new types, otherwise the number of nodes is inferred from the maximum node id in s
and t
.
sourceGNNGraphs.add_nodes
— Methodadd_nodes(g::GNNGraph, n; [ndata])
Add n
new nodes to graph g
. In the new graph, these nodes will have indexes from g.num_nodes + 1
to g.num_nodes + n
.
sourceGNNGraphs.add_self_loops
— Methodadd_self_loops(g::GNNGraph)
Return a graph with the same features as g
but also adding edges connecting the nodes to themselves.
Nodes with already existing self-loops will obtain a second self-loop.
If the graphs has edge weights, the new edges will have weight 1.
sourceGNNGraphs.add_self_loops
— Methodadd_self_loops(g::GNNHeteroGraph, edge_t::EType)
+add_self_loops(g::GNNHeteroGraph)
If the source node type is the same as the destination node type in edge_t
, return a graph with the same features as g
but also add self-loops of the specified type, edge_t
. Otherwise, it returns g
unchanged.
Nodes with already existing self-loops of type edge_t
will obtain a second set of self-loops of the same type.
If the graph has edge weights for edges of type edge_t
, the new edges will have weight 1.
If no edges of type edge_t
exist, or all existing edges have no weight, then all new self loops will have no weight.
If edge_t
is not passed as argument, for the entire graph self-loop is added to each node for every edge type in the graph where the source and destination node types are the same. This iterates over all edge types present in the graph, applying the self-loop addition logic to each applicable edge type.
sourceGNNGraphs.getgraph
— Methodgetgraph(g::GNNGraph, i; nmap=false)
Return the subgraph of g
induced by those nodes j
for which g.graph_indicator[j] == i
or, if i
is a collection, g.graph_indicator[j] ∈ i
. In other words, it extract the component graphs from a batched graph.
If nmap=true
, return also a vector v
mapping the new nodes to the old ones. The node i
in the subgraph will correspond to the node v[i]
in g
.
sourceGNNGraphs.negative_sample
— Methodnegative_sample(g::GNNGraph;
num_neg_edges = g.num_edges,
- bidirected = is_bidirected(g))
Return a graph containing random negative edges (i.e. non-edges) from graph g
as edges.
If bidirected=true
, the output graph will be bidirected and there will be no leakage from the origin graph.
See also is_bidirected
.
sourceGNNGraphs.perturb_edges
— Methodperturb_edges([rng], g::GNNGraph, perturb_ratio)
Return a new graph obtained from g
by adding random edges, based on a specified perturb_ratio
. The perturb_ratio
determines the fraction of new edges to add relative to the current number of edges in the graph. These new edges are added without creating self-loops.
The function returns a new GNNGraph
instance that shares some of the underlying data with g
but includes the additional edges. The nodes for the new edges are selected randomly, and no edge data (edata
) or weights (w
) are assigned to these new edges.
Arguments
g::GNNGraph
: The graph to be perturbed.perturb_ratio
: The ratio of the number of new edges to add relative to the current number of edges in the graph. For example, a perturb_ratio
of 0.1 means that 10% of the current number of edges will be added as new random edges.rng
: An optionalrandom number generator to ensure reproducible results.
Examples
julia> g = GNNGraph((s, t, w))
+ bidirected = is_bidirected(g))
Return a graph containing random negative edges (i.e. non-edges) from graph g
as edges.
If bidirected=true
, the output graph will be bidirected and there will be no leakage from the origin graph.
See also is_bidirected
.
sourceGNNGraphs.perturb_edges
— Methodperturb_edges([rng], g::GNNGraph, perturb_ratio)
Return a new graph obtained from g
by adding random edges, based on a specified perturb_ratio
. The perturb_ratio
determines the fraction of new edges to add relative to the current number of edges in the graph. These new edges are added without creating self-loops.
The function returns a new GNNGraph
instance that shares some of the underlying data with g
but includes the additional edges. The nodes for the new edges are selected randomly, and no edge data (edata
) or weights (w
) are assigned to these new edges.
Arguments
g::GNNGraph
: The graph to be perturbed.perturb_ratio
: The ratio of the number of new edges to add relative to the current number of edges in the graph. For example, a perturb_ratio
of 0.1 means that 10% of the current number of edges will be added as new random edges.rng
: An optionalrandom number generator to ensure reproducible results.
Examples
julia> g = GNNGraph((s, t, w))
GNNGraph:
num_nodes: 4
num_edges: 5
@@ -114,7 +114,7 @@
julia> perturbed_g = perturb_edges(g, 0.2)
GNNGraph:
num_nodes: 4
- num_edges: 6
sourceGNNGraphs.ppr_diffusion
— Methodppr_diffusion(g::GNNGraph{<:COO_T}, alpha =0.85f0) -> GNNGraph
Calculates the Personalized PageRank (PPR) diffusion based on the edge weight matrix of a GNNGraph and updates the graph with new edge weights derived from the PPR matrix. References paper: The pagerank citation ranking: Bringing order to the web
The function performs the following steps:
- Constructs a modified adjacency matrix
A
using the graph's edge weights, where A
is adjusted by (α - 1) * A + I
, with α
being the damping factor (alpha_f32
) and I
the identity matrix. - Normalizes
A
to ensure each column sums to 1, representing transition probabilities. - Applies the PPR formula
α * (I + (α - 1) * A)^-1
to compute the diffusion matrix. - Updates the original edge weights of the graph based on the PPR diffusion matrix, assigning new weights for each edge from the PPR matrix.
Arguments
g::GNNGraph
: The input graph for which PPR diffusion is to be calculated. It should have edge weights available.alpha_f32::Float32
: The damping factor used in PPR calculation, controlling the teleport probability in the random walk. Defaults to 0.85f0
.
Returns
- A new
GNNGraph
instance with the same structure as g
but with updated edge weights according to the PPR diffusion calculation.
sourceGNNGraphs.rand_edge_split
— Methodrand_edge_split(g::GNNGraph, frac; bidirected=is_bidirected(g)) -> g1, g2
Randomly partition the edges in g
to form two graphs, g1
and g2
. Both will have the same number of nodes as g
. g1
will contain a fraction frac
of the original edges, while g2
wil contain the rest.
If bidirected = true
makes sure that an edge and its reverse go into the same split. This option is supported only for bidirected graphs with no self-loops and multi-edges.
rand_edge_split
is tipically used to create train/test splits in link prediction tasks.
sourceGNNGraphs.random_walk_pe
— Methodrandom_walk_pe(g, walk_length)
Return the random walk positional encoding from the paper Graph Neural Networks with Learnable Structural and Positional Representations of the given graph g
and the length of the walk walk_length
as a matrix of size (walk_length, g.num_nodes)
.
sourceGNNGraphs.remove_edges
— Methodremove_edges(g::GNNGraph, edges_to_remove::AbstractVector{<:Integer})
+ num_edges: 6
sourceGNNGraphs.ppr_diffusion
— Methodppr_diffusion(g::GNNGraph{<:COO_T}, alpha =0.85f0) -> GNNGraph
Calculates the Personalized PageRank (PPR) diffusion based on the edge weight matrix of a GNNGraph and updates the graph with new edge weights derived from the PPR matrix. References paper: The pagerank citation ranking: Bringing order to the web
The function performs the following steps:
- Constructs a modified adjacency matrix
A
using the graph's edge weights, where A
is adjusted by (α - 1) * A + I
, with α
being the damping factor (alpha_f32
) and I
the identity matrix. - Normalizes
A
to ensure each column sums to 1, representing transition probabilities. - Applies the PPR formula
α * (I + (α - 1) * A)^-1
to compute the diffusion matrix. - Updates the original edge weights of the graph based on the PPR diffusion matrix, assigning new weights for each edge from the PPR matrix.
Arguments
g::GNNGraph
: The input graph for which PPR diffusion is to be calculated. It should have edge weights available.alpha_f32::Float32
: The damping factor used in PPR calculation, controlling the teleport probability in the random walk. Defaults to 0.85f0
.
Returns
- A new
GNNGraph
instance with the same structure as g
but with updated edge weights according to the PPR diffusion calculation.
sourceGNNGraphs.rand_edge_split
— Methodrand_edge_split(g::GNNGraph, frac; bidirected=is_bidirected(g)) -> g1, g2
Randomly partition the edges in g
to form two graphs, g1
and g2
. Both will have the same number of nodes as g
. g1
will contain a fraction frac
of the original edges, while g2
wil contain the rest.
If bidirected = true
makes sure that an edge and its reverse go into the same split. This option is supported only for bidirected graphs with no self-loops and multi-edges.
rand_edge_split
is tipically used to create train/test splits in link prediction tasks.
sourceGNNGraphs.random_walk_pe
— Methodrandom_walk_pe(g, walk_length)
Return the random walk positional encoding from the paper Graph Neural Networks with Learnable Structural and Positional Representations of the given graph g
and the length of the walk walk_length
as a matrix of size (walk_length, g.num_nodes)
.
sourceGNNGraphs.remove_edges
— Methodremove_edges(g::GNNGraph, edges_to_remove::AbstractVector{<:Integer})
remove_edges(g::GNNGraph, p=0.5)
Remove specified edges from a GNNGraph, either by specifying edge indices or by randomly removing edges with a given probability.
Arguments
g
: The input graph from which edges will be removed.edges_to_remove
: Vector of edge indices to be removed. This argument is only required for the first method.p
: Probability of removing each edge. This argument is only required for the second method and defaults to 0.5.
Returns
A new GNNGraph with the specified edges removed.
Example
julia> using GraphNeuralNetworks
# Construct a GNNGraph
@@ -137,7 +137,7 @@
julia> g_new
GNNGraph:
num_nodes: 3
- num_edges: 2
sourceGNNGraphs.remove_multi_edges
— Methodremove_multi_edges(g::GNNGraph; aggr=+)
Remove multiple edges (also called parallel edges or repeated edges) from graph g
. Possible edge features are aggregated according to aggr
, that can take value +
,min
, max
or mean
.
See also remove_self_loops
, has_multi_edges
, and to_bidirected
.
sourceGNNGraphs.remove_nodes
— Methodremove_nodes(g::GNNGraph, p)
Returns a new graph obtained by dropping nodes from g
with independent probabilities p
.
Examples
julia> g = GNNGraph([1, 1, 2, 2, 3, 4], [1, 2, 3, 1, 3, 1])
+ num_edges: 2
sourceGNNGraphs.remove_multi_edges
— Methodremove_multi_edges(g::GNNGraph; aggr=+)
Remove multiple edges (also called parallel edges or repeated edges) from graph g
. Possible edge features are aggregated according to aggr
, that can take value +
,min
, max
or mean
.
See also remove_self_loops
, has_multi_edges
, and to_bidirected
.
sourceGNNGraphs.remove_nodes
— Methodremove_nodes(g::GNNGraph, p)
Returns a new graph obtained by dropping nodes from g
with independent probabilities p
.
Examples
julia> g = GNNGraph([1, 1, 2, 2, 3, 4], [1, 2, 3, 1, 3, 1])
GNNGraph:
num_nodes: 4
num_edges: 6
@@ -145,7 +145,7 @@
julia> g_new = remove_nodes(g, 0.5)
GNNGraph:
num_nodes: 2
- num_edges: 2
sourceGNNGraphs.remove_nodes
— Methodremove_nodes(g::GNNGraph, nodes_to_remove::AbstractVector)
Remove specified nodes, and their associated edges, from a GNNGraph. This operation reindexes the remaining nodes to maintain a continuous sequence of node indices, starting from 1. Similarly, edges are reindexed to account for the removal of edges connected to the removed nodes.
Arguments
g
: The input graph from which nodes (and their edges) will be removed.nodes_to_remove
: Vector of node indices to be removed.
Returns
A new GNNGraph with the specified nodes and all edges associated with these nodes removed.
Example
using GraphNeuralNetworks
+ num_edges: 2
sourceGNNGraphs.remove_nodes
— Methodremove_nodes(g::GNNGraph, nodes_to_remove::AbstractVector)
Remove specified nodes, and their associated edges, from a GNNGraph. This operation reindexes the remaining nodes to maintain a continuous sequence of node indices, starting from 1. Similarly, edges are reindexed to account for the removal of edges connected to the removed nodes.
Arguments
g
: The input graph from which nodes (and their edges) will be removed.nodes_to_remove
: Vector of node indices to be removed.
Returns
A new GNNGraph with the specified nodes and all edges associated with these nodes removed.
Example
using GraphNeuralNetworks
g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1])
@@ -153,7 +153,7 @@
g_new = remove_nodes(g, [2, 3])
# g_new now does not contain nodes 2 and 3, and any edges that were connected to these nodes.
-println(g_new)
sourceGNNGraphs.remove_self_loops
— Methodremove_self_loops(g::GNNGraph)
Return a graph constructed from g
where self-loops (edges from a node to itself) are removed.
See also add_self_loops
and remove_multi_edges
.
sourceGNNGraphs.set_edge_weight
— Methodset_edge_weight(g::GNNGraph, w::AbstractVector)
Set w
as edge weights in the returned graph.
sourceGNNGraphs.to_bidirected
— Methodto_bidirected(g)
Adds a reverse edge for each edge in the graph, then calls remove_multi_edges
with mean
aggregation to simplify the graph.
See also is_bidirected
.
Examples
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
+println(g_new)
sourceGNNGraphs.remove_self_loops
— Methodremove_self_loops(g::GNNGraph)
Return a graph constructed from g
where self-loops (edges from a node to itself) are removed.
See also add_self_loops
and remove_multi_edges
.
sourceGNNGraphs.set_edge_weight
— Methodset_edge_weight(g::GNNGraph, w::AbstractVector)
Set w
as edge weights in the returned graph.
sourceGNNGraphs.to_bidirected
— Methodto_bidirected(g)
Adds a reverse edge for each edge in the graph, then calls remove_multi_edges
with mean
aggregation to simplify the graph.
See also is_bidirected
.
Examples
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
julia> w = [1.0, 2.0, 3.0, 4.0, 5.0];
@@ -194,7 +194,7 @@
20.0
35.0
35.0
- 50.0
sourceGNNGraphs.to_unidirected
— Methodto_unidirected(g::GNNGraph)
Return a graph that for each multiple edge between two nodes in g
keeps only an edge in one direction.
sourceMLUtils.batch
— Methodbatch(gs::Vector{<:GNNGraph})
Batch together multiple GNNGraph
s into a single one containing the total number of original nodes and edges.
Equivalent to SparseArrays.blockdiag
. See also MLUtils.unbatch
.
Examples
julia> g1 = rand_graph(4, 6, ndata=ones(8, 4))
+ 50.0
sourceGNNGraphs.to_unidirected
— Methodto_unidirected(g::GNNGraph)
Return a graph that for each multiple edge between two nodes in g
keeps only an edge in one direction.
sourceMLUtils.batch
— Methodbatch(gs::Vector{<:GNNGraph})
Batch together multiple GNNGraph
s into a single one containing the total number of original nodes and edges.
Equivalent to SparseArrays.blockdiag
. See also MLUtils.unbatch
.
Examples
julia> g1 = rand_graph(4, 6, ndata=ones(8, 4))
GNNGraph:
num_nodes = 4
num_edges = 6
@@ -225,7 +225,7 @@
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
- 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
sourceMLUtils.unbatch
— Methodunbatch(g::GNNGraph)
Opposite of the MLUtils.batch
operation, returns an array of the individual graphs batched together in g
.
See also MLUtils.batch
and getgraph
.
Examples
julia> gbatched = MLUtils.batch([rand_graph(5, 6), rand_graph(10, 8), rand_graph(4,2)])
+ 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
sourceMLUtils.unbatch
— Methodunbatch(g::GNNGraph)
Opposite of the MLUtils.batch
operation, returns an array of the individual graphs batched together in g
.
See also MLUtils.batch
and getgraph
.
Examples
julia> gbatched = MLUtils.batch([rand_graph(5, 6), rand_graph(10, 8), rand_graph(4,2)])
GNNGraph:
num_nodes = 19
num_edges = 16
@@ -243,8 +243,8 @@
GNNGraph:
num_nodes = 4
- num_edges = 2
sourceSparseArrays.blockdiag
— Methodblockdiag(xs::GNNGraph...)
Equivalent to MLUtils.batch
.
sourceUtils
GNNGraphs.sort_edge_index
— Functionsort_edge_index(ei::Tuple) -> u', v'
-sort_edge_index(u, v) -> u', v'
Return a sorted version of the tuple of vectors ei = (u, v)
, applying a common permutation to u
and v
. The sorting is lexycographic, that is the pairs (ui, vi)
are sorted first according to the ui
and then according to vi
.
sourceGNNGraphs.color_refinement
— Functioncolor_refinement(g::GNNGraph, [x0]) -> x, num_colors, niters
The color refinement algorithm for graph coloring. Given a graph g
and an initial coloring x0
, the algorithm iteratively refines the coloring until a fixed point is reached.
At each iteration the algorithm computes a hash of the coloring and the sorted list of colors of the neighbors of each node. This hash is used to determine if the coloring has changed.
math x_i' = hashmap((x_i, sort([x_j for j \in N(i)]))).
`
This algorithm is related to the 1-Weisfeiler-Lehman algorithm for graph isomorphism testing.
Arguments
g::GNNGraph
: The graph to color.x0::AbstractVector{<:Integer}
: The initial coloring. If not provided, all nodes are colored with 1.
Returns
x::AbstractVector{<:Integer}
: The final coloring.num_colors::Int
: The number of colors used.niters::Int
: The number of iterations until convergence.
sourceGenerate
GNNGraphs.knn_graph
— Methodknn_graph(points::AbstractMatrix,
+ num_edges = 2
sourceSparseArrays.blockdiag
— Methodblockdiag(xs::GNNGraph...)
Equivalent to MLUtils.batch
.
sourceUtils
GNNGraphs.sort_edge_index
— Functionsort_edge_index(ei::Tuple) -> u', v'
+sort_edge_index(u, v) -> u', v'
Return a sorted version of the tuple of vectors ei = (u, v)
, applying a common permutation to u
and v
. The sorting is lexycographic, that is the pairs (ui, vi)
are sorted first according to the ui
and then according to vi
.
sourceGNNGraphs.color_refinement
— Functioncolor_refinement(g::GNNGraph, [x0]) -> x, num_colors, niters
The color refinement algorithm for graph coloring. Given a graph g
and an initial coloring x0
, the algorithm iteratively refines the coloring until a fixed point is reached.
At each iteration the algorithm computes a hash of the coloring and the sorted list of colors of the neighbors of each node. This hash is used to determine if the coloring has changed.
math x_i' = hashmap((x_i, sort([x_j for j \in N(i)]))).
`
This algorithm is related to the 1-Weisfeiler-Lehman algorithm for graph isomorphism testing.
Arguments
g::GNNGraph
: The graph to color.x0::AbstractVector{<:Integer}
: The initial coloring. If not provided, all nodes are colored with 1.
Returns
x::AbstractVector{<:Integer}
: The final coloring.num_colors::Int
: The number of colors used.niters::Int
: The number of iterations until convergence.
sourceGenerate
GNNGraphs.knn_graph
— Methodknn_graph(points::AbstractMatrix,
k::Int;
graph_indicator = nothing,
self_loops = false,
@@ -265,7 +265,7 @@
num_nodes = 10
num_edges = 30
num_graphs = 2
-
sourceGNNGraphs.radius_graph
— Methodradius_graph(points::AbstractMatrix,
+
sourceGNNGraphs.radius_graph
— Methodradius_graph(points::AbstractMatrix,
r::AbstractFloat;
graph_indicator = nothing,
self_loops = false,
@@ -286,7 +286,7 @@
num_nodes = 10
num_edges = 20
num_graphs = 2
-
References
Section B paragraphs 1 and 2 of the paper Dynamic Hidden-Variable Network Models
sourceGNNGraphs.rand_bipartite_heterograph
— Methodrand_bipartite_heterograph([rng,]
+
References
Section B paragraphs 1 and 2 of the paper Dynamic Hidden-Variable Network Models
sourceGNNGraphs.rand_bipartite_heterograph
— Methodrand_bipartite_heterograph([rng,]
(n1, n2), (m12, m21);
bidirected = true,
node_t = (:A, :B),
@@ -299,7 +299,7 @@
julia> g = rand_bipartite_heterograph((10, 15), (20, 0), node_t=(:user, :item), edge_t=:-, bidirected=false)
GNNHeteroGraph:
num_nodes: Dict(:item => 15, :user => 10)
- num_edges: Dict((:item, :-, :user) => 0, (:user, :-, :item) => 20)
sourceGNNGraphs.rand_graph
— Methodrand_graph([rng,] n, m; bidirected=true, edge_weight = nothing, kws...)
Generate a random (Erdós-Renyi) GNNGraph
with n
nodes and m
edges.
If bidirected=true
the reverse edge of each edge will be present. If bidirected=false
instead, m
unrelated edges are generated. In any case, the output graph will contain no self-loops or multi-edges.
A vector can be passed as edge_weight
. Its length has to be equal to m
in the directed case, and m÷2
in the bidirected one.
Pass a random number generator as the first argument to make the generation reproducible.
Additional keyword arguments will be passed to the GNNGraph
constructor.
Examples
julia> g = rand_graph(5, 4, bidirected=false)
+ num_edges: Dict((:item, :-, :user) => 0, (:user, :-, :item) => 20)
sourceGNNGraphs.rand_graph
— Methodrand_graph([rng,] n, m; bidirected=true, edge_weight = nothing, kws...)
Generate a random (Erdós-Renyi) GNNGraph
with n
nodes and m
edges.
If bidirected=true
the reverse edge of each edge will be present. If bidirected=false
instead, m
unrelated edges are generated. In any case, the output graph will contain no self-loops or multi-edges.
A vector can be passed as edge_weight
. Its length has to be equal to m
in the directed case, and m÷2
in the bidirected one.
Pass a random number generator as the first argument to make the generation reproducible.
Additional keyword arguments will be passed to the GNNGraph
constructor.
Examples
julia> g = rand_graph(5, 4, bidirected=false)
GNNGraph:
num_nodes = 5
num_edges = 4
@@ -317,11 +317,11 @@
# Each edge has a reverse
julia> edge_index(g)
-([1, 3, 3, 4], [3, 4, 1, 3])
sourceGNNGraphs.rand_heterograph
— Functionrand_heterograph([rng,] n, m; bidirected=false, kws...)
Construct an GNNHeteroGraph
with random edges and with number of nodes and edges specified by n
and m
respectively. n
and m
can be any iterable of pairs specifing node/edge types and their numbers.
Pass a random number generator as a first argument to make the generation reproducible.
Setting bidirected=true
will generate a bidirected graph, i.e. each edge will have a reverse edge. Therefore, for each edge type (:A, :rel, :B)
a corresponding reverse edge type (:B, :rel, :A)
will be generated.
Additional keyword arguments will be passed to the GNNHeteroGraph
constructor.
Examples
julia> g = rand_heterograph((:user => 10, :movie => 20),
+([1, 3, 3, 4], [3, 4, 1, 3])
sourceGNNGraphs.rand_heterograph
— Functionrand_heterograph([rng,] n, m; bidirected=false, kws...)
Construct an GNNHeteroGraph
with random edges and with number of nodes and edges specified by n
and m
respectively. n
and m
can be any iterable of pairs specifing node/edge types and their numbers.
Pass a random number generator as a first argument to make the generation reproducible.
Setting bidirected=true
will generate a bidirected graph, i.e. each edge will have a reverse edge. Therefore, for each edge type (:A, :rel, :B)
a corresponding reverse edge type (:B, :rel, :A)
will be generated.
Additional keyword arguments will be passed to the GNNHeteroGraph
constructor.
Examples
julia> g = rand_heterograph((:user => 10, :movie => 20),
(:user, :rate, :movie) => 30)
GNNHeteroGraph:
num_nodes: (:user => 10, :movie => 20)
- num_edges: ((:user, :rate, :movie) => 30,)
sourceOperators
Base.intersect
— Function" intersect(g1::GNNGraph, g2::GNNGraph)
Intersect two graphs by keeping only the common edges.
sourceSampling
GNNGraphs.sample_neighbors
— Functionsample_neighbors(g, nodes, K=-1; dir=:in, replace=false, dropnodes=false)
Sample neighboring edges of the given nodes and return the induced subgraph. For each node, a number of inbound (or outbound when dir = :out
) edges will be randomly chosen. If
dropnodes=false`, the graph returned will then contain all the nodes in the original graph, but only the sampled edges.
The returned graph will contain an edge feature EID
corresponding to the id of the edge in the original graph. If dropnodes=true
, it will also contain a node feature NID
with the node ids in the original graph.
Arguments
g
. The graph.nodes
. A list of node IDs to sample neighbors from.K
. The maximum number of edges to be sampled for each node. If -1, all the neighboring edges will be selected.dir
. Determines whether to sample inbound (:in
) or outbound (`:out
) edges (Default :in
).replace
. If true
, sample with replacement.dropnodes
. If true
, the resulting subgraph will contain only the nodes involved in the sampled edges.
Examples
julia> g = rand_graph(20, 100)
+ num_edges: ((:user, :rate, :movie) => 30,)
sourceOperators
Base.intersect
— Function" intersect(g1::GNNGraph, g2::GNNGraph)
Intersect two graphs by keeping only the common edges.
sourceSampling
GNNGraphs.sample_neighbors
— Functionsample_neighbors(g, nodes, K=-1; dir=:in, replace=false, dropnodes=false)
Sample neighboring edges of the given nodes and return the induced subgraph. For each node, a number of inbound (or outbound when dir = :out
) edges will be randomly chosen. If
dropnodes=false`, the graph returned will then contain all the nodes in the original graph, but only the sampled edges.
The returned graph will contain an edge feature EID
corresponding to the id of the edge in the original graph. If dropnodes=true
, it will also contain a node feature NID
with the node ids in the original graph.
Arguments
g
. The graph.nodes
. A list of node IDs to sample neighbors from.K
. The maximum number of edges to be sampled for each node. If -1, all the neighboring edges will be selected.dir
. Determines whether to sample inbound (:in
) or outbound (`:out
) edges (Default :in
).replace
. If true
, sample with replacement.dropnodes
. If true
, the resulting subgraph will contain only the nodes involved in the sampled edges.
Examples
julia> g = rand_graph(20, 100)
GNNGraph:
num_nodes = 20
num_edges = 100
@@ -360,7 +360,7 @@
num_nodes = 20
num_edges = 10
edata:
- EID => (10,)
sourceGraphs.induced_subgraph
— Methodinduced_subgraph(graph, nodes)
Generates a subgraph from the original graph using the provided nodes
. The function includes the nodes' neighbors and creates edges between nodes that are connected in the original graph. If a node has no neighbors, an isolated node will be added to the subgraph. Returns A new GNNGraph
containing the subgraph with the specified nodes and their features.
Arguments
graph
. The original GNNGraph containing nodes, edges, and node features.nodes
`. A vector of node indices to include in the subgraph.
Examples
julia> s = [1, 2]
+ EID => (10,)
sourceGraphs.induced_subgraph
— Methodinduced_subgraph(graph, nodes)
Generates a subgraph from the original graph using the provided nodes
. The function includes the nodes' neighbors and creates edges between nodes that are connected in the original graph. If a node has no neighbors, an isolated node will be added to the subgraph. Returns A new GNNGraph
containing the subgraph with the specified nodes and their features.
Arguments
graph
. The original GNNGraph containing nodes, edges, and node features.nodes
`. A vector of node indices to include in the subgraph.
Examples
julia> s = [1, 2]
2-element Vector{Int64}:
1
2
@@ -393,4 +393,4 @@
y = 2-element Vector{Float32}
x = 32×2 Matrix{Float32}
edata:
- e = 1-element Vector{Float32}
sourceSettings
This document was generated with Documenter.jl version 1.7.0 on Saturday 9 November 2024. Using Julia version 1.10.5.
\ No newline at end of file
+ e = 1-element Vector{Float32}