Graph data structures
Data structures for representing graphs and graph batches.
This module provides object-based graph classes (Node, Edge, Graph) and a flat batched container (GraphTuple) used by high-level GraphNet APIs.
- class tf_gnns.tfgnns_datastructures.Node(node_attr_tensor)[source]
Bases:
objectGraph node with a feature tensor.
- Parameters:
node_attr_tensor – Tensor-like node attributes with at least rank 2. The first dimension is usually batch-like in object graph mode.
- class tf_gnns.tfgnns_datastructures.Edge(edge_attr_tensor, node_from, node_to)[source]
Bases:
objectDirected edge connecting two
Nodeobjects.- Parameters:
edge_attr_tensor – Tensor-like edge attributes.
node_from – Source node.
node_to – Destination node.
- class tf_gnns.tfgnns_datastructures.Graph(nodes, edges, global_attr=None, NO_VALIDATION=True)[source]
Bases:
objectObject graph made of node and edge instances.
- Parameters:
- is_equal_by_value(g2)[source]
Checks if the graphs have the same values for node and edge attributes
- get_subgraph_from_nodes(nodes, edge_trimming_mode='+from+to')[source]
Create a subgraph by filtering nodes and incident edges.
- Parameters:
nodes – Node subset to keep.
edge_trimming_mode – Edge filter mode. Supported values are
"+from+to"(keep edges where both endpoints are innodes) and"-from+to"(keep edges where both endpoints are not innodes).
- Returns:
A new
Graphwith copied nodes and matching copied edges.
- tf_gnns.tfgnns_datastructures.make_graph_tuple_from_graph_list(list_of_graphs)[source]
Create a
GraphTuplefrom a list of object graphs.- Parameters:
list_of_graphs – List of
Graphobjects with consistent feature dimensionality.- Returns:
A
GraphTuplewith flattened node/edge tensors and bookkeeping vectors (senders, receivers, n_nodes, n_edges).
Notes
This helper currently expects node and edge attributes in each input graph to have first dimension equal to
1.
- class tf_gnns.tfgnns_datastructures.GraphTuple(nodes, edges, senders, receivers, n_nodes, n_edges, global_attr=None, global_reps_for_nodes=None, global_reps_for_edges=None, n_graphs=None)[source]
Bases:
objectBatched graph representation used by GraphNet tensor-dict paths.
A
GraphTuplestores all node and edge features in contiguous tensors and keeps graph boundaries via n_nodes and n_edges vectors.The
GraphTuplemakes multiple smaller graphs appear as a single large graph, with contiguous indexing for nodes and edges. This allows fast batched computation and takes advantage of default performance optimizations in deep learning frameworks.- __init__(nodes, edges, senders, receivers, n_nodes, n_edges, global_attr=None, global_reps_for_nodes=None, global_reps_for_edges=None, n_graphs=None)[source]
Initialize a graph batch.
- Parameters:
nodes – Tensor-like node feature array with shape
[sum(n_nodes), d_n].edges – Tensor-like edge feature array with shape
[sum(n_edges), d_e].senders – Sender node indices for each edge. Indices are unique across graphs in the flattened representation.
receivers – Receiver node indices for each edge. Indices are unique across graphs in the flattened representation.
n_nodes – Per-graph node counts.
n_edges – Per-graph edge counts.
global_attr – Optional graph-level features of shape
[n_graphs, d_g].global_reps_for_nodes – Optional precomputed mapping from node rows to graph ids.
global_reps_for_edges – Optional precomputed mapping from edge rows to graph ids.
n_graphs – Optional number of graphs.
- assign_global(global_attr, check_shape=False)[source]
Assign graph-level features.
- Parameters:
global_attr – Tensor-like global features.
check_shape – If
True, assert first dimension equalsn_graphs.