Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(nengo_ensemble.encoders, nengo.dists.Distribution):
encoders = nengo_ensemble.encoders.sample(
nengo_ensemble.n_neurons, nengo_ensemble.dimensions,
rng=random_number_generator)
encoders = numpy.asarray(encoders, dtype=numpy.float64)
else:
encoders = nengo_numpy.array(
nengo_ensemble.encoders, min_dims=2, dtype=numpy.float64)
encoders /= nengo_numpy.norm(encoders, axis=1, keepdims=True)
# Get correct sample function (seems dists.get_samples not in nengo
# dists in some versions, so has to be a if / else)
if hasattr(ensemble, 'sample'):
sample_function = ensemble.sample
else:
sample_function = nengo.dists.get_samples
# Get maximum rates and intercepts
max_rates = sample_function(
nengo_ensemble.max_rates, nengo_ensemble.n_neurons,
rng=random_number_generator)
intercepts = sample_function(
nengo_ensemble.intercepts, nengo_ensemble.n_neurons,
rng=random_number_generator)
# Build the neurons
if nengo_ensemble.gain is None and nengo_ensemble.bias is None:
gain, bias = nengo_ensemble.neuron_type.gain_bias(
max_rates, intercepts)
elif (nengo_ensemble.gain is not None and
nengo_ensemble.bias is not None):
gain = sample_function(
## build an ensemble exactly as in the test file simulation
## and find the bises of those neurons
## NOTE: Set the seeds and other params manually below,
## as they could be ambiguous from name of the file
import nengo
Nexc, N, reprRadius, nrngain = 3000, 2, 5, 2
seedR0, seedR2 = 2, 4
gain_bias_set = True
#biaslow, biashigh = 1 - nrngain, 1 + nrngain
biaslow, biashigh = -nrngain, nrngain
print('building model')
mainModel = nengo.Network(label="Single layer network", seed=seedR0)
with mainModel:
ratorOut = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadius,
neuron_type=nengo.neurons.LIF(),
bias=nengo.dists.Uniform(biaslow,biashigh), gain=np.ones(Nexc)*nrngain,
#max_rates=nengo.dists.Uniform(200, 400),
noise=None, seed=seedR2, label='ratorOut' )
sim = nengo.Simulator(mainModel,dt)
biases = sim.data[ratorOut].bias
zerofiringbiases = biases[zeroidxs]
gains = sim.data[ratorOut].gain
zerofiringgains = gains[zeroidxs]
if gain_bias_set: histrange, biasrange = 5, 5
else: histrange, biasrange = 500, 100
fig = plt.figure(facecolor='w')
ax1 = plt.subplot(231)
vals,_,_ = ax1.hist(gains,bins=50,range=(0,histrange),color='k',histtype='step')
ax1.set_xlabel('all gains')
ax2 = plt.subplot(232)
vals,_,_ = ax2.hist(biases,bins=50,range=(-histrange,biasrange),color='k',histtype='step')
seed=seedR1)
processOut = nengo.processes.WhiteNoise(
default_size_out=Nexc, default_dt = dt,
dist=nengo.dists.Gaussian(0 if (dynNoiseMean is None) else dynNoiseMean, dynNoise),
seed=seedR2)
else:
processIn = None
processOut = None
nodeIn = nengo.Node( size_in=N, output = lambda timeval,currval: inpfn(timeval) )
# with zero bias, at reprRadius, if you want 50Hz, gain=1.685, if 100Hz, gain=3.033, if 400Hz, 40.5
#nrngain = 1.5#2#3.033
# input layer from which feedforward weights to ratorOut are computed
ratorIn = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadiusIn,
neuron_type=nengo.neurons.LIF(),
#bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain,
max_rates=nengo.dists.Uniform(200, 400),
noise=processIn, seed=seedR1, label='ratorIn' )
nengo.Connection(nodeIn, ratorIn, synapse=None) # No filtering here as no filtering/delay in the plant/arm
# another layer with learning incorporated
ratorOut = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadius,
neuron_type=nengo.neurons.LIF(),
#bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain,
max_rates=nengo.dists.Uniform(200, 400),
noise=processOut, seed=seedR2, label='ratorOut' )
if trialClamp:
# clamp ratorIn and ratorOut at the end of each trial (Tperiod) for 100ms.
# Error clamped below during end of the trial for 100ms.
clampValsZeros = np.zeros(Nexc)
clampValsNegs = -100.*np.ones(Nexc)
endTrialClamp = nengo.Node(lambda t: clampValsZeros if (t%Tperiod)<(Tperiod-Tclamp) else clampValsNegs)
nengo.Connection(endTrialClamp,ratorIn.neurons,synapse=1e-3)
# no need to clamp error during ramp, as expected signal is accurate during ramp too
# clamp error neurons to zero for the last 2 Tperiods to check generation
# by injecting strong negative input to error neurons
if spikingNeurons:
clampValsZeros = np.zeros(Nerror)
clampValsNegs = -100.*np.ones(Nerror)
rampClamp = lambda t: clampValsZeros if t<(Tmax-Tnolearning) else clampValsNegs
errorClamp = nengo.Node(rampClamp)
nengo.Connection(errorClamp,error.neurons,synapse=1e-3)
# fast synapse for fast-reacting clamp
if errNoise is not None:
errNoiseNode = nengo.Node( nengo.processes.WhiteNoise(
default_size_out=N, default_dt = dt,
dist=nengo.dists.Gaussian(0 if (errNoiseMean is None) else errNoiseMean, errNoise),
seed=seedR1) )
nengo.Connection(errNoiseNode,error)
###
### Add the relevant pre signal to the error ensemble ###
###
if recurrentLearning: # L2 rec learning
if copycatLayer:
# Error = post - desired_output
rateEvolve2error = nengo.Connection(expectOut,error,synapse=tau,transform=-np.eye(N))
# - desired output here (post above)
# tau-filtered expectOut must be compared to tau-filtered ratorOut (post above)
else:
rateEvolve = nengo.Node(rateEvolveFn)
# Error = post - desired_output
rateEvolve2error = nengo.Connection(rateEvolve,error,synapse=tau,transform=-np.eye(N))
def generate_parameters_from_ensemble(
nengo_ensemble, random_number_generator):
""" goes through the nengo ensemble object and extracts the
connection_parameters for the lif neurons
:param nengo_ensemble: the ensemble handed down by nengo
:param random_number_generator: the random number generator
controlling all random in this nengo run
:return: dict of params with names.
"""
eval_points = nengo_builder.ensemble.gen_eval_points(
nengo_ensemble, nengo_ensemble.eval_points,
rng=random_number_generator)
# Get the encoders
if isinstance(nengo_ensemble.encoders, nengo.dists.Distribution):
encoders = nengo_ensemble.encoders.sample(
nengo_ensemble.n_neurons, nengo_ensemble.dimensions,
rng=random_number_generator)
encoders = numpy.asarray(encoders, dtype=numpy.float64)
else:
encoders = nengo_numpy.array(
nengo_ensemble.encoders, min_dims=2, dtype=numpy.float64)
encoders /= nengo_numpy.norm(encoders, axis=1, keepdims=True)
# Get correct sample function (seems dists.get_samples not in nengo
# dists in some versions, so has to be a if / else)
if hasattr(ensemble, 'sample'):
sample_function = ensemble.sample
else:
sample_function = nengo.dists.get_samples
Ad = expm(A*dt)
Bd = np.dot(np.linalg.inv(A), np.dot((Ad - np.eye(2)), B))
# account for discrete lowpass filter
a = np.exp(-dt/tau)
A = 1.0 / (1.0 - a) * (Ad - a * np.eye(2))
B = 1.0 / (1.0 - a) * Bd
if net is None:
net = nengo.Network(label='Point Attractor')
config = nengo.Config(nengo.Connection, nengo.Ensemble)
config[nengo.Connection].synapse = nengo.Lowpass(tau)
with config, net:
net.ydy = nengo.Ensemble(n_neurons=n_neurons, dimensions=2,
# set it up so neurons are tuned to one dimensions only
encoders=nengo.dists.Choice([[1, 0], [-1, 0], [0, 1], [0, -1]]))
# set up Ax part of point attractor
nengo.Connection(net.ydy, net.ydy, transform=A)
# hook up input
net.input = nengo.Node(size_in=1, size_out=1)
# set up Bu part of point attractor
nengo.Connection(net.input, net.ydy, transform=B)
# hook up output
net.output = nengo.Node(size_in=1, size_out=1)
# add in forcing function
nengo.Connection(net.ydy[0], net.output, synapse=None)
return net
processOut = None
nodeIn = nengo.Node( size_in=N, output = lambda timeval,currval: inpfn(timeval) )
# with zero bias, at reprRadius, if you want 50Hz, gain=1.685, if 100Hz, gain=3.033, if 400Hz, 40.5
#nrngain = 1.5#2#3.033
# input layer from which feedforward weights to ratorOut are computed
ratorIn = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadiusIn,
neuron_type=nengo.neurons.LIF(),
#bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain,
max_rates=nengo.dists.Uniform(200, 400),
noise=processIn, seed=seedR1, label='ratorIn' )
nengo.Connection(nodeIn, ratorIn, synapse=None) # No filtering here as no filtering/delay in the plant/arm
# another layer with learning incorporated
ratorOut = nengo.Ensemble( Nexc, dimensions=N, radius=reprRadius,
neuron_type=nengo.neurons.LIF(),
#bias=nengo.dists.Uniform(-nrngain,nrngain), gain=np.ones(Nexc)*nrngain,
max_rates=nengo.dists.Uniform(200, 400),
noise=processOut, seed=seedR2, label='ratorOut' )
if trialClamp:
# clamp ratorIn and ratorOut at the end of each trial (Tperiod) for 100ms.
# Error clamped below during end of the trial for 100ms.
clampValsZeros = np.zeros(Nexc)
clampValsNegs = -100.*np.ones(Nexc)
endTrialClamp = nengo.Node(lambda t: clampValsZeros if (t%Tperiod)<(Tperiod-Tclamp) else clampValsNegs)
nengo.Connection(endTrialClamp,ratorIn.neurons,synapse=1e-3)
nengo.Connection(endTrialClamp,ratorOut.neurons,synapse=1e-3)
# fast synapse for fast-reacting clamp
if plastDecoders:
# don't use the same seeds across the connections,
# else they seem to be all evaluated at the same values of low-dim variables
# causing seed-dependent convergence issues possibly due to similar frozen noise across connections