Arctan#
- class braintools.surrogate.Arctan(alpha=1.0)#
Judge spiking state with an arctan function.
This class implements a surrogate gradient using the arctangent function.
The forward function:
\[\begin{split}g(x) = \begin{cases} 1, & x \geq 0 \\ 0, & x < 0 \\ \end{cases}\end{split}\]The original function:
\[g(x) = \frac{1}{\pi} \arctan(\frac{\pi}{2}\alpha x) + \frac{1}{2}\]Backward function:
\[g'(x) = \frac{\alpha}{2(1 + (\frac{\pi}{2}\alpha x)^2)}\]- Parameters:
alpha (float, optional) – Parameter controlling the steepness of the surrogate gradient. Higher values make the transition sharper. Default is 1.0.
See also
arctanFunction version of this class.
Examples
>>> import brainstate >>> import jax.numpy as jnp >>> >>> # Create an arctangent surrogate >>> arctan_fn = braintools.surrogate.Arctan(alpha=2.0) >>> >>> # Apply to membrane potentials >>> x = jnp.array([-1.0, 0.0, 1.0]) >>> spikes = arctan_fn(x) >>> print(spikes) # Binary spike output
>>> import jax >>> import brainstate as brainstate >>> import matplotlib.pyplot as plt >>> xs = jax.numpy.linspace(-3, 3, 1000) >>> for alpha in [0.5, 1., 2., 4.]: >>> arctan_fn = braintools.surrogate.Arctan(alpha=alpha) >>> grads = brainstate.augment.vector_grad(arctan_fn)(xs) >>> plt.plot(xs, grads, label=r'$\alpha$=' + str(alpha)) >>> plt.legend() >>> plt.show()
Notes
The arctangent function provides smooth gradients with polynomial decay, offering a balance between the fast decay of exponential functions and the slow decay of linear functions.