Skip to content

graph_algorithms

pytorch_symbolic.graph_algorithms

check_for_missing_inputs

check_for_missing_inputs(used_nodes: Set[SymbolicData], inputs: Tuple[SymbolicData, ...] | List[SymbolicData])

Check if there exist nodes which require input from outside of the graph.

It is forbidden, as it doesn't make sense.

Example of such violation::

x1 = Input(shape=(32,))
x2 = Input(shape=(32,))
x3 = x1 + x2
model = SymbolicModel(inputs=x1, outputs=x3)

Model cannot execute defined operations unless x2 is given, because x3 requires it.

figure_out_nodes_between

figure_out_nodes_between(inputs: Tuple[SymbolicData, ...] | List[SymbolicData] | None = None, outputs: Tuple[SymbolicData, ...] | List[SymbolicData] | None = None) -> Set[SymbolicData]

Returns intersection of predecessors tree of outputs and succesors tree of inputs.

draw_graph

draw_graph(*, model: SymbolicModel | None = None, inputs: Iterable[SymbolicData] | SymbolicData | None = None, outputs: Iterable[SymbolicData] | SymbolicData | None = None, node_text_func: Callable[[SymbolicData], str] | None = None, edge_text_func: Callable[[nn.Module], str] | None = None, node_text_namespace: Dict[str, Any] | None = None, rotate_graph: bool = False, rotate_labels: bool = False, show: bool = False, figsize = None)

Plot graph of the computations, nodes being placeholder variables and nn.Modules being edges.

This is not suitable for large graphs or large neural networks. This is a simple tool that was designed to demonstrate that Pytorch Symbolic creates sensible graphs that are nice to visualize.

Parameters:

Name Type Description Default
model SymbolicModel | None

A SymbolicModel to be plotted. This or inputs must be provided.

None
inputs Iterable[SymbolicData] | SymbolicData | None

Input in the graph of Symbolic computations. This or model or/and inputs must be provided.

None
outputs Iterable[SymbolicData] | SymbolicData | None

Output in the graph of Symbolic computations. This or model or/and outputs must be provided.

None
node_text_func Callable[[SymbolicData], str] | None

A function that returns text that will be written on Nodes.

None
edge_text_func Callable[[nn.Module], str] | None

A function that returns text that will be written on Edges.

None
node_text_namespace Dict[str, Any] | None

If used with globals(), it will try to show variable name on each node. Has an effect only if node_text_func is None.

None
rotate_labels bool

If True, text on edges will be rotated in the direction of the arrow.

False
rotate_graph bool

If True, the consecutive layers will be shown to the right, instead of downwards.

False
show bool

Call matplotlib.pyplot.show

False

sort_graph_and_check_DAG

sort_graph_and_check_DAG(nodes: Set[SymbolicData]) -> List[SymbolicData]

Sort graph topologically.

Wikipedia: In graph theory, a topological sort or topological ordering of a directed acyclic graph (DAG) is a linear ordering of its nodes in which each node comes before all nodes to which it has outbound edges. Every DAG has one or more topological sorts.