Sorry for not explaining what J and X is, because the code is just borrowed from example of extending Nengo (example of Rectified Linear Neurons) and took only the function (rates) from “Direct” neurons type. So I though they were something common to be used.
The Code:
# In[ ]:
import numpy as np
import matplotlib.pyplot as plt
import nengo
class RectifiedLinear(nengo.neurons.NeuronType): # Neuron types must subclass `nengo.Neurons`
"""A rectified linear neuron model."""
# We don't need any additional parameters here;
# gain and bias are sufficient. But, if we wanted
# more parameters, we could accept them by creating
# an __init__ method.
def gain_bias(self, max_rates, intercepts):
"""Return gain and bias given maximum firing rate and x-intercept."""
gain = max_rates / (1 - intercepts)
bias = -intercepts * gain
return gain, bias
def rates(self, x, gain, bias):
"""Always returns ``x``."""
return x
def step_math(self, dt, J, output):
"""Compute rates in Hz for input current (incl. bias)"""
output[...] = np.maximum(0., J)
# In[ ]:
from nengo.builder import Operator
class SimRectifiedLinear(Operator):
"""Set a neuron model output for the given input current.
Implements ``neurons.step_math(dt, J, output, *states)``.
Parameters
----------
neurons : NeuronType
The `.NeuronType`, which defines a ``step_math`` function.
J : Signal
The input current.
output : Signal
The neuron output signal that will be set.
states : list, optional (Default: None)
A list of additional neuron state signals set by ``step_math``.
tag : str, optional (Default: None)
A label associated with the operator, for debugging purposes.
Attributes
----------
J : Signal
The input current.
neurons : NeuronType
The `.NeuronType`, which defines a ``step_math`` function.
output : Signal
The neuron output signal that will be set.
states : list
A list of additional neuron state signals set by ``step_math``.
tag : str or None
A label associated with the operator, for debugging purposes.
Notes
-----
1. sets ``[output] + states``
2. incs ``[]``
3. reads ``[J]``
4. updates ``[]``
"""
def __init__(self, neurons, J, output, states=None, tag=None):
super(SimRectifiedLinear, self).__init__(tag=tag)
self.neurons = neurons
self.J = J
self.output = output
self.states = [] if states is None else states
self.sets = [output]
self.incs = []
self.reads = [J]
self.updates = []
def _descstr(self):
return '%s, %s, %s' % (self.neurons, self.J, self.output)
def make_step(self, signals, dt, rng):
J = signals[self.J]
output = signals[self.output]
def step_simrectifiedlinear():
self.neurons.step_math(dt, J, output)
return step_simrectifiedlinear
# In[ ]:
from nengo.builder import Builder
from nengo.builder.operator import Copy
from nengo.builder.signal import Signal
from nengo.neurons import NeuronType
@Builder.register(RectifiedLinear)
def build_rectified_linear(model, neuron_type, neurons):
model.operators.append(SimRectifiedLinear(
output=model.sig[neurons]['out'], J=model.sig[neurons]['in'],neurons=neuron_type))