Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions brainpy/_src/math/surrogate/_one_input_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,30 @@ def _as_jax(x):


class Surrogate(object):
"""The base surrograte gradient function."""
"""The base surrograte gradient function.

To customize a surrogate gradient function, you can inherit this class and
implement the `surrogate_fun` and `surrogate_grad` methods.

Examples
--------

>>> import brainpy as bp
>>> import brainpy.math as bm
>>> import jax.numpy as jnp

>>> class MySurrogate(bm.Surrogate):
... def __init__(self, alpha=1.):
... super().__init__()
... self.alpha = alpha
...
... def surrogate_fun(self, x):
... return jnp.sin(x) * self.alpha
...
... def surrogate_grad(self, x):
... return jnp.cos(x) * self.alpha

"""

def __call__(self, x):
x = _as_jax(x)
Expand Down Expand Up @@ -123,7 +146,7 @@ def __init__(self, alpha: float = 4.):
self.alpha = alpha

def surrogate_fun(self, x):
return sci.special.expit(x)
return sci.special.expit(self.alpha * x)

def surrogate_grad(self, x):
sgax = sci.special.expit(x * self.alpha)
Expand Down
3 changes: 2 additions & 1 deletion brainpy/math/surrogate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-



from brainpy._src.math.surrogate._one_input_new import (
Surrogate,

Sigmoid,
sigmoid as sigmoid,

Expand Down