bind_variables

Contents

bind_variables#

penzai.core.variables.bind_variables(tree: Any, variables: Iterable[AbstractVariable | AbstractVariableValue], allow_unused: bool = False, unfreeze_as_copy: bool = False) Any[source]#

Binds variables (mutable or frozen) into the variable slots in a pytree.

This function re-inserts variable instances into a tree in place of their matching variable slots. It is the inverse of unbind_variables.

Parameters:
  • tree – The tree to substitute variables into.

  • variables – The collection of variables to insert.

  • allow_unused – Whether to ignore variables that do not have any matching slot (in which case they will not be inserted).

  • unfreeze_as_copy – Whether to unfreeze variable values before inserting them, producing a new mutable copy of each input variable. If True, all input variables must be instances of AbstractVariableValue.

Returns:

A copy of tree with variables re-inserted.