def __init__(self, vocab=Default, threshold=1., **kwargs): super(SimilarityThreshold, self).__init__(**kwargs) self.vocab = vocab with self: self.bias = nengo.Node(1) self.dot = spa.Compare(self.vocab) with nengo.presets.ThresholdingEnsembles(0.0): self.threshold = nengo.Ensemble(150, 1) nengo.Connection(self.bias, self.threshold, transform=-threshold) self.output = nengo.Node(size_in=1) self.input_a = self.dot.input_a self.input_b = self.dot.input_b nengo.Connection(self.dot.output, self.threshold) nengo.Connection(self.threshold, self.output, function=lambda x: x > 0., synapse=None) self.inputs = dict(input_a=(self.input_a, self.vocab), input_b=(self.input_b, self.vocab)) self.outputs = dict(default=(self.output, None))
def test_nondefault_routing(Simulator, seed): m = spa.Network(seed=seed) m.config[spa.State].vocab = 3 m.config[spa.State].subdimensions = 3 with m: m.ctrl = spa.State(16, subdimensions=16, label="ctrl") def input_func(t): if t < 0.2: return "A" elif t < 0.4: return "B" else: return "C" m.input = spa.Transcode(input_func, output_vocab=16) m.buff1 = spa.State(label="buff1") m.buff2 = spa.State(label="buff2") m.cmp = spa.Compare(3) node1 = nengo.Node([0, 1, 0]) node2 = nengo.Node([0, 0, 1]) nengo.Connection(node1, m.buff1.input) nengo.Connection(node2, m.buff2.input) m.input >> m.ctrl with spa.ActionSelection(): spa.ifmax( spa.dot(m.ctrl, spa.sym.A), m.buff1 >> m.cmp.input_a, m.buff1 >> m.cmp.input_b, ) spa.ifmax( spa.dot(m.ctrl, spa.sym.B), m.buff1 >> m.cmp.input_a, m.buff2 >> m.cmp.input_b, ) spa.ifmax( spa.dot(m.ctrl, spa.sym.C), m.buff2 >> m.cmp.input_a, m.buff2 >> m.cmp.input_b, ) compare_probe = nengo.Probe(m.cmp.output, synapse=0.03) with Simulator(m) as sim: sim.run(0.6) similarity = sim.data[compare_probe] valueA = np.mean(similarity[150:200], axis=0) # should be [1] valueB = np.mean(similarity[350:400], axis=0) # should be [0] valueC = np.mean(similarity[550:600], axis=0) # should be [1] assert valueA > 0.6 assert valueB < 0.3 assert valueC > 0.6
def test_basic(): with spa.Network(): compare = spa.Compare(vocab=16) vocab_a = spa.Network.get_input_vocab(compare.input_a) vocab_b = spa.Network.get_input_vocab(compare.input_b) # all inputs should share the same vocab assert vocab_a is vocab_b assert vocab_a.dimensions == 16 # output should have no vocab assert spa.Network.get_output_vocab(compare.output) is None
def BoundedIntegrator(d, beta, **kwargs): kwargs.setdefault('label', "context.BoundedIntegrator") net = nengo.Network(**kwargs) with net: net.input = nengo.Node(size_in=d) net.bias_node = nengo.Node(1) net.gate = spa.State(d) net.current = spa.State(d, feedback=1, neurons_per_dimension=300) net.dot = spa.Compare(d) with nengo.presets.ThresholdingEnsembles(0.): net.update_done = nengo.Ensemble(150, 1) net.update_done_th = nengo.Node(size_in=1) nengo.Connection(net.input, net.gate.input, synapse=None) nengo.Connection(net.input, net.dot.input_a) nengo.Connection(net.gate.output, net.current.input, transform=0.3) nengo.Connection(net.current.output, net.dot.input_b) nengo.Connection(net.dot.output, net.update_done) nengo.Connection(net.bias_node, net.update_done, transform=-beta) nengo.Connection(net.update_done, net.update_done_th, synapse=None, function=lambda x: x > 0) inhibit_net(net.update_done_th, net.gate, strength=3.) ctx_square = net.current.state_ensembles.add_output( 'square', lambda x: x * x) with nengo.presets.ThresholdingEnsembles(-0.1): net.length = nengo.Ensemble(150, 1) nengo.Connection(ctx_square, net.length, transform=-np.ones((1, d))) nengo.Connection(net.bias_node, net.length) net.downscale = spa.State(d) nengo.Connection(net.current.output, net.downscale.input) nengo.Connection(net.downscale.output, net.current.input, transform=-0.1) inhibit_net(net.length, net.downscale, strength=3, function=lambda x: 1 if x >= -0.1 else 0) net.output = net.current.output return net
def AlternatingMemoryBuffers(d, beta, **kwargs): kwargs.setdefault('label', "context.AlternatingMemories") with nengo.Network(**kwargs) as net: net.input = nengo.Node(size_in=d) net.new_ctx = nengo.Node(size_in=d) net.current = GatedMemory(d) net.old = GatedMemory(d) net.dot = spa.Compare(d) net.bias = nengo.Node(1.) with nengo.presets.ThresholdingEnsembles(0.): net.update_done = nengo.Ensemble(150, 1) net.update_done_th = nengo.Node(size_in=1) nengo.Connection(net.bias, net.update_done, transform=-1.1) nengo.Connection(net.input, net.new_ctx, transform=beta) nengo.Connection(net.new_ctx, net.current.input, synapse=None) nengo.Connection(net.update_done, net.update_done_th, synapse=None, function=lambda x: x > 0) nengo.Connection(net.new_ctx, net.dot.input_a) nengo.Connection(net.current.output, net.dot.input_b) nengo.Connection(net.dot.output, net.update_done) nengo.Connection(net.update_done_th, net.current.input_store) nengo.Connection(net.current.output, net.old.input) nengo.Connection(net.old.output, net.new_ctx, transform=np.sqrt(1. - (beta)**2)) with nengo.presets.ThresholdingEnsembles(0.): net.invert = nengo.Ensemble(50, 1) nengo.Connection(net.bias, net.invert) nengo.Connection(net.update_done_th, net.invert.neurons, transform=-3 * np.ones((net.invert.n_neurons, 1))) nengo.Connection(net.invert, net.old.input_store) net.output = net.current.output return net
def test_run(Simulator, seed): with spa.Network(seed=seed) as model: model.compare = spa.Compare(vocab=16) model.compare.vocab.populate("A; B") def inputA(t): if 0 <= t < 0.1: return "A" else: return "B" model.input = spa.Transcode(inputA, output_vocab=16) model.input >> model.compare.input_a spa.sym.A >> model.compare.input_b with model: p = nengo.Probe(model.compare.output, synapse=0.03) with Simulator(model) as sim: sim.run(0.2) assert sim.data[p][100] > 0.8 assert sim.data[p][199] < 0.2
def test_basal_ganglia(Simulator, seed, plt): d = 64 with spa.Network(seed=seed) as m: m.vision = spa.State(vocab=d) m.motor = spa.State(vocab=d) m.compare = spa.Compare(vocab=d) def input(t): if t < 0.1: return '0' elif t < 0.2: return 'CAT' elif t < 0.3: return 'DOG*~CAT' elif t < 0.4: return 'PARROT' elif t < 0.5: return 'MOUSE' else: return '0' m.encode = spa.Transcode(input, output_vocab=d) # test all acceptable condition formats with spa.ActionSelection() as actions: spa.ifmax(0.5, spa.sym.A >> m.motor) spa.ifmax(spa.dot(m.vision, spa.sym.CAT), spa.sym.B >> m.motor) spa.ifmax(spa.dot(m.vision * spa.sym.CAT, spa.sym.DOG), spa.sym.C >> m.motor) spa.ifmax(2 * spa.dot(m.vision, spa.sym.CAT * 0.5), spa.sym.D >> m.motor) spa.ifmax( spa.dot(m.vision, spa.sym.CAT) + 0.5 - spa.dot(m.vision, spa.sym.CAT), spa.sym.E >> m.motor) spa.ifmax( spa.dot(m.vision, spa.sym.PARROT) + m.compare, spa.sym.F >> m.motor) spa.ifmax(0.5 * spa.dot(m.vision, spa.sym.MOUSE) + 0.5 * m.compare, spa.sym.G >> m.motor) spa.ifmax((spa.dot(m.vision, spa.sym.MOUSE) - m.compare) * 0.5, spa.sym.H >> m.motor) m.encode >> m.vision spa.sym.SHOOP >> m.compare.input_a spa.sym.SHOOP >> m.compare.input_b bg = actions.bg p = nengo.Probe(bg.input, 'output', synapse=0.03) with Simulator(m) as sim: sim.run(0.5) t = sim.trange() plt.plot(t, sim.data[p]) plt.legend(["A", "B", "C", "D", "E", "F", "G", "H"]) plt.title('Basal Ganglia output') # assert the basal ganglia is prioritizing things correctly # Motor F assert sim.data[p][t == 0.4, 5] > 0.8 # Motor G assert sim.data[p][t == 0.5, 6] > 0.8 # Motor A assert 0.6 > sim.data[p][t == 0.1, 0] > 0.4 # Motor B assert sim.data[p][t == 0.2, 1] > 0.8 # Motor C assert sim.data[p][t == 0.3, 2] > 0.5 # Motor B should be the same as Motor D assert np.allclose(sim.data[p][:, 1], sim.data[p][:, 3], atol=0.2) # Motor A should be the same as Motor E assert np.allclose(sim.data[p][:, 0], sim.data[p][:, 4], atol=0.2)
def __init__(self, vocab=Default, noise=0., min_evidence=0., n_inputs=1, **kwargs): super(NeuralAccumulatorDecisionProcess, self).__init__(**kwargs) self.vocab = vocab n_items, d = self.vocab.vectors.shape with self: assert n_inputs > 0 self.input_list = [nengo.Node(size_in=d) for _ in range(n_inputs)] # Input rectification with nengo.presets.ThresholdingEnsembles(0.): self.inp_thrs = [ nengo.networks.EnsembleArray(50, n_items) for _ in range(n_inputs) ] # Evidence integration with nengo.presets.ThresholdingEnsembles(0.): self.state = nengo.networks.EnsembleArray(50, n_items + 1) for inp, inp_thr in zip(self.input_list, self.inp_thrs): nengo.Connection(inp, inp_thr.input, transform=self.vocab.vectors, synapse=None) nengo.Connection(inp_thr.output, self.state.input[:-1], transform=0.1) self.bias = nengo.Node(1.) nengo.Connection(self.bias, self.state.input[-1], transform=min_evidence) nengo.Connection(self.state.output, self.state.input, synapse=0.1) # Thresholding layer with nengo.presets.ThresholdingEnsembles(0.8): self.threshold = nengo.networks.EnsembleArray(50, n_items + 1) nengo.Connection(self.state.output, self.threshold.input) tr = -2 * (1. - np.eye(n_items + 1)) + 1. * np.eye(n_items + 1) nengo.Connection(self.threshold.add_output( 'heaviside', lambda x: 1 if x > 0.8 else 0.), self.state.input, transform=tr, synapse=0.1) # Buffer for recalled item self.buf = GatedMemory(self.vocab, diff_scale=10.) nengo.Connection(self.threshold.heaviside[:-1], self.buf.diff.input, transform=self.vocab.vectors.T) self.buf_input_store = nengo.Ensemble(25, 1) nengo.Connection(self.buf_input_store, self.buf.input_store) nengo.Connection(nengo.Node(1.), self.buf_input_store) nengo.Connection(self.threshold.heaviside[:-1], self.buf_input_store.neurons, transform=-2. * np.ones( (self.buf_input_store.n_neurons, n_items))) # Inhibition of recalled items self.inhibit = spa.State(self.vocab, feedback=1.) self.inhibit_gate = spa.State(self.vocab) nengo.Connection(self.buf.mem.output, self.inhibit_gate.input) nengo.Connection(self.inhibit_gate.output, self.inhibit.input, synapse=0.1, transform=0.1) self.cmp = spa.Compare(self.vocab, neurons_per_dimension=50) nengo.Connection(self.buf.mem.output, self.cmp.input_a) nengo.Connection(self.inhibit.output, self.cmp.input_b) with nengo.presets.ThresholdingEnsembles(0.): self.inhibit_update_done = nengo.Ensemble(50, 1) nengo.Connection(nengo.Node(-0.5), self.inhibit_update_done) nengo.Connection(self.cmp.output, self.inhibit_update_done) inhibit_net(self.inhibit_update_done, self.inhibit_gate, function=lambda x: x > 0) with nengo.presets.ThresholdingEnsembles(0.1): self.inhib_thr = nengo.networks.EnsembleArray(50, n_items) # nengo.Connection( # self.inhibit.output, self.inhib_thr.input, # transform=self.vocab.vectors) self.out_inhibit_gate = GatedMemory(self.vocab) nengo.Connection(self.inhibit.output, self.out_inhibit_gate.input) nengo.Connection(self.out_inhibit_gate.output, self.inhib_thr.input, transform=self.vocab.vectors) nengo.Connection(self.inhib_thr.output, self.state.input[:-1], transform=-6.) # Start over if recall fails self.failed_recall_int = nengo.Ensemble(50, 1) nengo.Connection(self.failed_recall_int, self.failed_recall_int, synapse=0.1, transform=0.9) nengo.Connection(self.threshold.heaviside[-1], self.failed_recall_int, transform=0.1, synapse=0.1) with nengo.presets.ThresholdingEnsembles(0.): self.failed_recall = nengo.Ensemble(50, 1) nengo.Connection(self.failed_recall_int, self.failed_recall) nengo.Connection(nengo.Node(1.), self.failed_recall, transform=-0.3) self.failed_recall_heaviside = nengo.Node(size_in=1) nengo.Connection(self.failed_recall, self.failed_recall_heaviside, function=lambda x: x > 0.) for e in self.state.ensembles: nengo.Connection(self.failed_recall_heaviside, e.neurons, transform=-50. * np.ones((e.n_neurons, 1)), synapse=0.1) # Noise on input if noise > 0.: self.noise = nengo.Node(nengo.processes.WhiteNoise( dist=nengo.dists.Gaussian(mean=0., std=noise)), size_out=n_items + 1) nengo.Connection(self.noise, self.state.input) self.output = self.buf.output self.inputs = dict(default=(self.input_list[0], self.vocab)) self.outputs = dict(default=(self.output, self.vocab))