class Test(object): ntp = NeuronTypeParam('ntp', default=None)
class Ensemble(NengoObject): """A group of neurons that collectively represent a vector. Parameters ---------- n_neurons : int The number of neurons. dimensions : int The number of representational dimensions. radius : int, optional The representational radius of the ensemble. encoders : Distribution or ndarray (`n_neurons`, `dimensions`), optional The encoders, used to transform from representational space to neuron space. Each row is a neuron's encoder, each column is a representational dimension. intercepts : Distribution or ndarray (`n_neurons`), optional The point along each neuron's encoder where its activity is zero. If e is the neuron's encoder, then the activity will be zero when dot(x, e) <= c, where c is the given intercept. max_rates : Distribution or ndarray (`n_neurons`), optional The activity of each neuron when dot(x, e) = 1, where e is the neuron's encoder. eval_points : Distribution or ndarray (`n_eval_points`, `dims`), optional The evaluation points used for decoder solving, spanning the interval (-radius, radius) in each dimension, or a distribution from which to choose evaluation points. Default: ``UniformHypersphere``. n_eval_points : int, optional The number of evaluation points to be drawn from the `eval_points` distribution. If None (the default), then a heuristic is used to determine the number of evaluation points. neuron_type : Neurons, optional The model that simulates all neurons in the ensemble. noise : StochasticProcess, optional Random noise injected directly into each neuron in the ensemble as current. A sample is drawn for each individual neuron on every simulation step. seed : int, optional The seed used for random number generation. label : str, optional A name for the ensemble. Used for debugging and visualization. """ n_neurons = IntParam(default=None, low=1) dimensions = IntParam(default=None, low=1) radius = NumberParam(default=1, low=1e-10) neuron_type = NeuronTypeParam(default=LIF()) encoders = DistributionParam(default=UniformHypersphere(surface=True), sample_shape=('n_neurons', 'dimensions')) intercepts = DistributionParam(default=Uniform(-1.0, 1.0), optional=True, sample_shape=('n_neurons', )) max_rates = DistributionParam(default=Uniform(200, 400), optional=True, sample_shape=('n_neurons', )) n_eval_points = IntParam(default=None, optional=True) eval_points = DistributionParam(default=UniformHypersphere(), sample_shape=('*', 'dimensions')) bias = DistributionParam(default=None, optional=True, sample_shape=('n_neurons', )) gain = DistributionParam(default=None, optional=True, sample_shape=('n_neurons', )) noise = StochasticProcessParam(default=None, optional=True) seed = IntParam(default=None, optional=True) label = StringParam(default=None, optional=True) def __init__(self, n_neurons, dimensions, radius=Default, encoders=Default, intercepts=Default, max_rates=Default, eval_points=Default, n_eval_points=Default, neuron_type=Default, gain=Default, bias=Default, noise=Default, seed=Default, label=Default): self.n_neurons = n_neurons self.dimensions = dimensions self.radius = radius self.encoders = encoders self.intercepts = intercepts self.max_rates = max_rates self.label = label self.n_eval_points = n_eval_points self.eval_points = eval_points self.bias = bias self.gain = gain self.neuron_type = neuron_type self.noise = noise self.seed = seed self._neurons = Neurons(self) def __getitem__(self, key): return ObjView(self, key) def __len__(self): return self.dimensions @property def neurons(self): return self._neurons @neurons.setter def neurons(self, dummy): raise AttributeError("neurons cannot be overwritten.") @property def probeable(self): return ["decoded_output", "input"] @property def size_in(self): return self.dimensions @property def size_out(self): return self.dimensions
class Test: ntp = NeuronTypeParam("ntp", default=None)
class Ensemble(NengoObject): """A group of neurons that collectively represent a vector. Parameters ---------- n_neurons : int The number of neurons. dimensions : int The number of representational dimensions. radius : int, optional (Default: 1.0) The representational radius of the ensemble. encoders : Distribution or (n_neurons, dimensions) array_like, optional \ (Default: UniformHypersphere(surface=True)) The encoders used to transform from representational space to neuron space. Each row is a neuron's encoder; each column is a representational dimension. intercepts : Distribution or (n_neurons,) array_like, optional \ (Default: ``nengo.dists.Uniform(-1.0, 1.0)``) The point along each neuron's encoder where its activity is zero. If ``e`` is the neuron's encoder, then the activity will be zero when ``dot(x, e) <= c``, where ``c`` is the given intercept. max_rates : Distribution or (n_neurons,) array_like, optional \ (Default: ``nengo.dists.Uniform(200, 400)``) The activity of each neuron when the input signal ``x`` is magnitude 1 and aligned with that neuron's encoder ``e``; i.e., when ``dot(x, e) = 1``. eval_points : Distribution or (n_eval_points, dims) array_like, optional \ (Default: ``nengo.dists.UniformHypersphere()``) The evaluation points used for decoder solving, spanning the interval (-radius, radius) in each dimension, or a distribution from which to choose evaluation points. n_eval_points : int, optional (Default: None) The number of evaluation points to be drawn from the ``eval_points`` distribution. If None, then a heuristic is used to determine the number of evaluation points. neuron_type : `~nengo.neurons.NeuronType`, optional \ (Default: ``nengo.LIF()``) The model that simulates all neurons in the ensemble (see `~nengo.neurons.NeuronType`). gain : Distribution or (n_neurons,) array_like (Default: None) The gains associated with each neuron in the ensemble. If None, then the gain will be solved for using ``max_rates`` and ``intercepts``. bias : Distribution or (n_neurons,) array_like (Default: None) The biases associated with each neuron in the ensemble. If None, then the gain will be solved for using ``max_rates`` and ``intercepts``. noise : Process, optional (Default: None) Random noise injected directly into each neuron in the ensemble as current. A sample is drawn for each individual neuron on every simulation step. normalize_encoders : bool, optional (Default: True) Indicates whether the encoders should be normalized. label : str, optional (Default: None) A name for the ensemble. Used for debugging and visualization. seed : int, optional (Default: None) The seed used for random number generation. Attributes ---------- bias : Distribution or (n_neurons,) array_like or None The biases associated with each neuron in the ensemble. dimensions : int The number of representational dimensions. encoders : Distribution or (n_neurons, dimensions) array_like The encoders, used to transform from representational space to neuron space. Each row is a neuron's encoder, each column is a representational dimension. eval_points : Distribution or (n_eval_points, dims) array_like The evaluation points used for decoder solving, spanning the interval (-radius, radius) in each dimension, or a distribution from which to choose evaluation points. gain : Distribution or (n_neurons,) array_like or None The gains associated with each neuron in the ensemble. intercepts : Distribution or (n_neurons) array_like or None The point along each neuron's encoder where its activity is zero. If ``e`` is the neuron's encoder, then the activity will be zero when ``dot(x, e) <= c``, where ``c`` is the given intercept. label : str or None A name for the ensemble. Used for debugging and visualization. max_rates : Distribution or (n_neurons,) array_like or None The activity of each neuron when ``dot(x, e) = 1``, where ``e`` is the neuron's encoder. n_eval_points : int or None The number of evaluation points to be drawn from the ``eval_points`` distribution. If None, then a heuristic is used to determine the number of evaluation points. n_neurons : int or None The number of neurons. neuron_type : NeuronType The model that simulates all neurons in the ensemble (see ``nengo.neurons``). noise : Process or None Random noise injected directly into each neuron in the ensemble as current. A sample is drawn for each individual neuron on every simulation step. radius : int The representational radius of the ensemble. seed : int or None The seed used for random number generation. """ probeable = ('decoded_output', 'input', 'scaled_encoders') n_neurons = IntParam('n_neurons', low=1) dimensions = IntParam('dimensions', low=1) radius = NumberParam('radius', default=1.0, low=1e-10) encoders = DistOrArrayParam('encoders', default=UniformHypersphere(surface=True), sample_shape=('n_neurons', 'dimensions')) intercepts = DistOrArrayParam('intercepts', default=Uniform(-1.0, 1.0), optional=True, sample_shape=('n_neurons', )) max_rates = DistOrArrayParam('max_rates', default=Uniform(200, 400), optional=True, sample_shape=('n_neurons', )) eval_points = DistOrArrayParam('eval_points', default=UniformHypersphere(), sample_shape=('*', 'dimensions')) n_eval_points = IntParam('n_eval_points', default=None, optional=True) neuron_type = NeuronTypeParam('neuron_type', default=LIF()) gain = DistOrArrayParam('gain', default=None, optional=True, sample_shape=('n_neurons', )) bias = DistOrArrayParam('bias', default=None, optional=True, sample_shape=('n_neurons', )) noise = ProcessParam('noise', default=None, optional=True) normalize_encoders = BoolParam('normalize_encoders', default=True, optional=True) def __init__(self, n_neurons, dimensions, radius=Default, encoders=Default, intercepts=Default, max_rates=Default, eval_points=Default, n_eval_points=Default, neuron_type=Default, gain=Default, bias=Default, noise=Default, normalize_encoders=Default, label=Default, seed=Default): super(Ensemble, self).__init__(label=label, seed=seed) self.n_neurons = n_neurons self.dimensions = dimensions self.radius = radius self.encoders = encoders self.intercepts = intercepts self.max_rates = max_rates self.n_eval_points = n_eval_points self.eval_points = eval_points self.bias = bias self.gain = gain self.neuron_type = neuron_type self.noise = noise self.normalize_encoders = normalize_encoders def __getitem__(self, key): return ObjView(self, key) def __len__(self): return self.dimensions @property def neurons(self): """A direct interface to the neurons in the ensemble.""" return Neurons(self) @neurons.setter def neurons(self, dummy): raise ReadonlyError(attr="neurons", obj=self) @property def size_in(self): """The dimensionality of the ensemble.""" return self.dimensions @property def size_out(self): """The dimensionality of the ensemble.""" return self.dimensions
class EchoState(Network, Reservoir): """An Echo State Network (ESN) within a Nengo Reservoir. This creates a standard Echo State Network (ENS) as a Nengo network, defaulting to the standard set of assumptions of non-spiking Tanh units and a random recurrent weight matrix [1]_. This is based on the minimalist Python implementation from [2]_. The network takes some arbitrary time-varying vector as input, encodes it randomly, and filters it using nonlinear units and a random recurrent weight matrix normalized by its spectral radius. This class also inherits ``nengolib.networks.Reservoir``, and thus the optimal linear readout is solved for in the same way: the network is simulated on a test signal, and then a solver is used to optimize the decoding connection weights. References: [1] http://www.scholarpedia.org/article/Echo_state_network [2] http://minds.jacobs-university.de/mantas/code """ n_neurons = IntParam('n_neurons', default=None, low=1) dimensions = IntParam('dimensions', default=None, low=1) dt = NumberParam('dt', low=0, low_open=True) recurrent_synapse = SynapseParam('recurrent_synapse') gain = NumberParam('gain', low=0, low_open=True) neuron_type = NeuronTypeParam('neuron_type') def __init__(self, n_neurons, dimensions, recurrent_synapse=0.005, readout_synapse=None, radii=1.0, gain=1.25, rng=None, neuron_type=Tanh(), include_bias=True, ens_seed=None, label=None, seed=None, add_to_container=None, **ens_kwargs): """Initializes the Echo State Network. Parameters ---------- n_neurons : int The number of neurons to use in the reservoir. dimensions : int The dimensionality of the input signal. recurrent_synapse : nengo.synapses.Synapse (Default: ``0.005``) Synapse used to filter the recurrent connection. readout_synapse : nengo.synapses.Synapse (Default: ``None``) Optional synapse to filter all of the outputs before solving for the linear readout. This is included in the connection to the ``output`` Node created within the network. radii : scalar or array_like, optional (Default: ``1``) The radius of each dimension of the input signal, used to normalize the incoming connection weights. gain : scalar, optional (Default: ``1.25``) A scalar gain on the recurrent connection weight matrix. rng : ``numpy.random.RandomState``, optional (Default: ``None``) Random state used to initialize all weights. neuron_type : ``nengo.neurons.NeuronType`` optional \ (Default: ``Tanh()``) Neuron model to use within the reservoir. include_bias : ``bool`` (Default: ``True``) Whether to include a bias current to the neural nonlinearity. This should be ``False`` if the neuron model already has a bias, e.g., ``LIF`` or ``LIFRate``. ens_seed : int, optional (Default: ``None``) Seed passed to the ensemble of neurons. """ Network.__init__(self, label, seed, add_to_container) self.n_neurons = n_neurons self.dimensions = dimensions self.recurrent_synapse = recurrent_synapse self.radii = radii # TODO: make array or scalar parameter? self.gain = gain self.rng = np.random if rng is None else rng self.neuron_type = neuron_type self.include_bias = include_bias self.W_in = (self.rng.rand(self.n_neurons, self.dimensions) - 0.5) / self.radii if self.include_bias: self.W_bias = self.rng.rand(self.n_neurons, 1) - 0.5 else: self.W_bias = np.zeros((self.n_neurons, 1)) self.W = self.rng.rand(self.n_neurons, self.n_neurons) - 0.5 self.W *= self.gain / max(abs(eig(self.W)[0])) with self: self.ensemble = nengo.Ensemble(self.n_neurons, 1, neuron_type=self.neuron_type, seed=ens_seed, **ens_kwargs) self.input = nengo.Node(size_in=self.dimensions) pool = self.ensemble.neurons nengo.Connection(self.input, pool, transform=self.W_in, synapse=None) nengo.Connection( # note the bias will be active during training nengo.Node(output=1, label="bias"), pool, transform=self.W_bias, synapse=None) nengo.Connection(self.ensemble.neurons, pool, transform=self.W, synapse=self.recurrent_synapse) Reservoir.__init__(self, self.input, pool, readout_synapse=readout_synapse, network=self)
class Test: ntp = NeuronTypeParam('ntp', default=None)