Skip to content

scirex.utils

Small helpers.

from scirex.utils import count_model_params

Parameter counting

One complex number counts as two parameters.

count_model_params

count_model_params(params: Any) -> int

Returns the total number of parameters of a JAX/Flax model.

Notes

One complex number is counted as two parameters (real and imaginary parts).

Examples:

>>> import jax.numpy as jnp
>>> count_model_params({"w": jnp.ones((3, 4)), "b": jnp.ones(4)})
16

count_tensor_params

count_tensor_params(
    tensor: ndarray, dims: Sequence[int] | None = None
) -> int

Returns the number of parameters (elements) in a single tensor, optionally, along certain dimensions only

Args:

Name Type Description Default
tensor Array
dims int list or None

if not None, the dimensions to consider when counting the number of parameters (elements)

is None
Notes

One complex number is counted as two parameters (we count real and imaginary parts)'