"""Nengo teaching circuit for ยงยง2.3.6 and 2.3.7. Run build_lexicon.py first, then python memory_example.py. The two website examples are views of one saved 2.05 s simulation. This script runs a fresh simulation: 149,280 LIF neurons, no articulatory model. Nengo 4.1.0 and NengoSPA 2.0.0 were used for the archive. """ from pathlib import Path import hashlib import json import nengo import nengo_spa as spa import numpy as np OUT = Path(__file__).resolve().parent def weight_hash(sim, net): h = hashlib.sha256() count = 0 for conn in net.all_connections: sig = sim.model.sig[conn].get('weights') if sig is not None: a = np.asarray(sim.signals[sig]) h.update(str((a.shape, a.dtype)).encode()) h.update(a.tobytes()) count += 1 return h.hexdigest(), count def run_example(): with np.load(OUT / 'built-lexicon.npz') as saved: words = saved['words'].tolist() vs = {k: saved[k].copy() for k in ['concept', 'lemma', 'phonology']} target = words.index('Tasse') vocabs = {} for level in ['concept', 'lemma']: vocabs[level] = spa.Vocabulary(512, strict=True) for w, v in zip(words, vs[level]): vocabs[level].add(w, v) def pulse(t): return float(.10 <= t < .20 or 1.45 <= t < 1.55) with spa.Network(seed=220, label='Short-term activity and long-term lexical mapping') as net: net.config[nengo.Ensemble].neuron_type = nengo.LIF() cue = nengo.Node(lambda t: pulse(t) * vs['concept'][target]) clear = nengo.Node(lambda t: float(.80 <= t < 1.35)) memory = spa.State(vocabs['concept'], neurons_per_dimension=120, subdimensions=1, represent_cc_identity=False, feedback=1.0, feedback_synapse=.1, seed=221, label='Recurrent concept buffer') control = spa.State(vocabs['concept'], neurons_per_dimension=120, subdimensions=1, represent_cc_identity=False, feedback=0.0, seed=221, label='Matched buffer without feedback') for i, (a, b) in enumerate(zip(memory.state_ensembles.ensembles, control.state_ensembles.ensembles)): a.seed = b.seed = 221000 + i lemma = spa.State(vocabs['lemma'], neurons_per_dimension=50, subdimensions=1, represent_cc_identity=False, feedback=0.0, seed=224, label='Retrieved lemma') c2l = spa.WTAAssocMem(threshold=.3, input_vocab=vocabs['concept'], output_vocab=vocabs['lemma'], mapping='by-key', n_neurons=100, function=lambda x: 1., seed=223) nengo.Connection(cue, memory.input, synapse=.1) nengo.Connection(cue, control.input, synapse=.1) nengo.Connection(memory.output, c2l.input, synapse=.01) nengo.Connection(c2l.output, lemma.input, synapse=.01) for ens in memory.state_ensembles.ensembles: nengo.Connection(clear, ens.neurons, transform=-10 * np.ones((ens.n_neurons, 1)), synapse=.005) probes = {k: nengo.Probe(obj, synapse=.02) for k, obj in { 'memory': memory.output, 'control': control.output, 'lemma': lemma.output, 'entries': c2l.selection.output}.items()} probes['cue'] = nengo.Probe(cue) probes['clear'] = nengo.Probe(clear) for k, st in [('memory', memory), ('control', control), ('lemma', lemma)]: probes[k + '_spikes'] = nengo.Probe(st.state_ensembles.ensembles[0].neurons) for i, ens in enumerate(c2l.selection.thresholding.ensembles): probes[f'entry_spikes_{i}'] = nengo.Probe(ens.neurons) assert all(c.learning_rule_type is None for c in net.all_connections) assert all(isinstance(e.neuron_type, nengo.LIF) for e in net.all_ensembles) n_neurons = sum(e.n_neurons for e in net.all_ensembles) assert n_neurons == 149280 print(f'Building isolated teaching circuit: {n_neurons} LIF neurons.', flush=True) nengo.rc.set('decoder_cache', 'enabled', 'False') with nengo.Simulator(net, dt=.001, progress_bar=False) as sim: for a, b in zip(memory.state_ensembles.ensembles, control.state_ensembles.ensembles): for field in ['encoders', 'gain', 'bias']: assert np.array_equal(getattr(sim.data[a], field), getattr(sim.data[b], field)) before, count = weight_hash(sim, net) sim.run(2.05) after, count2 = weight_hash(sim, net) assert before == after and count == count2 t = sim.trange() rec = {k: sim.data[p].copy() for k, p in probes.items()} # Always retain data before interpreting this local realization. saved = {k: (v > 0 if 'spikes' in k else v.astype(np.float32)) for k, v in rec.items()} np.savez_compressed(OUT / 'memory-circuit-data.npz', time=t, words=np.asarray(words), **saved, **{k+'_vectors': v for k, v in vs.items()}) checks = {'before': before, 'after': after, 'arrays_checked': count, 'all_learning_rules_absent': True, 'matched_buffer_encoders_gains_biases': True} (OUT / 'memory-weight-checks.json').write_text(json.dumps(checks, indent=2)) print('Saved memory-circuit-data.npz and memory-weight-checks.json.') if __name__ == '__main__': run_example()