element_wise#
- class braintrace.element_wise(weight, *, weight_fn=None)[source]#
ETP-aware element-wise operation.
Applies
weight_fntoweightinside the ETP primitive, so the transform is visible to the eligibility-trace compiler. The operation is treated as diagonal in the hidden-state space; the weight participates in trace computation as a per-element trainable parameter.- Parameters:
weight (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Weight parameter (may be abrainunit.Quantity).weight_fn (
Callable[[Array|ndarray|bool|number|bool|int|float|complex|Quantity],Array|ndarray|bool|number|bool|int|float|complex|Quantity] |None) – Element-wise function applied to the raw weight mantissa inside the primitive. WhenNone(default), the identity is used and the output equalsweightexactly. The transform operates on the unitless mantissa; physical units are split off before and recombined after.
- Returns:
weight_fn(weight)(orweightwhenweight_fnisNone), with the same shape asweight.- Return type:
Array|ndarray|bool|number|bool|int|float|complex|Quantity
Examples
>>> import brainstate >>> import braintrace >>> >>> brainstate.environ.set(precision=64) >>> w = brainstate.random.randn(5) >>> y = braintrace.element_wise(w) >>> print(y.shape) (5,) >>> >>> # Apply a non-linearity to the weight >>> import jax.numpy as jnp >>> y1 = braintrace.element_wise(w, weight_fn=jnp.tanh) >>> print(y1.shape) (5,)