brainpy.math.surrogate.piecewise_leaky_relu

brainpy.math.surrogate.piecewise_leaky_relu#

brainpy.math.surrogate.piecewise_leaky_relu(x, c=0.01, w=1.0)[source]#

Judge spiking state with a piecewise leaky relu function [1] [2] [3] [4] [5] [6] [7] [8].

If origin=False, computes the forward function:

\[\begin{split}g(x) = \begin{cases} 1, & x \geq 0 \\ 0, & x < 0 \\ \end{cases}\end{split}\]

If origin=True, computes the original function:

\[\begin{split}\begin{split}g(x) = \begin{cases} cx + cw, & x < -w \\ \frac{1}{2w}x + \frac{1}{2}, & -w \leq x \leq w \\ cx - cw + 1, & x > w \\ \end{cases}\end{split}\end{split}\]

Backward function:

\[\begin{split}\begin{split}g'(x) = \begin{cases} \frac{1}{w}, & |x| \leq w \\ c, & |x| > w \end{cases}\end{split}\end{split}\]
>>> import brainpy as bp
>>> import brainpy.math as bm
>>> import matplotlib.pyplot as plt
>>> bp.visualize.get_figure(1, 1, 4, 6)
>>> xs = bm.linspace(-3, 3, 1000)
>>> for c in [0.01, 0.05, 0.1]:
>>>   for w in [1., 2.]:
>>>     grads1 = bm.vector_grad(bm.surrogate.piecewise_leaky_relu)(xs, c=c, w=w)
>>>     plt.plot(bm.as_numpy(xs), bm.as_numpy(grads1), label=f'x={c}, w={w}')
>>> plt.legend()
>>> plt.show()

(Source code, png, hires.png, pdf)

../../_images/brainpy-math-surrogate-piecewise_leaky_relu-1.png
Parameters:
  • x (jax.Array, Array) – The input data.

  • c (float) – When \(|x| > w\) the gradient is c.

  • w (float) – When \(|x| <= w\) the gradient is 1 / w.

Returns:

out – The spiking state.

Return type:

jax.Array

References