Skip to content

Commit

Permalink
Merge pull request #356 from chaoming0625/master
Browse files Browse the repository at this point in the history
fix `Array` transform bug
  • Loading branch information
chaoming0625 committed Apr 8, 2023
2 parents 00c790a + b0fd30b commit c0c7910
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 146 deletions.
3 changes: 1 addition & 2 deletions brainpy/_src/math/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,12 +1655,11 @@ def update(self, value):

def _jaxarray_unflatten(aux_data, flat_contents):
r = Array(*flat_contents)
r._transform_context = aux_data[0]
return r


register_pytree_node(Array,
lambda t: ((t.value,), (t._transform_context,)),
lambda t: ((t.value,), None),
_jaxarray_unflatten)

register_pytree_node(Variable,
Expand Down
4 changes: 4 additions & 0 deletions brainpy/_src/tools/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
'SUPPORT_NUMBA',
]

_minimal_brainpylib_version = '0.1.7'


def import_numba():
if numba is None:
Expand All @@ -35,6 +37,8 @@ def import_brainpylib():
if brainpylib is None:
raise ModuleNotFoundError('brainpylib is needed. Please install brainpylib through:\n'
'> pip install brainpylib\n\n')
if brainpylib.__version__ < _minimal_brainpylib_version:
raise SystemError(f'This version of brainpy needs brainpylib >= {_minimal_brainpylib_version}.')
return brainpylib


Expand Down
152 changes: 8 additions & 144 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,155 +1,19 @@
# Change from Version 2.3.4 to Version 2.3.5


This release (under the branch of ``brainpy=2.3.x``) continues to add supports for brain-inspired computation.
This release continues to add supports for improving the usability of BrainPy.


## New Features


### 1. ``brainpy.share`` for sharing data across submodules
1. New data structures for object-oriented transformations.
- ``NodeList`` and ``NodeDict`` for a list/tuple/dict of ``BrainPyObject`` instances.
- ``ListVar`` and ``DictVar`` for a list/tuple/dict of brainpy data.
2. `Clip` transformation for brainpy initializers.
3. All ``brainpylib`` operators are accessible in ``brainpy.math`` module.
4. Enable monitoring GPU models on CPU when setting ``DSRunner(..., memory_efficient=True)``. This setting can usually reduce so much memory usage.
5. ``brainpylib`` wheels on the linux platform support the GPU operators. Users can install gpu version of ``brainpylib`` (require ``brainpylib>=0.1.7``) directly by ``pip install brainpylib``.

In this release, we abstract the shared data as a ``brainpy.share`` object.

This object together with ``brainpy.Delay`` we will introduce below
constitute the support that enable to define SNN models like ANN ones.


### 2. ``brainpy.Delay`` for delay processing

``Delay`` is abstracted as a dynamical system, which can be updated / retrieved by users.

```python
import brainpy as bp

class EINet(bp.DynamicalSystemNS):
def __init__(self, scale=1.0, e_input=20., i_input=20., delay=None):
super().__init__()

self.bg_exc = e_input
self.bg_inh = i_input

# network size
num_exc = int(3200 * scale)
num_inh = int(800 * scale)

# neurons
pars = dict(V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
V_initializer=bp.init.Normal(-55., 2.), input_var=False)
self.E = bp.neurons.LIF(num_exc, **pars)
self.I = bp.neurons.LIF(num_inh, **pars)

# synapses
we = 0.6 / scale # excitatory synaptic weight (voltage)
wi = 6.7 / scale # inhibitory synaptic weight
self.E2E = bp.experimental.Exponential(
bp.conn.FixedProb(0.02, pre=self.E.size, post=self.E.size),
g_max=we, tau=5., out=bp.experimental.COBA(E=0.)
)
self.E2I = bp.experimental.Exponential(
bp.conn.FixedProb(0.02, pre=self.E.size, post=self.I.size, ),
g_max=we, tau=5., out=bp.experimental.COBA(E=0.)
)
self.I2E = bp.experimental.Exponential(
bp.conn.FixedProb(0.02, pre=self.I.size, post=self.E.size),
g_max=wi, tau=10., out=bp.experimental.COBA(E=-80.)
)
self.I2I = bp.experimental.Exponential(
bp.conn.FixedProb(0.02, pre=self.I.size, post=self.I.size),
g_max=wi, tau=10., out=bp.experimental.COBA(E=-80.)
)
self.delayE = bp.Delay(self.E.spike, entries={'E': delay})
self.delayI = bp.Delay(self.I.spike, entries={'I': delay})

def update(self):
e_spike = self.delayE.at('E')
i_spike = self.delayI.at('I')
e_inp = self.E2E(e_spike, self.E.V) + self.I2E(i_spike, self.E.V) + self.bg_exc
i_inp = self.I2I(i_spike, self.I.V) + self.E2I(e_spike, self.I.V) + self.bg_inh
self.delayE(self.E(e_inp))
self.delayI(self.I(i_inp))

```



### 3. ``brainpy.checkpoints.save_pytree`` and ``brainpy.checkpoints.load_pytree`` for saving/loading target from the filename

Now we can directly use ``brainpy.checkpoints.save_pytree`` to save a
network state into the filepath we specified.

Similarly, we can use ``brainpy.checkpoints.load_pytree`` to load
states from the given file path.


### 4. More ANN layers


- brainpy.layers.ConvTranspose1d
- brainpy.layers.ConvTranspose2d
- brainpy.layers.ConvTranspose3d
- brainpy.layers.Conv1dLSTMCell
- brainpy.layers.Conv2dLSTMCell
- brainpy.layers.Conv3dLSTMCell


### 5. More compatible dense operators

PyTorch operators:

- brainpy.math.Tensor
- brainpy.math.flatten
- brainpy.math.cat
- brainpy.math.abs
- brainpy.math.absolute
- brainpy.math.acos
- brainpy.math.arccos
- brainpy.math.acosh
- brainpy.math.arccosh
- brainpy.math.add
- brainpy.math.addcdiv
- brainpy.math.addcmul
- brainpy.math.angle
- brainpy.math.asin
- brainpy.math.arcsin
- brainpy.math.asinh
- brainpy.math.arcsin
- brainpy.math.atan
- brainpy.math.arctan
- brainpy.math.atan2
- brainpy.math.atanh


TensorFlow operators:

- brainpy.math.concat
- brainpy.math.reduce_sum
- brainpy.math.reduce_max
- brainpy.math.reduce_min
- brainpy.math.reduce_mean
- brainpy.math.reduce_all
- brainpy.math.reduce_any
- brainpy.math.reduce_logsumexp
- brainpy.math.reduce_prod
- brainpy.math.reduce_std
- brainpy.math.reduce_variance
- brainpy.math.reduce_euclidean_norm
- brainpy.math.unsorted_segment_sqrt_n
- brainpy.math.segment_mean
- brainpy.math.unsorted_segment_sum
- brainpy.math.unsorted_segment_prod
- brainpy.math.unsorted_segment_max
- brainpy.math.unsorted_segment_min
- brainpy.math.unsorted_segment_mean
- brainpy.math.segment_sum
- brainpy.math.segment_prod
- brainpy.math.segment_max
- brainpy.math.segment_min
- brainpy.math.clip_by_value
- brainpy.math.cast


### Others

- Remove the hard requirements of ``brainpylib`` and ``numba``.

0 comments on commit c0c7910

Please sign in to comment.