Module graph.utils
Utilities for generating and manipulating computational graphs
See also
See Computational graphs in Rockpool for an introduction to computational graphs.
Functions overview
|
Convert a graph into a collection of connection nodes and modules, by traversal |
|
Connect two |
|
Search a graph for all |
|
Search for graph modules that are connected in a one-module loop |
|
Replace a graph module with a different module |
Functions
- graph.utils.bag_graph(graph: GraphModuleBase, nodes_bag: SetList[GraphNode] | None = None, modules_bag: SetList[GraphModule] | None = None) Tuple[SetList[GraphNode], SetList[GraphModule]][source]
Convert a graph into a collection of connection nodes and modules, by traversal
A graph will be traversed, following all connections. The connection
GraphNodes andGraphModules will be collected and returned in two collections. AnyGraphHoldermodules will be ignored and discarded.- Parameters:
graph (GraphModuleBase) – A graph to analyse
nodes_bag (SetList) – The initial nodes bag. Used in recursive calls. Default:
Nonemodules_bag (SetList) – The initial modules bag. Used in recursive calls. Default:
None
- Returns:
nodes, modules.
nodeswill be aSetListcontaining all the reachableGraphNodes ingraph.moduleswill be aSetListcontaining all the reachableGraphModules ingraph.- Return type:
Tuple[SetList[GraphNode]]
- graph.utils.connect_modules(source: GraphModuleBase, dest: GraphModuleBase, source_indices: Iterable[int] | None = None, dest_indices: Iterable[int] | None = None) None[source]
Connect two
GraphModules togetherConnecting two graph modules can only occur if the output and input dimensionality match across the connection. The output
GraphNodes from the source module will be merged with the inputGraphNodesof the destination module. TheGraphNodes of the destination module will then be discarded.If
sourceordestareGraphHolders, then the internal subgraphs will be connected, and theGraphHolders may be discarded.Examples
>>> connect_modules(mod1, mod2) # Modules are connected in place, from all output node to all input nodes
>>> connect_modules(mod1, mod2, range(5)) # Connect a subset of source output nodes to the destination module # Output nodes `mod1.output_nodes[0:5]` are connected to all input nodes `mod2.input_nodes[:]`
>>> connect_modules(mod1, mod2, None, range(3)) # All output nodes `mod1.output_nodes[:]` are connected to input nodes `mod2.input_nodes[0:3]`
>>> connect_modules(mod1, mod2, [0, 2, 4], [1, 2, 5]) # `mod1` output nodes 0, 2 and 4 are connected to `mod2` input nodes 1, 2, 5
- Parameters:
source (GraphModule) – The source graph module to connect
dest (GraphModule) – The destination graph module to connect
source_indices (Optional[Iterable[int]]) – The indices of the
sourceoutput nodes to connect over. Default:None, use all output nodesdest_indices (Optional[Iterable[int]]) – The indices of
destinput nodes to connect over. Default:None, use all input nodes
- graph.utils.find_modules_of_subclass(graph: GraphModuleBase, cls) SetList[Any][source]
Search a graph for all
GraphModules of a specific class or any subclassThe search uses
isinstanceto search forcls, so any subclass ofclswill also be found.- Parameters:
graph (GraphModuleBase) –
cls – A class to search for instances of, or instances of any subclass
- Returns:
A collection of objects of the desired class
- Return type:
SetList[Any]
- graph.utils.find_recurrent_modules(graph: GraphModuleBase) Tuple[SetList[GraphModule]][source]
Search for graph modules that are connected in a one-module loop
A “recurrent module” is defined as a graph module that connects with itself via another single graph module. e.g. a module of neurons, connected to a module of weights that itself connects recurrently back from output of the neurons to the input of the neurons.
- Parameters:
graph (GraphModuleBase) – A graph to search
- Returns:
A collection containing all identified recurrent modules in the graph Tuple[SetList[GraphNode]] : modules, recurrent_modules.
moduleswill be aSetListcontaining all the reachableGraphModules ingraph.recurrent_modulesis a collection containing all identified recurrent modules in the graph- Return type:
SetList[GraphModule]
- graph.utils.replace_module(target_module: GraphModule, replacement_module: GraphModule) None[source]
Replace a graph module with a different module
This function removes a target graph module from a graph, and replaces it with a replacement module. It removes the target module from any connection
GraphNodes, and wires in the replacement module instead.- Parameters:
target_module (GraphModule) – A module inside a graph to replace
replacement_module (GraphModule) – A replacement module to wire into the graph, in place of
target_module