nengo_dl.configure_settings(stateful=False) # the input node that will be used to feed in (context, location, goal) inp = nengo.Node(np.zeros((36 * 4 + args.maze_id_dim, ))) if '.pkl' in args.param_file: print("Loading values from pickle file") localization_params = pickle.load(open(args.param_file, 'rb')) localization_inp_params = localization_params[0] localization_ens_params = localization_params[1] localization_out_params = localization_params[2] hidden_ens = nengo.Ensemble( n_neurons=args.hidden_size, # dimensions=36*4 + args.maze_id_dim, dimensions=1, neuron_type=neuron_type, **localization_ens_params) out = nengo.Node(size_in=args.dim) if args.n_layers == 1: conn_in = nengo.Connection(inp, hidden_ens.neurons, synapse=None, **localization_inp_params) conn_out = nengo.Connection( hidden_ens.neurons, out, synapse=None,
intercepts = np.linspace(-radius, radius, n_neurons) encoders = np.tile([[1], [-1]], (n_neurons // 2, 1)) intercepts *= encoders[:, 0] return intercepts, encoders model = nengo.Network(label="NEF summary") with model: input = nengo.Node(lambda t: t * 2 - 1) input_probe = nengo.Probe(input) intercepts, encoders = aligned(8) # Makes evenly spaced intercepts with model: A = nengo.Ensemble(8, dimensions=1, intercepts=intercepts, max_rates=Uniform(80, 100), encoders=encoders) with model: nengo.Connection(input, A) A_spikes = nengo.Probe(A.neurons) model = nengo.Network(label="NEF summary") with model: input = nengo.Node(lambda t: t * 2 - 1) input_probe = nengo.Node(WhiteSignal(1, high=5), size_out=1) intercepts, encoders = aligned(8) # Makes evenly spaced intercepts A = nengo.Ensemble(30, dimensions=1, max_rates=Uniform(80, 100)) nengo.Connection(input, A) A_spikes = nengo.Probe(A.neurons, synapse=0.01)
def go(NPre=100, N=100, t=10, m=Uniform(30, 30), i=Uniform(-0.8, 0.8), neuron_type=LIF(), seed=0, dt=0.001, fPre=None, fEns=None, fS=DoubleExp(2e-2, 2e-1), dPreA=None, dPreB=None, dEns=None, ePreA=None, ePreB=None, eBio=None, stage=None, alpha=1e-6, eMax=1e0, stimA=lambda t: np.sin(t), stimB=lambda t: 0): with nengo.Network(seed=seed) as model: inptA = nengo.Node(stimA) inptB = nengo.Node(stimB) preInptA = nengo.Ensemble(NPre, 1, radius=3, max_rates=m, seed=seed) preInptB = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed) ens = nengo.Ensemble(N, 1, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed) tarEns = nengo.Ensemble(N, 1, max_rates=m, intercepts=i, neuron_type=nengo.LIF(), seed=seed) cpa = nengo.Connection(inptA, preInptA, synapse=None, seed=seed) cpb = nengo.Connection(inptB, preInptB, synapse=None, seed=seed) pInptA = nengo.Probe(inptA, synapse=None) pInptB = nengo.Probe(inptB, synapse=None) pPreInptA = nengo.Probe(preInptA.neurons, synapse=None) pPreInptB = nengo.Probe(preInptB.neurons, synapse=None) pEns = nengo.Probe(ens.neurons, synapse=None) pTarEns = nengo.Probe(tarEns.neurons, synapse=None) if stage == 1: c0b = nengo.Connection(preInptB, tarEns, synapse=fPre, solver=NoSolver(dPreB), seed=seed + 1) c1b = nengo.Connection(preInptB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed + 1) learnEncoders(c1b, tarEns, fS, alpha=alpha, eMax=eMax) if stage == 2: c0a = nengo.Connection(preInptA, tarEns, synapse=fPre, solver=NoSolver(dPreA), seed=seed) c0b = nengo.Connection(preInptB, tarEns, synapse=fPre, solver=NoSolver(dPreB), seed=seed + 1) c1a = nengo.Connection(preInptA, ens, synapse=fPre, solver=NoSolver(dPreA), seed=seed) c1b = nengo.Connection(preInptB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed + 1) learnEncoders(c1a, tarEns, fS, alpha=alpha, eMax=eMax) if stage == 3: c1a = nengo.Connection(preInptA, ens, synapse=fPre, solver=NoSolver(dPreA), seed=seed) c1b = nengo.Connection(preInptB, ens, synapse=fPre, solver=NoSolver(dPreB), seed=seed + 1) if stage == 4: preInptC = nengo.Ensemble(NPre, 1, max_rates=m, seed=seed) ens2 = nengo.Ensemble(N, 1, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed) ens3 = nengo.Ensemble(N, 1, max_rates=m, intercepts=i, neuron_type=neuron_type, seed=seed) nengo.Connection(inptB, preInptC, synapse=fEns, seed=seed) c1a = nengo.Connection(preInptA, ens, synapse=fPre, solver=NoSolver(dPreA), seed=seed) c2b = nengo.Connection(preInptB, ens2, synapse=fPre, solver=NoSolver(dPreB), seed=seed + 1) c3 = nengo.Connection(ens2, ens, synapse=fEns, solver=NoSolver(dEns), seed=seed) c4a = nengo.Connection(preInptA, ens3, synapse=fPre, solver=NoSolver(dPreA), seed=seed) c4b = nengo.Connection(preInptC, ens3, synapse=fPre, solver=NoSolver(dPreB), seed=seed) learnEncoders(c3, ens3, fS, alpha=alpha / 10, eMax=eMax) pTarEns = nengo.Probe(ens3.neurons, synapse=None) if stage == 5: c1a = nengo.Connection(preInptA, ens, synapse=fPre, solver=NoSolver(dPreA), seed=seed) c5 = nengo.Connection(ens, ens, synapse=fEns, solver=NoSolver(dEns), seed=seed) with nengo.Simulator(model, seed=seed, dt=dt, progress_bar=False) as sim: if isinstance(neuron_type, Bio): if stage == 1: setWeights(c1b, dPreB, ePreB) if stage == 2: setWeights(c1a, dPreA, ePreA) setWeights(c1b, dPreB, ePreB) if stage == 3: setWeights(c1a, dPreA, ePreA) setWeights(c1b, dPreB, ePreB) if stage == 4: setWeights(c1a, dPreA, ePreA) setWeights(c2b, dPreB, ePreB) setWeights(c4a, dPreA, ePreA) setWeights(c4b, dPreB, ePreB) setWeights(c3, dEns, eBio) if stage == 5: setWeights(c1a, dPreA, ePreA) setWeights(c5, dEns, eBio) neuron.h.init() sim.run(t, progress_bar=True) reset_neuron(sim, model) else: sim.run(t, progress_bar=True) ePreB = c1b.e if stage == 1 else ePreB ePreA = c1a.e if stage == 2 else ePreA eBio = c3.e if stage == 4 else eBio return dict( times=sim.trange(), inptA=sim.data[pInptA], inptB=sim.data[pInptB], preInptA=sim.data[pPreInptA], preInptB=sim.data[pPreInptB], ens=sim.data[pEns], tarEns=sim.data[pTarEns], ePreA=ePreA, ePreB=ePreB, eBio=eBio, )
for off in np.linspace(0, 2 * np.pi, N + 1)[:N] ]) p_ideal = nengo.Probe(stim) def relu(x): return max(x, 0) def neg_relu(x): return -max(x, 0) ensembles = [] for i in range(16): ensembles.append( nengo.Ensemble( n_neurons=n_neurons, dimensions=1, intercepts=nengo.dists.Uniform(0, 1), encoders=nengo.dists.Choice([[1]]), )) for i in range(N): c = nengo.Node(lambda t, x: x, size_in=1, label='c%d' % i) nengo.Connection(stim[i], c) nengo.Connection(c, ensembles[good[i]]) output_a = nengo.Node(lambda t, x: x, size_in=N) p_a = nengo.Probe(output_a) for i in range(N): c = nengo.Node(lambda t, x: x, size_in=1, label='c%d' % i) nengo.Connection(ensembles[good[i]], c, function=relu) nengo.Connection(c, output_a[i]) for i in range(N):
def test_voja_encoders(Simulator, nl_nodirect, rng, seed, allclose): """Tests that voja changes active encoders to the input.""" n = 200 learned_vector = np.asarray([0.3, -0.4, 0.6]) learned_vector /= np.linalg.norm(learned_vector) n_change = n // 2 # modify first half of the encoders # Set the first half to always fire with random encoders, and the # remainder to never fire due to their encoder's dot product with the input intercepts = np.asarray([-1] * n_change + [0.99] * (n - n_change)) rand_encoders = UniformHypersphere(surface=True).sample( n_change, len(learned_vector), rng=rng) encoders = np.append(rand_encoders, [-learned_vector] * (n - n_change), axis=0) m = nengo.Network(seed=seed) with m: m.config[nengo.Ensemble].neuron_type = nl_nodirect() u = nengo.Node(output=learned_vector) x = nengo.Ensemble( n, dimensions=len(learned_vector), intercepts=intercepts, encoders=encoders, max_rates=nengo.dists.Uniform(300.0, 400.0), radius=2.0, ) # to test encoder scaling conn = nengo.Connection(u, x, synapse=None, learning_rule_type=Voja(learning_rate=1e-1)) p_enc = nengo.Probe(conn.learning_rule, "scaled_encoders") p_enc_ens = nengo.Probe(x, "scaled_encoders") with Simulator(m) as sim: sim.run(1.0) t = sim.trange() tend = t > 0.5 # Voja's rule relies on knowing exactly how the encoders were scaled # during the build process, because it modifies the scaled_encoders signal # proportional to this factor. Therefore, we should check that its # assumption actually holds. encoder_scale = (sim.data[x].gain / x.radius)[:, np.newaxis] assert allclose(sim.data[x].encoders, sim.data[x].scaled_encoders / encoder_scale) # Check that the last half kept the same encoders throughout the simulation assert allclose(sim.data[p_enc][0, n_change:], sim.data[p_enc][:, n_change:]) # and that they are also equal to their originally assigned value assert allclose(sim.data[p_enc][0, n_change:] / encoder_scale[n_change:], -learned_vector) # Check that the first half converged to the input assert allclose( sim.data[p_enc][tend, :n_change] / encoder_scale[:n_change], learned_vector, atol=0.01, ) # Check that encoders probed from ensemble equal encoders probed from Voja assert allclose(sim.data[p_enc], sim.data[p_enc_ens])
value = -100 value = '%d' % value ev3link.link.write(path0 + 'duty_cycle_sp', value) try: p = link.read(path0 + 'position') return float(p) / 180 * np.pi except: return 0 ev3 = nengo.Node(ev3_system, size_in=1, size_out=1) pid = pid.PID(0.5, 1, 0, tau_d=0.001) control = nengo.Node(lambda t, x: pid.step(x[:1], x[1:]), size_in=2) nengo.Connection(ev3, control[:1], synapse=0) nengo.Connection(control, ev3, synapse=None) adapt = nengo.Ensemble(n_neurons=200, dimensions=2, neuron_type=nengo.LIFRate()) nengo.Connection(ev3, adapt[0], synapse=None) nengo.Connection(ev3, adapt[1], synapse=0.01) conn = nengo.Connection(adapt, ev3, synapse=0.001, function=lambda x: 0, learning_rule_type=nengo.PES(learning_rate=1e-4)) nengo.Connection(control, conn.learning_rule, transform=-1, synapse=None) desired = nengo.Node(lambda t: np.sin(t * 2 * np.pi)) nengo.Connection(desired, control[1:], synapse=None)
def subtraction(input1, input2): input2s_comp = tc.twos_comp(input2) subtraction = [] flag = 1 print(input2s_comp) model = nengo.Network(label='Subtraction') with model: input1.reverse() input2s_comp.reverse() input1_nodes = [] input2_nodes = [] len(input1) for i in range(len(input1)): input1_nodes.append(nengo.Node(output=input1[i])) input2_nodes.append(nengo.Node(output=input2s_comp[i])) c0 = nengo.Node(output=0) a_plus_b_ensembles = [] a_plus_b_plus_c_ensembles = [] carry_ensembles = [] sum_ensembles = [] carry_ensembles.append(c0) for i in range(len(input1)): a_plus_b_ensembles.append( nengo.Ensemble(n_neurons=200, dimensions=1, radius=2)) a_plus_b_plus_c_ensembles.append( nengo.Ensemble(n_neurons=400, dimensions=2, radius=2)) sum_ensembles.append( nengo.Ensemble(n_neurons=200, dimensions=1, radius=2)) carry_ensembles.append( nengo.Ensemble(n_neurons=200, dimensions=1, radius=2)) for i in range(len(input1)): nengo.Connection(input1_nodes[i], a_plus_b_ensembles[i]) nengo.Connection(input2_nodes[i], a_plus_b_ensembles[i]) for i in range(len(input1)): nengo.Connection(a_plus_b_ensembles[i], a_plus_b_plus_c_ensembles[i][0]) nengo.Connection(carry_ensembles[i], a_plus_b_plus_c_ensembles[i][1]) nengo.Connection(a_plus_b_plus_c_ensembles[i], sum_ensembles[i], function=calculate_output_bit) nengo.Connection(a_plus_b_plus_c_ensembles[i], carry_ensembles[i + 1], function=calculate_carry_out) sum_probes = [] for i in range(len(input1)): sum_probes.append(nengo.Probe(sum_ensembles[i], synapse=0.01)) carry_out_probe = nengo.Probe(carry_ensembles[len(input1)], synapse=0.01) with nengo.Simulator(model) as sim: # Run the model # Print probe values sim.run(5.0) error = 0 for i in range(len(input1)): j = np.mean(sim.data[sum_probes[i]]) subtraction.append(int(round(j))) # print("Sum Out " + str(i) + " " + str(j) + " " + str(int(round(j)))) if (int(round(j)) == 1): error += 1 - j else: error += j #print("Carry Out: " + str(np.mean(sim.data[carry_out_probe]))) accuracy = 1 - (error / len(input1)) #print("Accuracy: " + str(accuracy)) subtraction.reverse() carry = np.mean(sim.data[carry_out_probe]) print("Carry ", int(round(carry))) print(subtraction) carry = int(round(carry)) if (carry == 0): # flag 0 means input1 is smaller than input2 flag = 0 return subtraction, flag
transform=10 * np.ones((mdC_e.n_neurons, 1)), synapse=None) # map motorClean output to a scalar......................................... def mapdirection_to_scalar(x): # returns values in degrees. if np.dot(vocab.parse('LEFT').v, x) >= 0.2: return 45 elif np.dot(vocab.parse('RIGHT').v, x) >= 0.2: return -45 else: return 0 # ens = nengo.Ensemble(500, dimensions = 64) # nengo.Connection(motorClean.output, ens, synapse=None) input3 = spa.State(vocab, subdimensions=64, represent_identity=False) turn_direction = nengo.Ensemble(n_neurons=200, dimensions=1) motorClean >> input3 nengo.Connection(input3.all_ensembles[0], turn_direction, function=mapdirection_to_scalar) # nengo.Connection( turn_direction, env[65], synapse = None) # feed input to arm.. # connect turn_direction -->> output des_xyz = nengo.Node([1.5, 1.5]) output = nengo.Node(output_fcn, size_in=3, size_out=5) nengo.Connection(des_xyz, output)
return [float(e) for e in pos] def get_vel(t): vel = db.get('vel').decode('utf-8').split(';') return [float(e) for e in vel] dec = np.load('prediction_decoder_2Rows.npz') dec = dec['dec3'] model = nengo.Network() with model: redis_pos = nengo.Node(get_pos, size_out=2) redis_vel = nengo.Node(get_vel, size_out=2) pos = nengo.Ensemble(n_neurons=100, dimensions=2) vel = nengo.Ensemble(n_neurons=100, dimensions=2) nengo.Connection(redis_pos, pos, transform=1 / 240, synapse=None) nengo.Connection(redis_vel, vel, transform=1 / 200, synapse=None) def goalie_prediction(t, x): pass f = nengo.Node(None, size_in=dec.shape[0]) ens = nengo.Ensemble(n_neurons=2000, dimensions=4, neuron_type=nengo.LIFRate(), radius=2, seed=1) conn = nengo.Connection(ens.neurons, f, transform=dec, synapse=None)
import nengo from nengo.utils.distributions import Uniform model = nengo.Network(label='Two Neurons') with model: neurons = nengo.Ensemble( 2, dimensions=1, # Representing a scalar intercepts=Uniform(-.5, -.5), # Set the intercepts at .5 max_rates=Uniform(100, 100), # Set the max firing rate at 100hz encoders=[[1], [-1]]) # One 'on' and one 'off' neuron input = nengo.Node([0]) with model: nengo.Connection(input, neurons, synapse=0.01) nengo.Probe(input) # The original input nengo.Probe(neurons.neurons, 'spikes') # Raw spikes from each neuron nengo.Probe(neurons.neurons)
# OFC possible_stim = np.vstack( (np.linspace(0.1, 0.9, num=7), np.linspace(0.05, 0.6, num=7) * (-1))) all_stim = input_stim(possible_stim, n_trials) # indicate number of trials cur_dic = stim_time(all_stim, len_stim, len_pause, len_fix) # (len_stim, len_pause,len_fix) # mod((length of stim),(number of features plus 1))!=0 cur_dic = collections.OrderedDict(sorted(cur_dic.items())) stim_ofc = nengo.Node(piecewise(cur_dic)) # ENSEMBLES # # AMYG amyg = nengo.Ensemble(N, 1, radius=brad, max_rates=get_maxrates(N, 100, 100), neuron_type=neurontype) # OFC ofc = nengo.Ensemble(N, 2, max_rates=get_maxrates(N, 100, 100), radius=brad, neuron_type=neurontype) # 5-HT ht5_Eminus = nengo.Ensemble(n, 1, encoders=np.ones((n, 1)), intercepts=Uniform(low=0.02, high=1.0),
neuron_type = nengo.RectifiedLinear() elif args.neuron_type == 'lifrate': neuron_type = nengo.LIFRate(amplitude=0.01) else: raise NotImplementedError nengo_dl.configure_settings(stateful=False) # nengo_dl.configure_settings(keep_history=True) nengo_dl.configure_settings(keep_history=False) # the input node that will be used to feed in (context, location, goal) inp = nengo.Node(np.zeros((args.dim*2 + args.maze_id_dim,))) hidden_ens = nengo.Ensemble( n_neurons=args.hidden_size, # dimensions=args.dim*2 + args.maze_id_dim, dimensions=1, neuron_type=neuron_type ) out = nengo.Node(size_in=2) out_p = nengo.Probe(out, label="out_p") net.config[out_p].keep_history = False if args.n_layers == 1: conn_in = nengo.Connection(inp, hidden_ens.neurons, synapse=None) conn_out = nengo.Connection(hidden_ens.neurons, out, synapse=None) elif args.n_layers == 2: hidden_ens_two = nengo.Ensemble(
import nengo model = nengo.Network() with model: # inputs to control the critter food = nengo.Node([0, 0]) # where should it go? scare = nengo.Node([0]) # is it scared? # where is the critter currently going movement = nengo.Ensemble(n_neurons=100, dimensions=2) # a memory to figure out where it is position = nengo.Ensemble(n_neurons=500, dimensions=2, radius=5) nengo.Connection(position, position, synapse=0.2) nengo.Connection(movement, position, transform=0.5) # A system for figuring out if it should follow the food do_food = nengo.Ensemble(n_neurons=300, dimensions=2) nengo.Connection(food, do_food) nengo.Connection(do_food, movement) def gate_food(scare): return -2 if scare > 0.5 else 0 nengo.Connection(scare, do_food.neurons, transform=[[1]]*do_food.n_neurons, function=gate_food)
# --- load the data img_rows, img_cols = 28, 28 #The desired output def one_hot(labels, c=None): assert labels.ndim == 1 n = labels.shape[0] c = len(np.unique(labels)) if c is None else c y = np.zeros((n, c)) y[np.arange(n), labels] = 1 return y #X is the img and y is the label (X_train, y_train), (X_test, y_test) = load_mnist() X_train = 2 * X_train - 1 # normalize to -1 to 1 X_test = 2 * X_test - 1 # normalize to -1 to 1 train_targets = one_hot(y_train, 10) test_targets = one_hot(y_test, 10) model = nengo.Network() with model: stim = nengo.Node([0]) a = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(stim, a)
env = grid.GridNode(world, dt=0.005) '''INPUT COLOR/ COLOR SEQUENCE''' color_seq = nengo.Node([0, 0, 0, 0, 0]) targ_color = nengo.Node(0) '''BASIC MOVEMENT''' movement = nengo.Node(move, size_in=2) #Three sensors for distance to the walls def detect(t): angles = (np.linspace(-0.5, 0.5, 3) + body.dir) % world.directions return [body.detect(d, max_distance=4)[0] for d in angles] stim_radar = nengo.Node(detect) radar = nengo.Ensemble(n_neurons=100, dimensions=4, radius=4) nengo.Connection(stim_radar, radar[0:3]) in_target = nengo.Node(0) #a basic movement function that just avoids walls based def movement_func(x): if x[3]: turn = x[3] else: turn = x[2] - x[0] spd = x[1] - 0.8 return spd, turn #the movement function is only driven by information from the radar nengo.Connection(radar, movement, function=movement_func)
# # Nengo Example: Multiplication # This example will show you how to multiply two values. The model # architecture can be thought of as a combination of the combining demo and # the squaring demo. Essentially, we project both inputs independently into a # 2D space, and then decode a nonlinear transformation of that space (the # product of the first and second vector elements). # Create the model object import nengo model = nengo.Network(label='Multiplication') with model: # Create 4 ensembles of leaky integrate-and-fire neurons A = nengo.Ensemble(100, dimensions=1, radius=10, label="A") B = nengo.Ensemble(100, dimensions=1, radius=10, label="B") # Radius on this ensemble is ~sqrt(10^2+10^2) combined = nengo.Ensemble(224, dimensions=2, radius=15, label="combined") prod = nengo.Ensemble(100, dimensions=1, radius=20, label="product") # These next two lines make all of the encoders in the Combined population # point at the corners of the cube. This improves the quality of the # computation. Note the number of neurons is assumed to be divisible by 4 import numpy as np # Comment out the line below for 'normal' encoders combined.encoders = np.tile([[1, 1], [-1, 1], [1, -1], [-1, -1]], (combined.n_neurons // 4, 1)) with model: # Create a piecewise step function for input
coeffs.append(c1 * c2) def L(x): accumulator = np.zeros(x.shape) argument = np.ones(x.shape) for k in range(n + 1): accumulator += np.multiply(argument, coeffs[k]) argument = np.multiply(argument, x) return 2.0**n * accumulator return L model = nengo.Network(label='Integrator') with model: A = nengo.Ensemble(100, dimensions=1) input = nengo.Node(piecewise({ 0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0 })) tau = 0.1 conn_recurrent = nengo.Connection(A, A, transform=[[1]], synapse=tau) conn_input = nengo.Connection(input, A, transform=[[tau]], synapse=tau) input_probe = nengo.Probe(input) A_probe = nengo.Probe(A, synapse=0.01)
def make_ensemble(): with nengo.Network(): e = nengo.Ensemble(10, 1, radius=2.0) return e
def build_bias(model, bioensemble, biases, method, bias_gain=5e-5): rng = np.random.RandomState(bioensemble.seed) neurons_lif = 100 neurons_bio = bioensemble.n_neurons tau = 0.01 lif = nengo.Ensemble( neuron_type=nengo.LIF(), dimensions=1, n_neurons=neurons_lif, seed=bioensemble.seed, add_to_container=False) model.seeds[lif] = bioensemble.seed # seeds normally set early in builder model.build(lif) # add to the model model.add_op(Copy(Signal(0), model.sig[lif]['in'], inc=True)) # connect input(t)=1 A = get_activities(model.params[lif], # grab tuning curve activities lif, model.params[lif].eval_points) # Desired output function Y -- just repeat "bias" m times Y = np.tile(biases, (A.shape[0], 1)) bias_decoders = nengo.solvers.LstsqL2()(A, Y)[0] # initialize synaptic locations syn_loc = get_synaptic_locations( rng, neurons_lif, neurons_bio, n_syn=1) syn_weights = np.zeros(( neurons_bio, neurons_lif, syn_loc.shape[2])) if method == 'weights': for b in range(syn_weights.shape[0]): syn_weights[b] = rng.uniform(np.max(biases), np.min(biases), size=syn_weights[b].shape) if method == 'weights_fixed': for b in range(syn_weights.shape[0]): syn_weights[b] = bias_gain * biases[b]**5 * np.ones(syn_weights[b].shape) # unit test that synapse and weight arrays are compatible shapes if not syn_loc.shape[:-1] == bias_decoders.T.shape: raise BuildError("Shape mismatch: syn_loc=%s, bias_decoders=%s" % (syn_loc.shape[:-1], bias_decoders)) # add synapses to the bioneurons with weights = bias_decoders neurons = model.params[bioensemble.neurons] for j, bahl in enumerate(neurons): assert isinstance(bahl, Bahl) loc = syn_loc[j] bahl.synapses[lif] = np.empty( (loc.shape[0], loc.shape[1]), dtype=object) for pre in range(loc.shape[0]): for syn in range(loc.shape[1]): # section = bahl.cell.tuft(loc[pre, syn]) section = bahl.cell.apical(loc[pre, syn]) # w_ij = np.dot(decoders[pre], gain * encoder) if method == 'decode': syn_weights[j, pre, syn] = bias_decoders[pre, j] w_ij = syn_weights[j, pre, syn] synapse = ExpSyn(section, w_ij, tau, loc[pre, syn]) bahl.synapses[lif][pre][syn] = synapse neuron.init() model.add_op(TransmitSpikes( lif, bioensemble, None, neurons, model.sig[lif]['out'], states=[model.time]))
def make_probe(): with nengo.Network(): e = nengo.Ensemble(10, 1) p = nengo.Probe(e, synapse=0.01) return p
def OneHotCounter(n, **kwargs): with nengo.Network(**kwargs) as net: with nengo.presets.ThresholdingEnsembles(0.): net.state = nengo.networks.EnsembleArray(40, n) net.inhibit_threshold = nengo.networks.EnsembleArray(40, n) net.advance_threshold = nengo.networks.EnsembleArray(40, n) net.rising_edge_detector = nengo.Ensemble(50, 1) net.rising_edge_gate = nengo.Ensemble(50, 1) net.bias = nengo.Node(1.) net.input_inc = nengo.Node(size_in=1) nengo.Connection(net.input_inc, net.rising_edge_detector, synapse=0.05, transform=-1) nengo.Connection(net.input_inc, net.rising_edge_detector, synapse=0.005, transform=1) nengo.Connection(net.rising_edge_detector, net.rising_edge_gate) nengo.Connection(net.bias, net.state.input, transform=-0.2 * np.ones((n, 1))) nengo.Connection(net.state.add_output('const', lambda x: 1.2 if x > 0. else 0.), net.state.input, synapse=0.1) nengo.Connection(net.bias, net.inhibit_threshold.input, transform=-0.6 * np.ones((n, 1))) nengo.Connection(net.state.const, net.inhibit_threshold.input) nengo.Connection(net.inhibit_threshold.add_output( 'heaviside', lambda x: x > 0.), net.state.input, transform=-2. * np.roll(np.eye(n), 1, axis=1), synapse=0.1) nengo.Connection(net.inhibit_threshold.heaviside, net.state.input, transform=-2. * np.roll(np.eye(n), -2, axis=1), synapse=0.1) nengo.Connection(net.state.output, net.advance_threshold.input, transform=-(np.ones((n, n)) - np.eye(n))) nengo.Connection(net.bias, net.rising_edge_gate, transform=-0.1) nengo.Connection(net.rising_edge_gate, net.advance_threshold.input, transform=0.8 * np.ones((n, 1)), function=lambda x: x > 0) tr = 2. * np.roll(np.eye(n), -1, axis=1) tr[0, -1] = 0. nengo.Connection(net.advance_threshold.output, net.state.input, transform=tr, synapse=0.1) net.input = net.state.input net.output = net.state.const net.output_prev = nengo.Node(size_in=n) nengo.Connection(net.state.const, net.output_prev, transform=np.roll(np.eye(n), 1, axis=1)) return net
def make_connection(): with nengo.Network(): e1 = nengo.Ensemble(10, 1) e2 = nengo.Ensemble(10, 1) c = nengo.Connection(e1, e2, transform=2.0) return c
def _test_pes( Simulator, nl, plt, seed, allclose, pre_neurons=False, post_neurons=False, weight_solver=False, vin=np.array([0.5, -0.5]), vout=None, n=200, function=None, transform=np.array(1.0), rate=1e-3, ): vout = np.array(vin) if vout is None else vout with nengo.Network(seed=seed) as model: model.config[nengo.Ensemble].neuron_type = nl() stim = nengo.Node(output=vin) target = nengo.Node(output=vout) pre = nengo.Ensemble(n, dimensions=stim.size_out) post = nengo.Ensemble(n, dimensions=stim.size_out) error = nengo.Ensemble(n, dimensions=target.size_out) nengo.Connection(stim, pre) postslice = post[:target. size_out] if target.size_out < stim.size_out else post pre = pre.neurons if pre_neurons else pre post = post.neurons if post_neurons else postslice conn = nengo.Connection( pre, post, function=function, transform=transform, learning_rule_type=PES(rate), ) if weight_solver: conn.solver = nengo.solvers.LstsqL2(weights=True) nengo.Connection(target, error, transform=-1) nengo.Connection(postslice, error) nengo.Connection(error, conn.learning_rule) post_p = nengo.Probe(postslice, synapse=0.03) error_p = nengo.Probe(error, synapse=0.03) weights_p = nengo.Probe(conn, "weights", sample_every=0.01) with Simulator(model) as sim: sim.run(0.5) t = sim.trange() weights = sim.data[weights_p] plt.subplot(211) plt.plot(t, sim.data[post_p]) plt.ylabel("Post decoded value") plt.subplot(212) plt.plot(t, sim.data[error_p]) plt.ylabel("Error decoded value") plt.xlabel("Time (s)") tend = t > 0.4 assert allclose(sim.data[post_p][tend], vout, atol=0.05) assert allclose(sim.data[error_p][tend], 0, atol=0.05) assert not allclose(weights[0], weights[-1], atol=1e-5, record_rmse=False)
def make_function_connection(): with nengo.Network(): e1 = nengo.Ensemble(10, 1) e2 = nengo.Ensemble(10, 1) c = nengo.Connection(e1, e2, function=lambda x: x**2) return c
def __init__(self, x, ystar, initial_weights, t0=None, t1=None, n_output=None, o_kind='ensemble', o_encoders=UniformHypersphere(surface=True), o_intercepts=Uniform(-1, 0.9), o_rates=Uniform(100, 120), n_error=None, e_kind='ensemble', e_encoders=UniformHypersphere(surface=True), e_intercepts=Uniform(-1, 0.9), e_rates=Uniform(100, 120), psynapse=None, pdt=None, **kwargs): super(FeedforwardNetwork, self).__init__(**kwargs) din = initial_weights[0].shape[0] dout = initial_weights[-1].shape[1] dhids = [w.shape[1] for w in initial_weights[:-1]] self.n_output = n_output self.n_error = n_error self.e_kind = e_kind self.e_encoders = e_encoders self.e_intercepts = e_intercepts self.e_rates = e_rates self.pargs = dict(synapse=psynapse, sample_every=pdt) with self: # hidden layers self.layers = [ nengo.Ensemble(dhid, dhid, gain=Choice([1.]), bias=Choice([1.]), label='layer%d' % i) for i, dhid in enumerate(dhids)] # output layer if n_output is None: self.output = EAIO( nengo.Node, size_in=dout, label='output') elif o_kind == 'ensemble': self.output = EAIO( nengo.Ensemble, n_output*dout, dout, label='output', encoders=o_encoders, intercepts=o_intercepts, max_rates=o_rates) elif o_kind == 'array': self.output = nengo.networks.EnsembleArray( n_output, dout, label='output', encoders=o_encoders, intercepts=o_intercepts, max_rates=o_rates) self.yp = nengo.Probe(self.output.output, **self.pargs) # error layer tmask = ((lambda t: (t > t0) & (t < t1)) if t0 is not None and t1 is not None else \ (lambda t: (t > t0)) if t0 is not None else \ (lambda t: (t < t1)) if t1 is not None else \ (lambda t: (t >= 0))) nmask = lambda t: 20 * (tmask(t) - 1) if n_error is None: ferror = lambda t, x: tmask(t) * x self.error = EAIO( nengo.Node, ferror, size_in=dout, label='error') elif e_kind == 'ensemble': self.error = EAIO( nengo.Ensemble, n_error*dout, dout, label='error', encoders=e_encoders, intercepts=e_intercepts, max_rates=e_rates) error_switch = nengo.Node(nmask) nengo.Connection(error_switch, self.error.neuron_input, transform=np.ones((n_error*dout, 1)), synapse=None) elif e_kind == 'array': self.error = nengo.networks.EnsembleArray( n_error, dout, label='error', encoders=e_encoders, intercepts=e_intercepts, max_rates=e_rates) self.error.add_neuron_input() error_switch = nengo.Node(nmask) nengo.Connection(error_switch, self.error.neuron_input, transform=np.ones((n_error*dout, 1)), synapse=None) else: raise ValueError(e_kind) self.ep = nengo.Probe(self.error.output, **self.pargs) # connections nengo.Connection(ystar, self.error.input, transform=-1) with self: nengo.Connection(self.output.output, self.error.input) self.conns = [] self.conns.append(nengo.Connection( x.neurons, self.layers[0].neurons, transform=initial_weights[0].T)) with self: layers = self.layers # layers = self.layers + [self.output] for layer0, layer1, w in zip(layers, layers[1:], initial_weights[1:]): self.conns.append(nengo.Connection( layer0.neurons, layer1.neurons, transform=w.T)) self.conns.append(nengo.Connection( self.layers[-1].neurons, self.output.input, transform=initial_weights[-1].T))
# where r is the radius of the circle and s is the speed # (in radians per second). # # As discussed in the previous tutorial, we can convert this into a Nengo # model. In this case there is no input connection, so all we have to do # is multiply by the synapse and add the original value. # # Here we introduce a new kind of plot. The XY-value plot shows the same # information as the normal Value plot, but plots the two dimensions together # rather than using time to be the x-axis. This can be convenient for # representing multidimensional data. # # Try adjusting the r value to 0.5. Try 1.5. What about 0? # Try adjusting the speed s. What happens when it is very slow (0.5)? 0.1? import nengo model = nengo.Network() with model: x = nengo.Ensemble(n_neurons=200, dimensions=2) synapse = 0.1 def oscillator(x): r = 1 s = 6 return [synapse * (-x[1] * s + x[0] * (r - x[0]**2 - x[1]**2)) + x[0], synapse * (x[0] * s + x[1] * (r - x[0]**2 - x[1]**2)) + x[1]] nengo.Connection(x, x, synapse=synapse, function=oscillator)
def model(self, p): vocab = spa.Vocabulary(p.D, randomize=False) vocab.parse('S0+SA+SB+L+R') class Environment(object): def __init__(self, vocab, seed): self.vocab = vocab self.state = 'S0' self.consider_action = 'L' self.q = np.zeros( (3, 2)) + np.inf # we don't actually need Q(S0)!! # so maybe it could be removed? self.most_recent_action = 'L' self.values = np.zeros(2) self.value_wait_times = [p.T_interval / 2, p.T_interval] self.intervals_count = 0 self.rng = np.random.RandomState( seed=seed) # random number generator self.upper_boundary = 0.75 self.lower_boundary = 0.25 self.reward_prob = self.rng.uniform(self.lower_boundary, self.upper_boundary, size=(2, 2)) self.history = [ ] # history of actions chosen in state 0 and state seen as a result self.rewards = [ ] # history of rewards received in terminal states # environment node function # passes appropriate information around at correct time intervals def node_function(self, t, value): if t >= self.value_wait_times[0]: self.values[0] = value self.value_wait_times[0] = (self.intervals_count + 1.5) * p.T_interval self.consider_action = 'R' if t >= self.value_wait_times[1]: self.values[1] = value self.value_wait_times[1] = (self.intervals_count + 2) * p.T_interval self.choose_action() self.intervals_count += 1 self.consider_action = 'L' s = self.vocab.parse(self.state).v # replace infinities with 0 q = np.max(np.where(self.q == np.inf, 0, self.q), axis=1) a = self.vocab.parse(self.consider_action).v return np.hstack([s, a, q]) def choose_action(self): if self.state == 'S0': chosen = self.softmax(self.values) if chosen == 0: if self.rng.rand() < 0.7: self.state = 'SA' else: self.state = 'SB' else: if self.rng.rand() < 0.7: self.state = 'SB' else: self.state = 'SA' self.history.append((chosen, self.state)) else: q_index = 1 if self.state == 'SA' else 2 chosen = self.softmax(self.q[q_index]) pp = self.reward_prob[0 if self.state == 'SA' else 1, chosen] reward = self.rng.rand() < pp self.random_walk() q = self.q[q_index, chosen] if q == np.inf: # check for first setting of value q = reward else: q = q + p.alpha * (reward - q) self.q[q_index, chosen] = q self.state = 'S0' self.rewards.append(reward) # for action selection def softmax(self, values): return np.argmax(values + self.rng.normal(size=values.shape) * p.choice_noise) #return np.argmax(values + np.random.normal(size=values.shape)*p.choice_noise) # change reward_prob using random walk # all reward probabilities should change at each step def random_walk(self): new_noise = self.rng.normal( size=self.reward_prob.shape ) * 0.025 # magic number SD defined in Daw et al. 2011 for index, pp in np.ndenumerate(self.reward_prob): new_prob = pp + new_noise[index] if new_prob > self.upper_boundary or new_prob < self.lower_boundary: self.reward_prob[index] = pp - new_noise[index] else: self.reward_prob[index] = new_prob env = Environment(vocab, seed=p.env_seed) model = nengo.Network() with model: cfg = nengo.Config(nengo.Ensemble, nengo.Connection) if p.direct: cfg[nengo.Ensemble].neuron_type = nengo.Direct() cfg[nengo.Connection].synapse = None elif p.rate: cfg[nengo.Ensemble].neuron_type = nengo.LIFRate() cfg[nengo. Connection].synapse = None #nengo.synapses.Lowpass(tau=0.0) with cfg: env_node = nengo.Node(env.node_function, size_in=1) # for plotting state_plot = nengo.Node(size_in=3) nengo.Connection(env_node[:p.D - 2], state_plot) #action_plot = nengo.Node(size_in=2) #nengo.Connection(env_node[p.D+3:p.D*2], action_plot) # actually doing stuff state_and_action = nengo.Ensemble(n_neurons=p.N_state_action, dimensions=p.D * 2) nengo.Connection(env_node[:p.D * 2], state_and_action) prod = nengo.networks.Product(n_neurons=p.N_product, dimensions=p.D) transform = np.array([ vocab.parse('S0').v, vocab.parse('SA').v, vocab.parse('SB').v, ]) nengo.Connection(env_node[-3:], prod.A, transform=transform.T) def ideal_transition(x): sim_s = np.dot(x[:p.D], vocab.vectors) index_s = np.argmax(sim_s) s = vocab.keys[index_s] sim_a = np.dot(x[p.D:], vocab.vectors) index_a = np.argmax(sim_a) a = vocab.keys[index_a] threshold = 0.1 if sim_s[index_s] < threshold: return np.zeros(p.D) if sim_a[index_a] < threshold: return np.zeros(p.D) if s == 'S0': if a == 'L': pp = [0, 0.7, 0.3] elif a == 'R': pp = [0, 0.3, 0.7] else: pp = [0, 0, 0] elif s == 'SA' or s == 'SB': pp = [1, 0, 0] else: pp = [0, 0, 0] return np.dot(transform.T, pp) nengo.Connection(state_and_action, prod.B, function=ideal_transition) if p.rate or p.direct: nengo.Connection(prod.output, env_node, transform=np.ones((1, p.D)), synapse=0) else: nengo.Connection(prod.output, env_node, transform=np.ones((1, p.D)), synapse=p.syn) # for plotting value_plot = nengo.Node(size_in=1) nengo.Connection(prod.output, value_plot, transform=np.ones((1, p.D))) predicted_state = nengo.Node(size_in=p.D) nengo.Connection(prod.B, predicted_state) value_plot = nengo.Node(size_in=1) nengo.Connection(prod.output, value_plot, transform=np.ones((1, p.D))) q_prod = nengo.Node(size_in=p.D) nengo.Connection(prod.A, q_prod) self.env = env self.locals = locals() return model
def make_middle_layer(self, n_features, n_parallel, n_local, kernel_stride, kernel_size, padding='valid', use_neurons=True, init=nengo.dists.Uniform(-1, 1)): with self.net: prev_layer = self.layers[-1] prev_output_shape = self.output_shapes[-1] layer = [] for prev_row in prev_layer: row = [] for prev_col in prev_row: col = [] this_index = 0 index = 0 for k in range(n_parallel): prev_index = 0 if isinstance(init, nengo.dists.Distribution): this_inits = [init] * n_local else: this_inits = [] prev_size = init.shape[2] // n_local for i in range(n_local): this_init = init[:, :, prev_index:prev_index + prev_size, this_index:this_index + n_features] prev_index = (prev_index + prev_size) this_inits.append(this_init) this_index = (this_index + n_features) conv = nengo.Convolution(n_features, prev_output_shape, channels_last=False, kernel_size=kernel_size, padding=padding, strides=kernel_stride, init=this_inits[0]) if use_neurons: ens = nengo.Ensemble(conv.output_shape.size, dimensions=1, label='%s' % conv.output_shape) ens_neurons = ens.neurons else: ens = nengo.Node(None, size_in=conv.output_shape.size, label='%s' % conv.output_shape) ens_neurons = ens for kk in range(n_local): prev_k = prev_col[index % len(prev_col)] conv = nengo.Convolution(n_features, prev_output_shape, channels_last=False, kernel_size=kernel_size, padding=padding, strides=kernel_stride, init=this_inits[kk]) nengo.Connection(prev_k, ens_neurons, transform=conv) index += 1 col.append(ens_neurons) row.append(col) layer.append(row) self.layers.append(layer) self.output_shapes.append(conv.output_shape)
def test_missing_attribute(): with nengo.Network(): a = nengo.Ensemble(10, 1) with warns(SyntaxWarning): a.dne = 9
def evaluate_mnist_multiple_tio2_var_amp_th(args): ############################# # load the data ############################# input_nbr = args.input_nbr input_nbr = args.input_nbr probe_sample_rate = ( input_nbr / 10 ) / 1000 #Probe sample rate. Proportional to input_nbr to scale down sampling rate of simulations x = args.digit np.random.seed(args.seed) random.seed(args.seed) data = np.load('mnist_norm.npz', allow_pickle=True) image_train_filtered = data['image_train_filtered'] / 255 label_train_filtered = data['label_train_filtered'] image_test_filtered = data['image_test_filtered'] / 255 label_test_filtered = data['label_test_filtered'] image_train_filtered = np.tile(image_train_filtered, (args.iterations, 1, 1)) label_train_filtered = np.tile(label_train_filtered, (args.iterations)) #Simulation Parameters #Presentation time presentation_time = args.presentation_time #0.20 #Pause time pause_time = args.pause_time #Iterations iterations = args.iterations #Input layer parameters n_in = args.n_in # g_max = 1/784 #Maximum output contribution amp_neuron = args.amp_neuron n_neurons = args.n_neurons # Layer 1 neurons # inhib_factor = args.inhib_factor #Multiplication factor for lateral inhibition input_neurons_args = { "n_neurons": n_in, "dimensions": 1, "label": "Input layer", "encoders": nengo.dists.Choice([[1]]), # "max_rates":nengo.dists.Uniform(22,22), # "intercepts":nengo.dists.Uniform(0,0), "gain": nengo.dists.Choice([args.gain_in]), "bias": nengo.dists.Choice([args.bias_in]), # "noise":nengo.processes.WhiteNoise(dist=nengo.dists.Gaussian(args.noise_input, (args.noise_input/2)+0.00001), seed=1), "neuron_type": MyLIF_in(tau_rc=args.tau_in, min_voltage=-1.8, amplitude=args.amp_neuron, tau_ref=args.tau_ref_in) # "neuron_type":nengo.neurons.SpikingRectifiedLinear()#SpikingRelu neuron. } #Layer 1 parameters layer_1_neurons_args = { "n_neurons": n_neurons, "dimensions": 1, "label": "Layer 1", "encoders": nengo.dists.Choice([[1]]), "gain": nengo.dists.Choice([args.gain_out]), "bias": nengo.dists.Choice([args.bias_out]), # "intercepts":nengo.dists.Choice([0]), # "max_rates":nengo.dists.Choice([args.rate_out,args.rate_out]), # "noise":nengo.processes.WhiteNoise(dist=nengo.dists.Gaussian(0, 0.5), seed=1), # "neuron_type":nengo.neurons.LIF(tau_rc=args.tau_out, min_voltage=0) # "neuron_type":MyLIF_out(tau_rc=args.tau_out, min_voltage=-1) "neuron_type": STDPLIF(tau_rc=args.tau_out, min_voltage=-1, spiking_threshold=args.thr_out, inhibition_time=args.inhibition_time, tau_ref=args.tau_ref_out, inc_n=args.inc_n, tau_n=args.tau_n) } np.random.seed(args.seed) random.seed(args.seed) # random_matrix = np.random.normal(0.0, 1.0, (n_neurons,n_in)) #between -1 to 1 of shape W # var_amp_matrix_1 = 1 + (random_matrix*args.amp_vth_var) # random_matrix = np.random.normal(0.0, 1.0, (n_neurons,n_in)) #between -1 to 1 of shape W # var_amp_matrix_2 = 1 + (random_matrix*args.amp_vth_var) # random_matrix = np.random.normal(0.0, 1.0, (n_neurons,n_in)) #between -1 to 1 of shape W # var_vthp_matrix = 1 + (random_matrix*args.amp_vth_var) # random_matrix = np.random.normal(0.0, 1.0, (n_neurons,n_in)) #between -1 to 1 of shape W # var_vthn_matrix = 1 + (random_matrix*args.amp_vth_var) #Learning rule parameters learning_args = { "lr": args.lr, "winit_min": 0, "winit_max": 1, "vprog": args.vprog, "vthp": args.vthp, "vthn": args.vthn, "var_amp_th": args.amp_vth_var, "voltage_clip_max": args.voltage_clip_max, "voltage_clip_min": args.voltage_clip_min, "gmax": 0.0008, "gmin": 0.00008, "sample_distance": int((presentation_time + pause_time) * 200 * 10), #Store weight after 10 images } # argument_string = "presentation_time: "+ str(presentation_time)+ "\n pause_time: "+ str(pause_time)+ "\n input_neurons_args: " + str(input_neurons_args)+ " \n layer_1_neuron_args: " + str(layer_1_neurons_args)+"\n Lateral Inhibition parameters: " + str(lateral_inhib_args) + "\n learning parameters: " + str(learning_args)+ "\n g_max: "+ str(g_max) images = image_train_filtered labels = label_train_filtered np.random.seed(args.seed) random.seed(args.seed) model = nengo.Network("My network", seed=args.seed) ############################# # Model construction ############################# with model: picture = nengo.Node( nengo.processes.PresentInput(images, presentation_time=presentation_time)) true_label = nengo.Node( nengo.processes.PresentInput(labels, presentation_time=presentation_time)) # input layer input_layer = nengo.Ensemble(**input_neurons_args) input_conn = nengo.Connection(picture, input_layer.neurons, synapse=None) #first layer layer1 = nengo.Ensemble(**layer_1_neurons_args) #Weights between input layer and layer 1 w = nengo.Node(CustomRule_post_v6_tio2(**learning_args), size_in=n_in, size_out=n_neurons) nengo.Connection(input_layer.neurons, w, synapse=None) nengo.Connection(w, layer1.neurons, synapse=args.synapse_layer_1) weights = w.output.history # with nengo_ocl.Simulator(model) as sim : with nengo.Simulator(model, dt=args.dt, optimize=True) as sim: w.output.set_signal_vmem( sim.signals[sim.model.sig[input_layer.neurons]["voltage"]]) w.output.set_signal_out( sim.signals[sim.model.sig[layer1.neurons]["out"]]) sim.run((presentation_time + pause_time) * labels.shape[0]) # last_weight = weights[-1] last_weight = (0.00008 + (weights[-1] * (0.0008 - 0.00008))) * 200 sim.close() model = nengo.Network("My network", seed=args.seed) with model: picture = nengo.Node( nengo.processes.PresentInput(images, presentation_time=presentation_time)) true_label = nengo.Node( nengo.processes.PresentInput(labels, presentation_time=presentation_time)) # input layer input_layer = nengo.Ensemble(**input_neurons_args) input_conn = nengo.Connection(picture, input_layer.neurons, synapse=None) #first layer layer1 = nengo.Ensemble(**layer_1_neurons_args) nengo.Connection(input_layer.neurons, layer1.neurons, transform=last_weight, synapse=args.synapse_layer_1) #Probes p_true_label = nengo.Probe(true_label) p_layer_1 = nengo.Probe(layer1.neurons) # with nengo_ocl.Simulator(model) as sim : with nengo.Simulator(model, dt=args.dt, optimize=True) as sim: sim.run((presentation_time + pause_time) * labels.shape[0]) t_data = sim.trange() labels = sim.data[p_true_label][:, 0] output_spikes = sim.data[p_layer_1] neuron_class = np.zeros((n_neurons, 1)) n_classes = 10 for j in range(n_neurons): spike_times_neuron_j = t_data[np.where(output_spikes[:, j] > 0)] max_spike_times = 0 for i in range(n_classes): class_presentation_times_i = t_data[np.where(labels == i)] #Normalized number of spikes wrt class presentation time num_spikes = len( np.intersect1d(spike_times_neuron_j, class_presentation_times_i)) / ( len(class_presentation_times_i) + 1) if (num_spikes > max_spike_times): neuron_class[j] = i max_spike_times = num_spikes spikes_layer1_probe_train = sim.data[p_layer_1] input_nbr = 10000 model = nengo.Network(label="My network", ) with model: picture = nengo.Node( nengo.processes.PresentInput(image_test_filtered, presentation_time=presentation_time)) true_label = nengo.Node( nengo.processes.PresentInput(label_test_filtered, presentation_time=presentation_time)) input_layer = nengo.Ensemble(**input_neurons_args) input_conn = nengo.Connection(picture, input_layer.neurons, synapse=None) #first layer layer1 = nengo.Ensemble(**layer_1_neurons_args) nengo.Connection(input_layer.neurons, layer1.neurons, transform=last_weight, synapse=args.synapse_layer_1) p_true_label = nengo.Probe(true_label) p_layer_1 = nengo.Probe(layer1.neurons) step_time = (presentation_time + pause_time) with nengo.Simulator(model, dt=args.dt) as sim: sim.run(step_time * label_test_filtered.shape[0]) accuracy_2 = evaluation_v2( 10, n_neurons, int(((step_time * label_test_filtered.shape[0]) / sim.dt) / input_nbr), spikes_layer1_probe_train, label_train_filtered, sim.data[p_layer_1], label_test_filtered, sim.dt) labels = sim.data[p_true_label][:, 0] output_spikes = sim.data[p_layer_1] n_classes = 10 predicted_labels = [] true_labels = [] correct_classified = 0 wrong_classified = 0 class_spikes = np.ones((10, 1)) for num in range(input_nbr): output_spikes_num = output_spikes[ num * int(presentation_time / args.dt):(num + 1) * int(presentation_time / args.dt), :] # 0.350/0.005 num_spikes = np.sum(output_spikes_num > 0, axis=0) for i in range(n_classes): sum_temp = 0 count_temp = 0 for j in range(n_neurons): if ((neuron_class[j]) == i): sum_temp += num_spikes[j] count_temp += 1 if (count_temp == 0): class_spikes[i] = 0 else: class_spikes[i] = sum_temp # class_spikes[i] = sum_temp/count_temp k = np.argmax(num_spikes) class_pred = np.argmax(class_spikes) predicted_labels.append(class_pred) true_class = labels[(num * int(presentation_time / args.dt))] if (class_pred == true_class): correct_classified += 1 else: wrong_classified += 1 accuracy = correct_classified / (correct_classified + wrong_classified) * 100 print("Accuracy: ", accuracy) sim.close() del sim.data, labels, output_spikes, class_pred, t_data, spikes_layer1_probe_train return accuracy, accuracy_2, weights[-1]