braincore.surrogate.slayer_grad#
- braincore.surrogate.slayer_grad(x, alpha=1.0)[source]#
Spike function with the slayer surrogate gradient function.
Forward function:
\[\begin{split}g(x) = \begin{cases} 1, & x \geq 0 \\ 0, & x < 0 \\ \end{cases}\end{split}\]Backward function:
\[g'(x) = \exp(-\alpha |x|)\]>>> import braincore as bc >>> import matplotlib.pyplot as plt >>> xs = jax.numpy.linspace(-3, 3, 1000) >>> for alpha in [0.5, 1., 2., 4.]: >>> grads = bc.vector_grad(bc.surrogate.slayer_grad)(xs, alpha) >>> plt.plot(xs, grads, label=r'$\alpha$=' + str(alpha)) >>> plt.legend() >>> plt.show()
- Parameters:
x (jax.Array, Array) – The input data.
alpha (float) – Parameter to control smoothness of gradient
- Returns:
out – The spiking state.
- Return type:
jax.Array
References