"""Replay example's Nengo model: eight lexical entries, 78,400 LIF neurons. Run build_lexicon.py first, then: python association_example.py Nengo 4.1.0 and NengoSPA 2.0.0 were used for the archived recording. The website replays the saved recording; this script runs a fresh simulation. """ from pathlib import Path import nengo import nengo_spa as spa import numpy as np OUT = Path(__file__).resolve().parent def run(): with np.load(OUT / 'built-lexicon.npz') as saved: words = saved['words'].tolist() vectors = {k: saved[k].copy() for k in ['concept', 'lemma', 'phonology']} vocabs = {} for level, vs in vectors.items(): vocabs[level] = spa.Vocabulary(512, strict=True) for word, vector in zip(words, vs): vocabs[level].add(word, vector) ki, ti = words.index('Kanne'), words.index('Tasse') def cue(t): if .15 <= t < .65: return vectors['concept'][ki] if .90 <= t < 1.40: return vectors['concept'][ti] return np.zeros(512) with spa.Network(seed=210, label='Eight-entry lexical association teaching circuit') as net: net.config[nengo.Ensemble].neuron_type = nengo.LIF() states = {level: spa.State(vocab, subdimensions=1, neurons_per_dimension=50, feedback=0, represent_cc_identity=False, label=level) for level, vocab in vocabs.items()} stimulus = nengo.Node(cue) nengo.Connection(stimulus, states['concept'].input, synapse=.01) c2l = spa.WTAAssocMem(threshold=.3, input_vocab=vocabs['concept'], output_vocab=vocabs['lemma'], mapping='by-key', n_neurons=100, function=lambda x: 1., label='Concept to lemma: competitive cleanup') l2p = spa.ThresholdingAssocMem(threshold=.3, input_vocab=vocabs['lemma'], output_vocab=vocabs['phonology'], mapping='by-key', n_neurons=100, label='Lemma to phonological form') for a, b in [(states['concept'].output, c2l.input), (c2l.output, states['lemma'].input), (states['lemma'].output, l2p.input), (l2p.output, states['phonology'].input)]: nengo.Connection(a, b, synapse=.01) probes = {level: nengo.Probe(state.output, synapse=.02) for level, state in states.items()} probes['input'] = nengo.Probe(stimulus) for name, memory in [('c2l', c2l), ('l2p', l2p)]: probes[f'{name}_entries'] = nengo.Probe(memory.selection.output, synapse=.02) for i, ens in enumerate(memory.selection.thresholding.ensembles): probes[f'{name}_spikes_{i}'] = nengo.Probe(ens.neurons) n_neurons = sum(e.n_neurons for e in net.all_ensembles) assert n_neurons == 78400 assert all(isinstance(e.neuron_type, nengo.LIF) for e in net.all_ensembles) print(f'Building {n_neurons} LIF neurons; this may take a while.', flush=True) nengo.rc.set('decoder_cache', 'enabled', 'False') with nengo.Simulator(net, dt=.001, progress_bar=False) as sim: sim.run(1.7) t = sim.trange() rec = {name: sim.data[p].copy() for name, p in probes.items()} similarities = {k: rec[k] @ v.T for k, v in vectors.items()} settled = {} for word, index, a, b in [('Kanne', ki, .40, .60), ('Tasse', ti, 1.15, 1.35)]: mask = (t >= a) & (t < b) settled[word] = {} for level in ['lemma', 'phonology']: mean = similarities[level][mask].mean(axis=0) assert np.argmax(mean) == index and mean[index] > .7, (word, level, mean) settled[word][level] = mean.tolist() for stage in ['c2l', 'l2p']: assert np.count_nonzero(rec[f'{stage}_spikes_{index}'][mask]) > 0 stored = {name: (value > 0 if '_spikes_' in name else value.astype(np.float32)) for name, value in rec.items()} np.savez_compressed(OUT / 'association-circuit-data.npz', time=t, words=np.asarray(words), **stored, **{f'{k}_vectors': v for k, v in vectors.items()}) print('Saved association-circuit-data.npz.') if __name__ == '__main__': run()