Neural
A simple python package for simulating biological neural networks.
Neural is a CUDA-accelerated Python library consisting of implementation of neuron and synapse models with support of various numerical solvers. Without using a specialized specification language for neuron or synapse models, Neural defines computational models (and neural networks) as sets of ordinary differential equations in plain Python syntax.
For example, an Integrate-and-Fire (IAF) neuron model described by the equation:
\[C\cdot\frac{dV}{dt} = u(t) + b\]can be defined intuitively in neural as:
class IAF(Model):
"""
Integrate-and-Fire neuron model.
"""
Default_States = dict(spike=0.0, v=0.0)
Default_Params = dict(vt=0.025, c=5.0, bias=0.01)
def ode(self, stimulus: float = 0.0) -> None:
"""Defines the gradients of the state variables"""
self.spike = 0.0
# d_state syntax indicates gradient of state variable
self.d_v = 1.0 / self.c * (stimulus + self.bias)
def post(self) -> None:
"""Defines post-processing of model performed at every loop
Here, we define the resetting mechanism of the IAF neuron
and the corresponding spike-states when the voltage `self.v`
exceeds the threshold `self.vt`.
"""
self.spike = 1.0 if self.v > self.vt else 0.0
self.v = 0.0 if self.v > self.vt else self.v
A network of \(10\) IAF neuron running in parallel with different input streams can also be easily defined as:
nn = Network()
inp = nn.input(name='Input to IAF', num=10) # create 10 input streams
iaf = nn.add(IAF, name='My IAFs', num=10) # create 10 IAF nodes
iaf(stimulus=inp) # setup connection from input to IAF
nn.compile(backend='cuda', dtype=np.float_) # compile network to pycuda SourceModules
iaf.record('spike', 'v') # record spike and v states of iaf models
# assuming my_input_array is pycuda.gpuarray.GPUArray instance
# of shape (number of time steps, 10), we can populate data into the
# input component as follows
inp(my_input_array)
nn.run(dt, verbose=True) # simulate the entire network
print(iaf.recorder.v.shape) # should be np.float_ ndarray of shape (number of time steps, 10)
Neural was originally written by Dr. Chung-Heng Yeh, and
utilized pycuda and custom code-generation (from disassembled bytecodes generated
using dis) for GPU-acceleration.
The package was instrumental in many collaborations between Chung-Heng and I, and subsequently my other
research projects (see here for an example).
Since October of 2019, I’ve taken a lead on the
development of the package, and is current in the progress of transitioning to using
numba for both CPU and GPU accelerations. During this process,
I’ve also contributed to the numba project.