Skip to content

Commit

Permalink
Merge pull request #177 from PKU-NIP-Lab/updates
Browse files Browse the repository at this point in the history
Updates and fixes
  • Loading branch information
chaoming0625 authored Apr 22, 2022
2 parents 8be34fe + fe95783 commit 6bf7cc0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 237 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ publishment.md

development

examples/simulation/data
examples/analysis/data
extensions/.idea
extensions/wheelhouse
Expand Down
235 changes: 1 addition & 234 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BrainPy is a flexible, efficient, and extensible framework for computational neu



## Installation
## Install

BrainPy is based on Python (>=3.7) and can be installed on Linux (Ubuntu 16.04 or later), macOS (10.12 or later), and Windows platforms. Install the latest version of BrainPy:

Expand All @@ -42,239 +42,6 @@ The following packages are required for ``BrainPy``:
For detailed installation instructions, please refer to the documentation: [Quickstart/Installation](https://brainpy.readthedocs.io/en/latest/quickstart/installation.html)



## Examples



```python
import brainpy as bp
```



### 1. Operator level

Mathematical operators in BrainPy are the same as those in NumPy.

```python
>>> import numpy as np
>>> import brainpy.math as bm

# array creation
>>> np_arr = np.zeros((2, 4)); np_arr
array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> bm_arr = bm.zeros((2, 4)); bm_arr
JaxArray([[0., 0., 0., 0.],
[0., 0., 0., 0.]], dtype=float32)

# in-place updating
>>> np_arr[0] += 1.; np_arr
array([[1., 1., 1., 1.],
[0., 0., 0., 0.]])
>>> bm_arr[0] += 1.; bm_arr
JaxArray([[1., 1., 1., 1.],
[0., 0., 0., 0.]], dtype=float32)

# random number generation
>>> np.random.uniform(-0.1, 0.1, (2, 3))
array([[-0.02773637, 0.03766689, -0.01363128],
[-0.01946991, -0.06669802, 0.09426067]])
>>> bm.random.uniform(-0.1, 0.1, (2, 3))
JaxArray([[-0.03044081, -0.07787752, 0.04346445],
[-0.01366713, -0.0522548 , 0.04372055]], dtype=float32)
```



### 2. Integrator level

Numerical methods for ordinary differential equations (ODEs).

```python
sigma = 10; beta = 8/3; rho = 28

@bp.odeint(method='rk4')
def lorenz_system(x, y, z, t):
dx = sigma * (y - x)
dy = x * (rho - z) - y
dz = x * y - beta * z
return dx, dy, dz
```



Numerical methods for stochastic differential equations (SDEs).

```python
sigma = 10; beta = 8/3; rho = 28

def lorenz_noise(x, y, z, t):
return 0.1*x, 0.1*y, 0.1*z

@bp.odeint(method='milstein', g=lorenz_noise)
def lorenz_system(x, y, z, t):
dx = sigma * (y - x)
dy = x * (rho - z) - y
dz = x * y - beta * z
return dx, dy, dz
```



Numerical methods for delay differential equations (DDEs).

```python
xdelay = bm.TimeDelay(bm.zeros(1), delay_len=1., before_t0=1., dt=0.01)

@bp.ddeint(method='rk4', state_delays={'x': xdelay})
def second_order_eq(x, y, t):
dx = y
dy = -y - 2 * x - 0.5 * xdelay(t - 1)
return dx, dy
```


Numerical methods for fractional differential equations (FDEs).

```python
sigma = 10; beta = 8/3; rho = 28

@bp.fdeint(method='GLShortMemory', alpha=0.97)
def fractional_lorenz(x, y, z, t):
dx = sigma * (y - x)
dy = x * (rho - z) - y
dz = x * y - beta * z
return dx, dy, dz
```


### 3. Dynamics simulation level

Building an E-I balance network.

```python
class EINet(bp.dyn.Network):
def __init__(self):
E = bp.dyn.LIF(3200, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.)
I = bp.dyn.LIF(800, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.)
E.V[:] = bp.math.random.randn(3200) * 2 - 60.
I.V[:] = bp.math.random.randn(800) * 2 - 60.

E2E = bp.dyn.ExpCOBA(E, E, bp.conn.FixedProb(prob=0.02), E=0., g_max=0.6, tau=5.)
E2I = bp.dyn.ExpCOBA(E, I, bp.conn.FixedProb(prob=0.02), E=0., g_max=0.6, tau=5.)
I2E = bp.dyn.ExpCOBA(I, E, bp.conn.FixedProb(prob=0.02), E=-80., g_max=6.7, tau=10.)
I2I = bp.dyn.ExpCOBA(I, I, bp.conn.FixedProb(prob=0.02), E=-80., g_max=6.7, tau=10.)

super(EINet, self).__init__(E2E, E2I, I2E, I2I, E=E, I=I)
```

Simulating a whole-brain network by using rate models.

```python
class WholeBrainNet(bp.dyn.Network):
def __init__(self):
super(WholeBrainNet, self).__init__()

self.areas = bp.dyn.RateFHN(80, x_ou_sigma=0.01, y_ou_sigma=0.01, name='fhn')
self.conns = bp.dyn.DiffusiveDelayCoupling(self.areas, self.areas, 'x->input',
conn_mat=conn_mat,
delay_mat=delay_mat)

def update(self, _t, _dt):
self.conns.update(_t, _dt)
self.areas.update(_t, _dt)
```



### 4. Dynamics training level

Training an echo state network.

```python
i = bp.nn.Input(3)
r = bp.nn.Reservoir(100)
o = bp.nn.LinearReadout(3)

net = i >> r >> o

trainer = bp.nn.RidgeTrainer(net, beta=1e-5) # Ridge Regression

trainer = bp.nn.ForceTrainer(net, alpha=1.) # FORCE Learning
```



Training a next-generation reservoir computing model.

```python
i = bp.nn.Input(3)
r = bp.nn.NVAR(delay=2, order=2)
o = bp.nn.LinearReadout(3)

net = i >> r >> o

trainer = bp.nn.RidgeTrainer(net, beta=1e-5)
```



Training an artificial recurrent neural network.

```python
i = bp.nn.Input(3)
l1 = bp.nn.VanillaRNN(100)
l2 = bp.nn.VanillaRNN(200)
o = bp.nn.Dense(10)

net = i >> l1 >> l2 >> o

trainer = bp.nn.BPTT(net,
loss='cross_entropy_loss',
optimizer=bp.optim.Adam(0.01))
```



### 5. Dynamics analysis level

Analyzing a low-dimensional FitzHugh–Nagumo neuron model.

```python
model = bp.dyn.FHN(1)

analyzer = bp.analysis.PhasePlane2D(
model,
target_vars={'V': [-3, 3], 'w': [-3., 3.]},
pars_update={'I_ext': 0.8},
resolutions=0.01
)
analyzer.plot_nullcline()
analyzer.plot_vector_field()
analyzer.plot_fixed_point()
analyzer.show_figure()
```

Analyzing a high-dimensional continuous-attractor neural network (CANN).

```python
cann_model = CANN(100) # your high-dimensional CANN network

finder = bp.analysis.SlowPointFinder(f_cell=cann_model)
finder.find_fps_with_gd_method(candidates=bm.random.random((1000, 100)))
finder.filter_loss(tolerance=1e-5)
finder.keep_unique(tolerance=0.03)
finder.exclude_outliers(0.1)
```


### 6. More others

For **more functions and examples**, please refer to the [documentation](https://brainpy.readthedocs.io/) and [examples](https://brainpy-examples.readthedocs.io/).


## License

[GNU General Public License v3.0](https://github.com/PKU-NIP-Lab/BrainPy/blob/master/LICENSE)
2 changes: 1 addition & 1 deletion brainpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = "2.1.5"
__version__ = "2.1.7"


try:
Expand Down
5 changes: 3 additions & 2 deletions brainpy/dyn/synapses/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,9 @@ def update(self, _t, _dt):
post_g = bm.sum(self.g)
if not self.conn.include_self:
post_g = post_g - self.g
post_g = post_g * self.g_max
else:
post_g = self.g * self.g_max
post_g = self.g @ self.g_max
elif isinstance(self.conn, One2One):
post_g = self.g_max * self.g
else:
Expand All @@ -1242,4 +1243,4 @@ def update(self, _t, _dt):

# output
g_inf = 1 + self.cc_Mg / self.beta * bm.exp(-self.alpha * self.post.V)
self.post.input -= post_g * (self.post.V - self.E) / g_inf
self.post.input += post_g * (self.E - self.post.V) / g_inf
67 changes: 67 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,73 @@ brainpy 2.x (LTS)
*****************


Version 2.1.6 (2022.04.20)
==========================


What's Changed
~~~~~~~~~~~~~~




Version 2.1.5 (2022.04.18)
==========================


What's Changed
~~~~~~~~~~~~~~

* ``brainpy.math.random.shuffle`` is numpy like by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#153 <https://github.com/PKU-NIP-Lab/BrainPy/pull/153>`_
* update LICENSE by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#155 <https://github.com/PKU-NIP-Lab/BrainPy/pull/155>`_
* docs: add m1 warning by `@ztqakita <https://github.com/ztqakita>`_ in `#154 <https://github.com/PKU-NIP-Lab/BrainPy/pull/154>`_
* compatible apis of 'brainpy.math' with those of 'jax.numpy' in most modules by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#156 <https://github.com/PKU-NIP-Lab/BrainPy/pull/156>`_
* Important updates by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#157 <https://github.com/PKU-NIP-Lab/BrainPy/pull/157>`_
* Updates by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#159 <https://github.com/PKU-NIP-Lab/BrainPy/pull/159>`_
* Add LayerNorm, GroupNorm, and InstanceNorm as nn_nodes in normalization.py by `@c-xy17 <https://github.com/c-xy17>`_ in `#162 <https://github.com/PKU-NIP-Lab/BrainPy/pull/162>`_
* feat: add conv & pooling nodes by `@ztqakita <https://github.com/ztqakita>`_ in `#161 <https://github.com/PKU-NIP-Lab/BrainPy/pull/161>`_
* fix: update setup.py by `@ztqakita <https://github.com/ztqakita>`_ in `#163 <https://github.com/PKU-NIP-Lab/BrainPy/pull/163>`_
* update setup.py by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#165 <https://github.com/PKU-NIP-Lab/BrainPy/pull/165>`_
* fix: change trigger condition by `@ztqakita <https://github.com/ztqakita>`_ in `#166 <https://github.com/PKU-NIP-Lab/BrainPy/pull/166>`_
* fix: add build_conn() function by `@ztqakita <https://github.com/ztqakita>`_ in `#164 <https://github.com/PKU-NIP-Lab/BrainPy/pull/164>`_
* update synapses by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#167 <https://github.com/PKU-NIP-Lab/BrainPy/pull/167>`_
* get the deserved name: brainpy by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#168 <https://github.com/PKU-NIP-Lab/BrainPy/pull/168>`_
* update tests by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#169 <https://github.com/PKU-NIP-Lab/BrainPy/pull/169>`_

**Full Changelog**\ : `V2.1.4...V2.1.5 <https://github.com/PKU-NIP-Lab/BrainPy/compare/V2.1.4...V2.1.5>`_



Version 2.1.4 (2022.04.04)
==========================


What's Changed
~~~~~~~~~~~~~~

* fix doc parsing bug by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#127 <https://github.com/PKU-NIP-Lab/BrainPy/pull/127>`_
* Update overview_of_dynamic_model.ipynb by `@c-xy17 <https://github.com/c-xy17>`_ in `#129 <https://github.com/PKU-NIP-Lab/BrainPy/pull/129>`_
* Reorganization of ``brainpylib.custom_op`` and adding interface in ``brainpy.math`` by `@ztqakita <https://github.com/ztqakita>`_ in `#128 <https://github.com/PKU-NIP-Lab/BrainPy/pull/128>`_
* Fix: modify ``register_op`` and brainpy.math interface by `@ztqakita <https://github.com/ztqakita>`_ in `#130 <https://github.com/PKU-NIP-Lab/BrainPy/pull/130>`_
* new features about RNN training and delay differential equations by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#132 <https://github.com/PKU-NIP-Lab/BrainPy/pull/132>`_
* Fix `#123 <https://github.com/PKU-NIP-Lab/BrainPy/issues/123>`_\ : Add low-level operators docs and modify register_op by `@ztqakita <https://github.com/ztqakita>`_ in `#134 <https://github.com/PKU-NIP-Lab/BrainPy/pull/134>`_
* feat: add generate_changelog by `@ztqakita <https://github.com/ztqakita>`_ in `#135 <https://github.com/PKU-NIP-Lab/BrainPy/pull/135>`_
* fix `#133 <https://github.com/PKU-NIP-Lab/BrainPy/issues/133>`_\ , support batch size training with offline algorithms by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#136 <https://github.com/PKU-NIP-Lab/BrainPy/pull/136>`_
* fix `#84 <https://github.com/PKU-NIP-Lab/BrainPy/issues/84>`_\ : support online training algorithms by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#137 <https://github.com/PKU-NIP-Lab/BrainPy/pull/137>`_
* feat: add the batch normalization node by `@c-xy17 <https://github.com/c-xy17>`_ in `#138 <https://github.com/PKU-NIP-Lab/BrainPy/pull/138>`_
* fix: fix shape checking error by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#139 <https://github.com/PKU-NIP-Lab/BrainPy/pull/139>`_
* solve `#131 <https://github.com/PKU-NIP-Lab/BrainPy/issues/131>`_\ , support efficient synaptic computation for special connection types by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#140 <https://github.com/PKU-NIP-Lab/BrainPy/pull/140>`_
* feat: update the API and test for batch normalization by `@c-xy17 <https://github.com/c-xy17>`_ in `#142 <https://github.com/PKU-NIP-Lab/BrainPy/pull/142>`_
* Node is default trainable by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#143 <https://github.com/PKU-NIP-Lab/BrainPy/pull/143>`_
* Updates training apis and docs by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#145 <https://github.com/PKU-NIP-Lab/BrainPy/pull/145>`_
* fix: add dependencies and update version by `@ztqakita <https://github.com/ztqakita>`_ in `#147 <https://github.com/PKU-NIP-Lab/BrainPy/pull/147>`_
* update requirements by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#146 <https://github.com/PKU-NIP-Lab/BrainPy/pull/146>`_
* data pass of the Node is default SingleData by `@chaoming0625 <https://github.com/chaoming0625>`_ in `#148 <https://github.com/PKU-NIP-Lab/BrainPy/pull/148>`_

**Full Changelog**\ : `V2.1.3...V2.1.4 <https://github.com/PKU-NIP-Lab/BrainPy/compare/V2.1.3...V2.1.4>`_



Version 2.1.3 (2022.03.27)
==========================

Expand Down

0 comments on commit 6bf7cc0

Please sign in to comment.