replace – High level graph transformations#

pytensor.graph.replace.clone_replace(output: Sequence[Variable], replace: collections.abc.Iterable[tuple[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable]] | dict[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable] | None = None, **rebuild_kwds) list[pytensor.graph.basic.Variable][source]#
pytensor.graph.replace.clone_replace(output: Variable, replace: collections.abc.Iterable[tuple[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable]] | dict[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable] | None = None, **rebuild_kwds) Variable

Clone a graph and replace subgraphs within it.

It returns a copy of the initial subgraph with the corresponding substitutions.

Parameters:
  • output – PyTensor expression that represents the computational graph.

  • replace – Dictionary describing which subgraphs should be replaced by what.

  • rebuild_kwds – Keywords to rebuild_collect_shared.

pytensor.graph.replace.graph_replace(outputs: Variable, replace: collections.abc.Iterable[tuple[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable]] | dict[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable] | None = None, *, strict=True) Variable[source]#
pytensor.graph.replace.graph_replace(outputs: Sequence[Variable], replace: collections.abc.Iterable[tuple[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable]] | dict[pytensor.graph.basic.Variable, pytensor.graph.basic.Variable] | None = None, *, strict=True) list[pytensor.graph.basic.Variable]

Replace variables in outputs by replace.

Parameters:
  • outputs (Union[Sequence[Variable], Variable]) – Output graph

  • replace (Dict[Variable, Variable]) – Replace mapping

  • strict (bool) – Raise an error if some replacements were not used

  • return_unused (bool) – Return replacements that were not used

Returns:

Output graph with subgraphs replaced, see function overload for the exact type

Return type:

Union[Variable, List[Variable]]

Raises:

ValueError – If some replacements could not be applied and strict is True

pytensor.graph.replace.vectorize_graph(outputs: Variable, replace: Mapping[Variable, Variable]) Variable[source]#
pytensor.graph.replace.vectorize_graph(outputs: Sequence[Variable], replace: Mapping[Variable, Variable]) Sequence[Variable]

Vectorize outputs graph given mapping from old variables to expanded counterparts version.

Expanded dimensions must be on the left. Behavior is similar to the functional numpy.vectorize.

Examples

import pytensor
import pytensor.tensor as pt

from pytensor.graph import vectorize_graph

# Original graph
x = pt.vector("x")
y = pt.exp(x) / pt.sum(pt.exp(x))

# Vectorized graph
new_x = pt.matrix("new_x")
new_y = vectorize_graph(y, replace={x: new_x})

fn = pytensor.function([new_x], new_y)
fn([[0, 1, 2], [2, 1, 0]])
# array([[0.09003057, 0.24472847, 0.66524096],
#        [0.66524096, 0.24472847, 0.09003057]])
import pytensor
import pytensor.tensor as pt

from pytensor.graph import vectorize_graph

# Original graph
x = pt.vector("x")
y1 = x[0]
y2 = x[-1]

# Vectorized graph
new_x = pt.matrix("new_x")
[new_y1, new_y2] = vectorize_graph([y1, y2], replace={x: new_x})

fn = pytensor.function([new_x], [new_y1, new_y2])
fn([[-10, 0, 10], [-11, 0, 11]])
# [array([-10., -11.]), array([10., 11.])]
pytensor.graph.replace.vectorize_node(node: Apply, *batched_inputs) Apply[source]#

Returns vectorized version of node with new batched inputs.