jax.tree_util.register_pytree_with_keys_class#
- jax.tree_util.register_pytree_with_keys_class(cls)[源代码]#
扩展被视为 pytrees 内部节点的类型集。
此函数类似于
register_pytree_node_class,但需要一个类,该类定义了如何使用键来将其展平。它是
register_pytree_with_keys的一个轻量级封装,并提供面向类的接口。- 参数:
cls (类型) – 要注册为 pytree 的类型。
- 返回:
输入类
cls在被添加到 JAX 的 pytree 注册表中后会原封不动地返回。此返回值允许register_pytree_node_class用作装饰器。- 返回类型:
类型
另请参阅
register_static():注册静态 pytree 的简化 API。register_dataclass():注册 dataclass 的简化 API。
示例
>>> from jax.tree_util import register_pytree_with_keys_class, GetAttrKey >>> @register_pytree_with_keys_class ... class Special: ... def __init__(self, x, y): ... self.x = x ... self.y = y ... def tree_flatten_with_keys(self): ... return (((GetAttrKey('x'), self.x), (GetAttrKey('y'), self.y)), None) ... @classmethod ... def tree_unflatten(cls, aux_data, children): ... return cls(*children)