JaxBernoulliDistribution

class probly.representation.distribution.jax_bernoulli.JaxBernoulliDistribution(*args, **kwargs)[source]

Bases: BernoulliDistribution[Array], JaxCategoricalDistribution, Generic

A Bernoulli distribution represented as a categorical dstribution with 2 classes.

property T: ShapeResultT

Inverts the order of the axes of the underlying array.

abstract property array: Array

Get the underlying array representing the categorical distribution.

astype(dtype: DTypeLike | None, copy: bool = False, device: jax.Device | Sharding | None = None) → Self[source]

Return a copy of the array cast to the given data type.

Parameters:
  • dtype – The target data type.

  • copy – Whether to always return a copy.

  • device – The device the result should live on.

Returns:

A copy with every array-valued field cast to the given data type.

property at: JaxAxisProtectedIndexUpdateHelper[Self | SetValueT, SetResultT, ShapeResultT]

Return an indexed helper whose updates require permitted_at_methods permission.

block_until_ready() → Self[source]

Block until the asynchronous computation of every array-valued field has finished.

Returns:

The object itself.

broadcasted_batch = {}
copy() → OwnerT[source]

Return a copy of the array.

property device: Device

Device of the array.

devices() → set[Device][source]

Return the set of devices the array lives on.

property dtype: dtype

Data type of the array.

entropy() → ArrayLike[source]

Compute entropy.

flatten(order: str = 'C') → ShapeResultT[source]

Return a flattened version of the array.

Parameters:

order – The read/write element order.

Returns:

The flattened array.

item(*args: int) → bool | int | float | complex[source]

Return the array as a Python scalar.

Parameters:

*args – Optional index of the element to return.

Returns:

The selected element as a Python scalar.

property log_probabilities: Array

Get the log probabilities of the categorical distribution.

property logits: Array

Get logits of the categorical distribution.

property mH: ShapeResultT

The adjoint (conjugate) transposed version of the underlying array.

property mT: ShapeResultT

The version of the underlying array with its last two axes swapped.

property ndim: int

Number of dimensions.

property num_classes: int

Get the fixed number of Bernoulli classes.

numpy(*, force: bool = False) → ndarray[source]
permitted_at_methods = {'set'}
permitted_functions = {}
primary_protected_name
property probabilities: Array

Get the probabilities of the categorical distribution.

protected_axes = {'array': 1}
property protected_shape: tuple[int, ...]

Protected trailing shape of the primary field.

protected_value() → JaxProtectedValue[source]

Return the primary protected value.

protected_values(func: Callable | None = None) → dict[str, V] | None[source]

Read effective values as-is, optionally checking function permissions.

pytree_children
ravel(order: str = 'C') → ShapeResultT[source]

Return a flattened version of the array.

Parameters:

order – The read/write element order.

Returns:

The flattened array.

reshape(*args: int | Sequence[int], order: str = 'C') → ShapeResultT[source]

Return a copy with reshaped protected values.

sample(num_samples: int = 1, prng_key: ArrayLike | None = None) → JaxSample[jax.Array][source]

Sample from the categorical distribution (Jax backend).

Parameters:
  • num_samples – The number of samples to draw.

  • prng_key – The PRNG key to sample with. Defaults to a fresh key seeded from OS entropy, so repeated calls draw different samples.

Returns:

The drawn class indices with the sample axis first.

property shape: tuple[int, ...]

Read the logical shape without collecting fields unless all broadcast.

property sharding: Any

The sharding of the underlying array.

property size: int

Number of elements in the array.

squeeze(axis: int | Sequence[int] | None = None) → ShapeResultT[source]

Return the array with axes of length one removed.

Parameters:

axis – The axis or axes to remove. If omitted, all length-one axes are removed.

Returns:

The squeezed array.

stop_gradient() → Self[source]

Return a copy detached from the autodiff graph.

Subclasses holding NumPy sidecar fields should override this to leave those fields untouched.

Returns:

A copy through which gradients do not propagate.

take_along_axis(indices: jax.Array, axis: int = -1) → ShapeResultT[source]

Return a copy with gathered protected values along a batch dimension.

abstractmethod to_categorical() → CategoricalDistribution[source]

Convert to a two-class categorical distribution.

to_device(device: Any, /, *, stream: int | Any | None = None) → Self[source]

Move the array to the given device.

Parameters:
  • device – The target device.

  • stream – Unsupported by JAX, must be None.

Returns:

A copy with every array-valued field on the given device.

Raises:

NotImplementedError – If stream is not None.

tolist() → Any[source]

Return the array as a (nested) Python list.

transpose(*args: int | Sequence[int] | None) → ShapeResultT[source]

Return a transposed version of the array.

Parameters:

*args – The permutation of the axes, either as a single sequence or as separate integers. If omitted, the axes are reversed.

Returns:

The transposed array.

tree_flatten() → tuple[tuple[Any, ...], Any][source]

Split the object into pytree children and static auxiliary data.

Only fields declared in pytree_children become children, regardless of their values. All other fields become static auxiliary data. Subclasses may override this together with tree_unflatten() to provide different transformation semantics. NumPy array children are rejected by default, including for native tree utilities; opaque eager mapping does not flatten.

Returns:

The children and the auxiliary data.

Raises:
  • TypeError – If the schema, dynamic fields, or static metadata is unsupported.

  • ValueError – If child field names are repeated or unknown.

tree_map_traverse = False
classmethod tree_unflatten(aux_data: Any, children: tuple[Any, ...]) → Self[source]

Rebuild an object from pytree children and auxiliary data.

Neither __init__ nor __post_init__ is called: JAX may provide tracers, placeholders, or cotangents that violate primal-value invariants.

Parameters:
Returns:

The reconstructed object.

type: ClassVar[Literal['categorical', 'bernoulli']] = 'bernoulli'
property unnormalized_probabilities: Array

Get unnormalized probabilities of the categorical distribution.

validate() → None[source]

Raise if effective fields have insufficient rank or incompatible batches.

Call explicitly, or from __post_init__ to check dataclass construction. Fields owned by initialization (init=False) are excluded.

view(dtype: DTypeLike | None = None, type: None = None) → Self[source]

Return a bit-cast view of the array.

Parameters:
  • dtype – The data type to reinterpret the underlying bytes as.

  • type – Unsupported by JAX, must be None.

Returns:

A copy with every array-valued field bit-cast to the given data type.

Raises:

NotImplementedError – If type is not None.

with_protected_values(values: dict[str, Any], func: Callable | None = None) → OwnerT | JaxCategoricalDistribution | jax.Array[source]

Return a copy with a replaced primary protected value.