Код: Выделить всё
from functools import partial
import jax
# Function to apply pytree of functions to leaves
@partial(jax.jit, static_argnames=("func_pytree",))
def apply_func_pytree_to_leaves(func_pytree, data_pytree):
funcs, _ = jax.tree.flatten(func_pytree)
vals, def_vals = jax.tree.flatten(data_pytree)
out = [f(x) for f, x in zip(funcs, vals)]
return jax.tree.unflatten(def_vals, out)
# Some functions
def f0(x):
return x
def f10(x):
return x**2
def f11(x):
return x**3
# Apply functions to leaves
func_pytree = (f0, (f10, f11))
data_pytree = (5, (5, 5))
out = apply_func_pytree_to_leaves(func_pytree, data_pytree)
Может быть решение с использованием равноденствия, я рассмотрю это.
Подробнее здесь: https://stackoverflow.com/questions/798 ... ree-in-jax
Мобильная версия