EntropyReg#
- class brainstate.nn.EntropyReg(weight=1.0, maximize=True, fit_hyper=False)#
Entropy regularization.
Regularizes based on the entropy of softmax-normalized values:
\[L = -\lambda \sum_i p_i \log(p_i)\]where \(p = \text{softmax}(x)\).
When
maximize=True, maximizes entropy (encourages uniform distribution). Whenmaximize=False, minimizes entropy (encourages concentrated distribution).- Parameters:
Examples
>>> import jax.numpy as jnp >>> from brainstate.nn import EntropyReg >>> reg = EntropyReg(weight=0.01, maximize=True) >>> value = jnp.array([1.0, 2.0, 1.0]) >>> loss = reg.loss(value)
Notes
Entropy regularization is useful in attention mechanisms and reinforcement learning to encourage exploration.
The softmax is computed over all elements flattened together (one global distribution), not per-row. For a
(batch, K)input this yields a singlebatch * K-way distribution rather thanbatchindependentK-way ones, so the per-element wording above only matches a 1-D input.