furax.tree
Functions:
-
as_promoted_dtype–Promotes the data type of the leaves of a pytree to a common data type.
-
as_structure–Returns the pytree of ShapedDtypeStruct leaves associated with x.
-
nbytes–Returns the total byte size of a pytree.
-
zeros_like–Returns a pytrees of zeros with the same structure as x.
-
ones_like–Returns a pytrees of ones with the same structure as x.
-
full_like–Returns a pytrees of a specified value with the same structure as x.
-
normal_like–Returns a pytrees of a normal values with the same structure as x.
-
is_leaf–Returns true if the input is a Pytree leaf.
-
add– -
sub– -
mul– -
truediv– -
power– -
dot–Scalar product of two Pytrees.
-
norm–Compute the norm of a PyTree.
-
matvec–Generalized matrix-vector operation, where the matrix and the vector are pytrees.
-
vecmat–Generalized vector-matrix operation, where the matrix and the vector are pytrees.
-
matmat–Generalized matrix-matrix operation, where the matrices are pytrees.
furax.tree.as_promoted_dtype(x)
Promotes the data type of the leaves of a pytree to a common data type.
Parameters:
-
x(P) –The pytree to promote.
Examples:
>>> as_promoted_dtype({'a': jnp.ones(2, jnp.float16), 'b': jnp.ones(2, jnp.float32)})
{'a': Array([1., 1.], dtype=float32), 'b': Array([1., 1.], dtype=float32)}
Source code in src/furax/tree.py
furax.tree.as_structure(x)
Returns the pytree of ShapedDtypeStruct leaves associated with x.
Parameters:
-
x(P) –The pytree whose structure will be returned.
Examples:
>>> as_structure({'a': [jnp.zeros((2, 3)), jnp.array(2)]})
{'a': [ShapeDtypeStruct(shape=(2, 3), dtype=float32),
ShapeDtypeStruct(shape=(), dtype=int32, weak_type=True)]}
Source code in src/furax/tree.py
furax.tree.nbytes(x)
Returns the total byte size of a pytree.
Parameters:
-
x(P) –The pytree of array-like leaves (or
ShapeDtypeStructleaves), whose total byte size will be computed.
Examples:
Source code in src/furax/tree.py
furax.tree.zeros_like(x)
Returns a pytrees of zeros with the same structure as x.
Parameters:
-
x(P) –The pytree of array-like leaves with
shapeanddtypeattributes, whose structure will be used to construct the output pytree of zeros.
Examples:
Source code in src/furax/tree.py
furax.tree.ones_like(x)
Returns a pytrees of ones with the same structure as x.
Parameters:
-
x(P) –The pytree of array-like leaves with
shapeanddtypeattributes, whose structure will be used to construct the output pytree of ones.
Examples:
Source code in src/furax/tree.py
furax.tree.full_like(x, fill_value)
Returns a pytrees of a specified value with the same structure as x.
Parameters:
-
x(P) –The pytree of array-like leaves with
shapeanddtypeattributes, whose structure will be used to construct the output pytree of the specified value.. -
fill_value(ScalarLike) –The value to fill with.
Examples:
>>> full_like({'a': jnp.array(1, jnp.int32), 'b': jnp.array(2, jnp.float32)}, 3)
{'a': Array(3, dtype=int32), 'b': Array(3., dtype=float32)}
>>> full_like({'a': jax.ShapeDtypeStruct((2,), jnp.int32),
... 'b': jax.ShapeDtypeStruct((), jnp.float32)}, 3)
{'a': Array([3, 3], dtype=int32), 'b': Array(3., dtype=float32)}
Source code in src/furax/tree.py
furax.tree.normal_like(x, key)
Returns a pytrees of a normal values with the same structure as x.
Parameters:
-
x(P) –The pytree of array-like leaves with
shapeanddtypeattributes, whose structure will be used to construct the output pytree of pseudo-random values. -
key(Key[Array, '']) –The PRNGKey to use.
Examples:
>>> normal_like({'a': jnp.array(1, jnp.float16),
... 'b': jnp.array(2, jnp.float32)}, jax.random.PRNGKey(0))
{'a': Array(-1.34, dtype=float16), 'b': Array(-1.2515389, dtype=float32)}
>>> normal_like({'a': jax.ShapeDtypeStruct((2,), jnp.float16),
... 'b': jax.ShapeDtypeStruct((), jnp.float32)}, jax.random.PRNGKey(0))
{'a': Array([-1.34 , 0.1431], dtype=float16), 'b': Array(-1.2515389, dtype=float32)}
Source code in src/furax/tree.py
furax.tree.is_leaf(x)
furax.tree.add(a, b)
furax.tree.sub(a, b)
furax.tree.mul(a, b)
furax.tree.truediv(a, b)
furax.tree.power(a, b)
furax.tree.dot(x, y)
Scalar product of two Pytrees.
If one of the leaves is complex, the hermitian scalar product is returned.
Parameters:
Examples:
>>> import furax as fx
>>> x = {'a': jnp.array([1., 2, 3]), 'b': jnp.array([1, 0])}
>>> y = {'a': jnp.array([2, -1, 1]), 'b': jnp.array([2, 0])}
>>> fx.tree.dot(x, y)
Array(5., dtype=float32)
Source code in src/furax/tree.py
furax.tree.norm(x)
Compute the norm of a PyTree.
The norm is defined as sqrt(dot(x, x)).
Parameters:
-
x(PyTree[Num[Array, ...]]) –The Pytree.
Examples:
>>> import furax as fx
>>> x = {'a': jnp.array([3., 4.]), 'b': jnp.array([0.])}
>>> fx.tree.norm(x)
Array(5., dtype=float32)
Source code in src/furax/tree.py
furax.tree.matvec(outer_treedef, a, x)
Generalized matrix-vector operation, where the matrix and the vector are pytrees.
The structure of the generalized matrix is the tree product of an outer tree structure representing the rows and an inner tree structure that represents the columns. A tree product of two tree structures is formed by replacing each leaf of the first tree with a copy of the second. The leaves of the generalized matrix must be broadcastable when they belong to same inner tree (elements of the same row). There is no such requirement for leaves of different inner trees (elements of different rows).
Parameters:
-
outer_treedef(PyTreeDef | PyTree[Any]) –The outer structure of the generalized matrix.
-
a(PyTree[Array]) –The generalized matrix, i.e. a pytree whose structure follows the tree product of an outer and inner tree structures, with leaves given by the elements of the generalized matrix.
-
x(PyTree[Array]) –The generalized vector, with a structure matching the inner tree structure of
a.
Returns:
-
PyTree[Array]–A pytree with the same structure as the generalized matrix outer tree structure.
Examples:
To represent with pytrees the sparse tensor [ a11 a12 ] [ a21 a22 ] of shape (2, 2, 100) where a11, a12 and a21 are arrays of 100 elements and a22 is zero:
>>> from numpy.testing import assert_array_equal
>>> a11, a12, a21 = jax.random.normal(jax.random.key(0), (3, 100))
>>> a22 = 0
>>> a = {
... 'row1': {'col1': a11, 'col2': a12},
... 'row2': {'col1': a21, 'col2': a22},
... }
>>> x = {'col1': 1., 'col2': 2.}
>>> y = matvec({'row1': 0, 'row2': 0}, a, x)
>>> assert_array_equal(y['row1'], a11 * x['col1'] + a12 * x['col2'])
>>> assert_array_equal(y['row2'], a21 * x['col1'] + a22 * x['col2'])
Source code in src/furax/tree.py
furax.tree.vecmat(x, outer_treedef, a)
Generalized vector-matrix operation, where the matrix and the vector are pytrees.
The structure of the generalized matrix is the tree product of an outer tree structure representing the rows and an inner tree structure that represents the columns. A tree product of two tree structures is formed by replacing each leaf of the first tree with a copy of the second. The leaves of the generalized matrix must be broadcastable when they belong to same inner tree (elements of the same row). There is no such requirement for leaves of different inner trees (elements of different rows).
Parameters:
-
outer_treedef(PyTreeDef | PyTree[Any]) –The outer structure of the generalized matrix.
-
a(PyTree[Array]) –The generalized matrix, i.e. a pytree whose structure follows the tree product of an outer and inner tree structures, with leaves given by the elements of the generalized matrix.
-
x(PyTree[Array]) –The generalized vector, with a structure matching the outer tree structure of
a.
Returns:
-
PyTree[Array]–A pytree with the same structure as the generalized matrix inner tree structure.
Examples:
To represent with pytrees the sparse tensor [ a11 a12 ] [ a21 a22 ] of shape (2, 2, 100) where a11, a12 and a21 are arrays of 100 elements and a22 is zero:
>>> from numpy.testing import assert_array_equal
>>> a11, a12, a21 = jax.random.normal(jax.random.key(0), (3, 100))
>>> a22 = 0
>>> a = {
... 'row1': {'col1': a11, 'col2': a12},
... 'row2': {'col1': a21, 'col2': a22},
... }
>>> x = {'row1': 1., 'row2': 2.}
>>> y = vecmat(x, {'row1': 0, 'row2': 0}, a)
>>> assert_array_equal(y['col1'], a11 * x['row1'] + a21 * x['row2'])
>>> assert_array_equal(y['col2'], a12 * x['row1'] + a22 * x['row2'])
Source code in src/furax/tree.py
furax.tree.matmat(a_outer_treedef, a, b_outer_treedef, b)
Generalized matrix-matrix operation, where the matrices are pytrees.
The structure of the generalized matrices is the tree product of an outer tree structure representing the rows and an inner tree structure that represents the columns. A tree product of two tree structures is formed by replacing each leaf of the first tree with a copy of the second. The leaves of the generalized matrix must be broadcastable when they belong to same inner tree (elements of the same row). There is no such requirement for leaves of different inner trees (elements of different rows).
Parameters:
-
a_outer_treedef(PyTreeDef | PyTree[Any]) –The outer structure of the first generalized matrix.
-
a(PyTree[Array]) –The first generalized matrix, i.e. a pytree whose structure follows the tree product of an outer and inner tree structures, with leaves given by the elements of the generalized matrix.
-
b_outer_treedef(PyTreeDef | PyTree[Any]) –The outer structure of the second generalized matrix.
-
b(PyTree[Array]) –The second generalized matrix.
Returns:
-
PyTree[Array]–A pytree whose structure is the tree product of the outer structure of
aand the inner -
PyTree[Array]–structure of
b.