Skip to content

Commit

Permalink
add brainpy.math.Surrogate
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoming0625 committed Feb 29, 2024
1 parent 64d8f54 commit 6dd5125
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 24 additions & 1 deletion 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
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

0 comments on commit 6dd5125

Please sign in to comment.