Example #1
0
def test_spa_get():
    D = 16
    model = spa.SPA()
    with model:
        model.buf1 = spa.State(D)
        model.buf2 = spa.State(D)
        model.compare = spa.Compare(D)

    assert model.get_module("buf1") is model.buf1
    assert model.get_module("buf1_default") is model.buf1
    assert model.get_module("buf2") is model.buf2
    assert model.get_module_input("buf1")[0] is model.buf1.input
    assert model.get_module_output("buf1")[0] is model.buf1.output
    assert model.get_module_input("compare_A")[0] is model.compare.inputA
    assert model.get_module_input("compare_B")[0] is model.compare.inputB

    with pytest.raises(SpaModuleError):
        model.get_module("dummy")
    with pytest.raises(SpaModuleError):
        model.get_module_input("dummy")
    with pytest.raises(SpaModuleError):
        model.get_module_output("dummy")
    with pytest.raises(SpaModuleError):
        model.get_module_input("buf1_A")
    with pytest.raises(SpaModuleError):
        model.get_module_input("compare")
Example #2
0
def test_spa_get():
    D = 16
    model = spa.SPA()
    with model:
        model.buf1 = spa.State(D)
        model.buf2 = spa.State(D)
        model.compare = spa.Compare(D)

    assert model.get_module('buf1') is model.buf1
    assert model.get_module('buf1_default') is model.buf1
    assert model.get_module('buf2') is model.buf2
    assert model.get_module_input('buf1')[0] is model.buf1.input
    assert model.get_module_output('buf1')[0] is model.buf1.output
    assert model.get_module_input('compare_A')[0] is model.compare.inputA
    assert model.get_module_input('compare_B')[0] is model.compare.inputB

    with pytest.raises(SpaModuleError):
        model.get_module('dummy')
    with pytest.raises(SpaModuleError):
        model.get_module_input('dummy')
    with pytest.raises(SpaModuleError):
        model.get_module_output('dummy')
    with pytest.raises(SpaModuleError):
        model.get_module_input('buf1_A')
    with pytest.raises(SpaModuleError):
        model.get_module_input('compare')
Example #3
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.word = spa.State(dimensions=p.D)
            model.marker = spa.State(dimensions=p.D)
            model.memory = spa.State(dimensions=p.D, feedback=1)

            model.cortical = spa.Cortical(
                spa.Actions('memory = word * marker', ))

            def word(t):
                index = t / p.time_per_symbol
                if index < p.n_symbols:
                    return 'S%d' % index
                return '0'

            def marker(t):
                index = t / p.time_per_symbol
                if index < p.n_symbols:
                    return 'M%d' % index
                return '0'

            model.input = spa.Input(word=word, marker=marker)

            self.p_memory = nengo.Probe(model.memory.output, synapse=0.03)
            self.vocab = model.get_output_vocab('memory')

        return model
Example #4
0
def test_dimension_exception():
    with pytest.raises(Exception):
        with spa.SPA() as model:
            vocab = spa.Vocabulary(16)
            model.state = spa.State(dimensions=12, vocab=vocab)

    with spa.SPA() as model:
        model.state = spa.State(dimensions=12, subdimensions=3)
Example #5
0
    def model(self, p):
        vocab = spa.Vocabulary(p.D)
        for i in range(p.M):
            vocab.parse('M%d' % i)

        order = np.arange(p.n_tests)
        np.random.shuffle(order)

        model = spa.SPA()
        with model:
            model.cue = spa.State(p.D, vocab=vocab)
            for ens in model.cue.all_ensembles:
                ens.neuron_type = nengo.Direct()

            model.accum = spa.State(p.D, vocab=vocab, feedback=p.feedback)

            model.recall = spa.AssociativeMemory(vocab,
                                                 wta_output=True,
                                                 threshold_output=True)

            model.recalled = spa.State(p.D, vocab=vocab)
            for ens in model.recalled.all_ensembles:
                ens.neuron_type = nengo.Direct()

            nengo.Connection(model.cue.output,
                             model.accum.input,
                             transform=p.accum)
            nengo.Connection(model.recall.output, model.recalled.input)
            nengo.Connection(model.accum.output, model.recall.input)

            model.same = nengo.Ensemble(n_neurons=100,
                                        dimensions=1,
                                        encoders=nengo.dists.Choice([[1]]),
                                        intercepts=nengo.dists.Uniform(0.3, 1))

            model.dot = nengo.networks.Product(n_neurons=200, dimensions=p.D)
            nengo.Connection(model.cue.output, model.dot.A)
            nengo.Connection(model.recalled.output, model.dot.B)
            nengo.Connection(model.dot.output,
                             model.same,
                             transform=[[1] * p.D])

            def stim(t):
                index = int(t / p.T_test)
                index2 = order[index % len(order)]
                if index % 2 == 0:
                    return 'X%d' % (index2 % p.M)
                else:
                    return 'M%d' % (index2 % p.M)

            model.input = spa.Input(cue=stim)

            self.p_same = nengo.Probe(model.same, synapse=0.01)
        return model
Example #6
0
def test_neurons():
    with spa.SPA() as model:
        model.state = spa.State(dimensions=16, neurons_per_dimension=2)

    assert len(model.state.state_ensembles.ensembles) == 1
    assert model.state.state_ensembles.ensembles[0].n_neurons == 16 * 2

    with spa.SPA() as model:
        model.state = spa.State(dimensions=16, subdimensions=1,
                                neurons_per_dimension=2)

    assert len(model.state.state_ensembles.ensembles) == 16
    assert model.state.state_ensembles.ensembles[0].n_neurons == 2
Example #7
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.word = spa.State(dimensions=p.D)
            model.marker = spa.State(dimensions=p.D)
            model.memory = spa.State(dimensions=p.D, feedback=1)

            model.cortical = spa.Cortical(
                spa.Actions('memory = word * marker', ))

            def word(t):
                index = t / p.time_per_symbol
                if index < p.n_symbols:
                    return 'S%d' % index
                return '0'

            def marker(t):
                index = t / p.time_per_symbol
                if index < p.n_symbols:
                    return 'M%d' % index
                return '0'

            model.input = spa.Input(word=word, marker=marker)

            self.p_memory = nengo.Probe(model.memory.output, synapse=0.03)
            self.vocab = model.get_output_vocab('memory')

        split.split_input_nodes(model, max_dim=16)
        self.replaced = split.split_passthrough(model, max_dim=p.split_max_dim)
        if p.pass_ensembles > 0:
            split.pass_ensembles(model, max_dim=p.pass_ensembles)

        if p.backend == 'nengo_spinnaker':
            import nengo_spinnaker
            nengo_spinnaker.add_spinnaker_params(model.config)
            for node in model.all_nodes:
                if node.output is None:
                    if node.size_in > p.pf_max_dim:
                        print 'limiting', node
                        model.config[node].n_cores_per_chip = p.pf_cores
                        model.config[node].n_chips = p.pf_n_chips
            model.config[nengo_spinnaker.Simulator].placer_kwargs = dict(
                effort=0.1)

        split.remove_outputless_passthrough(model)

        if p.fixed_seed:
            for ens in model.all_ensembles:
                ens.seed = 1

        return model
Example #8
0
def test_memory_run(Simulator, seed, plt):
    with spa.SPA(seed=seed) as model:
        model.memory = spa.State(dimensions=32, feedback=1.0,
                                 feedback_synapse=0.01)

        def state_input(t):
            if 0 <= t < 0.05:
                return 'A'
            else:
                return '0'

        model.state_input = spa.Input(memory=state_input)

    memory, vocab = model.get_module_output('memory')

    with model:
        p = nengo.Probe(memory, 'output', synapse=0.03)

    sim = Simulator(model)
    sim.run(0.5)
    t = sim.trange()

    similarity = np.dot(sim.data[p], vocab.vectors.T)
    plt.plot(t, similarity)
    plt.ylabel("Similarity to 'A'")
    plt.xlabel("Time (s)")

    # value should peak above 1.0, then decay down to near 1.0
    assert np.mean(similarity[(t > 0.05) & (t < 0.1)]) > 1.2
    assert np.mean(similarity[(t > 0.2) & (t < 0.3)]) > 0.8
    assert np.mean(similarity[t > 0.49]) > 0.7
Example #9
0
def test_no_feedback_run(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.state = spa.State(dimensions=32, feedback=0.0)

        def state_input(t):
            if 0 <= t < 0.2:
                return 'A'
            elif 0.2 <= t < 0.4:
                return 'B'
            else:
                return '0'
        model.state_input = spa.Input(state=state_input)

    state, vocab = model.get_module_output('state')

    with model:
        p = nengo.Probe(state, 'output', synapse=0.03)

    sim = Simulator(model)
    sim.run(0.5)

    data = np.dot(sim.data[p], vocab.vectors.T)
    assert data[200, 0] > 0.9
    assert data[200, 1] < 0.2
    assert data[400, 0] < 0.2
    assert data[400, 1] > 0.9
    assert data[499, 0] < 0.2
    assert data[499, 1] < 0.2
Example #10
0
    def __init__(
            self, d, vocab=None, label=None, seed=None, add_to_container=None):
        super(FfwdRat, self).__init__(label, seed, add_to_container)

        if vocab is None:
            vocab = d

        with self:
            self.cue1 = nengo.Node(size_in=d)
            self.cue2 = nengo.Node(size_in=d)
            self.cue3 = nengo.Node(size_in=d)
            self.rat_state = spa.State(d)

            nengo.Connection(
                self.cue1, self.rat_state.input, synapse=None,
                transform=1/3.)
            nengo.Connection(
                self.cue2, self.rat_state.input, synapse=None,
                transform=1/3.)
            nengo.Connection(
                self.cue3, self.rat_state.input, synapse=None,
                transform=1/3.)

        self.inputs = dict(
            cue1=(self.cue1, vocab),
            cue2=(self.cue1, vocab),
            cue3=(self.cue1, vocab))
        self.outputs = dict(default=(self.rat_state.output, vocab))
Example #11
0
def test_memory_run_decay(Simulator, plt, seed):
    with spa.SPA(seed=seed) as model:
        model.memory = spa.State(dimensions=32, feedback=(1.0 - 0.01/0.05),
                                 feedback_synapse=0.01)

        def state_input(t):
            if 0 <= t < 0.05:
                return 'A'
            else:
                return '0'

        model.state_input = spa.Input(memory=state_input)

    memory, vocab = model.get_module_output('memory')

    with model:
        p = nengo.Probe(memory, 'output', synapse=0.03)

    sim = Simulator(model)
    sim.run(0.3)
    data = np.dot(sim.data[p], vocab.vectors.T)

    t = sim.trange()
    plt.plot(t, data)

    assert data[t == 0.05, 0] > 1.0
    assert data[t == 0.299, 0] < 0.4
Example #12
0
def test_actions():
    a = Actions(
        "dot(state, A) --> state=B",
        "dot(state, B) --> state=A",
        default="1.0 --> state=C",
    )
    assert a.count == 3

    model = spa.SPA()
    with model:
        model.state = spa.State(16)
    a.process(model)
    assert str(a.actions[0].condition) == "dot(state, A)"
    assert str(a.actions[0].effect) == "state=B"
    assert str(a.actions[1].condition) == "dot(state, B)"
    assert str(a.actions[1].effect) == "state=A"
    assert str(a.actions[2].condition) == "1.0"
    assert str(a.actions[2].effect) == "state=C"

    a.add("dot(state, D) --> state=E")
    a.add(added="dot(state, E) --> state=F")
    a.process(model)
    assert str(a.actions[2].condition) == "dot(state, D)"
    assert str(a.actions[2].effect) == "state=E"
    assert str(a.actions[3].condition) == "dot(state, E)"
    assert str(a.actions[3].effect) == "state=F"
Example #13
0
def test_actions():
    a = Actions(
        'dot(state, A) --> state=B',
        'dot(state, B) --> state=A',
        default='1.0 --> state=C',
    )
    assert a.count == 3

    model = spa.SPA()
    with model:
        model.state = spa.State(16)
    a.process(model)
    assert str(a.actions[0].condition) == 'dot(state, A)'
    assert str(a.actions[0].effect) == 'state=B'
    assert str(a.actions[1].condition) == 'dot(state, B)'
    assert str(a.actions[1].effect) == 'state=A'
    assert str(a.actions[2].condition) == '1.0'
    assert str(a.actions[2].effect) == 'state=C'

    a.add('dot(state, D) --> state=E')
    a.add(added='dot(state, E) --> state=F')
    a.process(model)
    assert str(a.actions[2].condition) == 'dot(state, D)'
    assert str(a.actions[2].effect) == 'state=E'
    assert str(a.actions[3].condition) == 'dot(state, E)'
    assert str(a.actions[3].effect) == 'state=F'
Example #14
0
    def __init__(
            self, assoc, vocab, neurons_per_dimension, label=None, seed=None,
            add_to_container=None):
        super(FfwdConnectionsRat, self).__init__(label, seed, add_to_container)

        d = vocab.dimensions

        assoc = np.copy(assoc)
        np.fill_diagonal(assoc, 0.)
        tr = np.dot(vocab.vectors.T, np.dot(assoc.T, vocab.vectors)) / 3.

        with self:
            self.cue1 = nengo.Node(size_in=d)
            self.cue2 = nengo.Node(size_in=d)
            self.cue3 = nengo.Node(size_in=d)
            self.rat_state = spa.State(
                d, subdimensions=64,
                neurons_per_dimension=neurons_per_dimension, vocab=vocab)

            nengo.Connection(
                self.cue1, self.rat_state.input, synapse=None,
                transform=tr)
            nengo.Connection(
                self.cue2, self.rat_state.input, synapse=None,
                transform=tr)
            nengo.Connection(
                self.cue3, self.rat_state.input, synapse=None,
                transform=tr)

        self.inputs = dict(
            cue1=(self.cue1, vocab),
            cue2=(self.cue1, vocab),
            cue3=(self.cue1, vocab))
        self.outputs = dict(default=(self.rat_state.output, vocab))
Example #15
0
def create_model():
    D = 16

    model = spa.SPA(seed=1)
    with model:
        model.a = spa.State(dimensions=D)
        model.b = spa.State(dimensions=D)
        model.c = spa.State(dimensions=D)
        model.cortical = spa.Cortical(spa.Actions(
            'c = a+b',
            ))

        model.input = spa.Input(
            a='A',
            b=(lambda t: 'C*~A' if (t%0.1 < 0.05) else 'D*~A'),
            )
    return model, list(), dict()
Example #16
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.word = spa.State(dimensions=p.D)
            model.marker = spa.State(dimensions=p.D)
            model.memory = spa.State(dimensions=p.D, feedback=1)
            model.motor = spa.State(dimensions=p.D)
            model.cue = spa.State(dimensions=p.D)

            model.cortical = spa.Cortical(
                spa.Actions(
                    'memory = word * marker',
                    'motor = memory * ~cue',
                ))

            def word(t):
                index = t / p.time_per_symbol
                if index < p.n_symbols:
                    return 'S%d' % index
                return '0'

            def marker(t):
                index = t / p.time_per_symbol
                if index < p.n_symbols:
                    return 'M%d' % index
                return '0'

            def cue(t):
                index = (t - p.time_per_symbol * p.n_symbols) / p.time_per_cue
                if index > 0:
                    index = index % (2 * p.n_symbols)
                    if index < p.n_symbols:
                        return 'S%d' % index
                    else:
                        return 'M%d' % (index - p.n_symbols)
                return '0'

            model.input = spa.Input(word=word, marker=marker, cue=cue)

            self.p_memory = nengo.Probe(model.memory.output, synapse=0.03)
            self.p_motor = nengo.Probe(model.motor.output, synapse=0.03)
            self.vocab = model.get_output_vocab('motor')

        return model
Example #17
0
def test_basic():
    with spa.SPA() as model:
        model.state = spa.State(dimensions=16)

    input = model.get_module_input('state')
    output = model.get_module_output('state')
    assert input[0] is model.state.input
    assert output[0] is model.state.output
    assert input[1] is output[1]
    assert input[1].dimensions == 16
Example #18
0
    def __init__(self,
                 stimulus,
                 vocab,
                 neurons_per_dimension,
                 label=None,
                 seed=None,
                 add_to_container=None):
        super(StimulusModule, self).__init__(label, seed, add_to_container)

        d = vocab.dimensions

        def vocab_parse(fn):
            return lambda x: vocab.parse(fn(x)).v

        with self:
            self.cue1 = spa.State(d,
                                  subdimensions=64,
                                  neurons_per_dimension=neurons_per_dimension,
                                  vocab=vocab)
            self.cue2 = spa.State(d,
                                  subdimensions=64,
                                  neurons_per_dimension=neurons_per_dimension,
                                  vocab=vocab)
            self.cue3 = spa.State(d,
                                  subdimensions=64,
                                  neurons_per_dimension=neurons_per_dimension,
                                  vocab=vocab)

            self.cue1_input = nengo.Node(vocab_parse(
                stimulus.create_cue_fn(0)))
            nengo.Connection(self.cue1_input, self.cue1.input)

            self.cue2_input = nengo.Node(vocab_parse(
                stimulus.create_cue_fn(1)))
            nengo.Connection(self.cue2_input, self.cue2.input)

            self.cue3_input = nengo.Node(vocab_parse(
                stimulus.create_cue_fn(2)))
            nengo.Connection(self.cue3_input, self.cue1.input)

        self.outputs = dict(cue1=(self.cue1.output, vocab),
                            cue2=(self.cue2.output, vocab),
                            cue3=(self.cue3.output, vocab))
Example #19
0
def test_applicable_targets():
    # Only return non-scalar targets
    model = spa.SPA()

    with model:
        model.comp = spa.Compare(4)
        assert SpaPlot.applicable_targets(model.comp) == []

        model.state = spa.State(4)
        res = SpaPlot.applicable_targets(model.state)
        assert res == list(model.state.outputs.keys())
Example #20
0
 def build_cleanup(self, net):
     net.vocab = spa.Vocabulary(dimensions=self.cleanup.dimensions)
     net.vocab.parse(" + ".join(s.label for s in self.syllables))
     net.cleanup = spa.AssociativeMemory(net.vocab,
                                         wta_output=True,
                                         threshold_output=True,
                                         **self.cleanup.kwargs())
     net.memory = spa.State(dimensions=self.cleanup.dimensions,
                            vocab=net.vocab,
                            **self.memory.kwargs())
     nengo.Connection(net.cleanup.output, net.memory.input)
Example #21
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.memory = spa.State(p.D, feedback=1)
            model.cue = spa.State(p.D)

            model.input = spa.Input(memory=lambda t: 'P1*DOG+P2*CAT+P3*RAT'
                                    if t < 0.1 else '0',
                                    cue='P3')

            model.item = spa.State(p.D)

            model.cortical = spa.Cortical(
                spa.Actions(
                    'item = memory*~cue',
                    'memory = %g*item*cue' % p.strength,
                ))

            self.p_memory = nengo.Probe(model.memory.output, synapse=0.03)
        self.vocab = model.get_output_vocab('memory')
        return model
Example #22
0
 def __init__(self,
              dimensions,
              n_states,
              vocab=None,
              channel_clarity=2,
              fwd_synapse=0.01,
              feedback_synapse=0.1,
              inh_synapse=0.01):
     super(StateArray, self).__init__()
     with self:
         self.input = nengo.Node(None, size_in=dimensions)
         self.mask = nengo.Node(None, size_in=n_states)
         self.reset = nengo.Node(None, size_in=1)
         self.channels = []
         self.states = []
         self.outputs = []
         for i in range(n_states):
             c = spa.State(dimensions, vocab=vocab)
             s = spa.State(dimensions,
                           feedback=1,
                           vocab=vocab,
                           feedback_synapse=feedback_synapse)
             nengo.Connection(self.input, c.input, synapse=None)
             nengo.Connection(c.output, s.input, synapse=fwd_synapse)
             self.channels.append(c)
             self.states.append(s)
             self.outputs.append(s.output)
             for ens in c.all_ensembles:
                 nengo.Connection(self.mask[i],
                                  ens.neurons,
                                  transform=-channel_clarity * np.ones(
                                      (ens.n_neurons, 1)),
                                  synapse=inh_synapse)
             for ens in s.all_ensembles:
                 nengo.Connection(self.reset,
                                  ens.neurons,
                                  transform=-2 * np.ones(
                                      (ens.n_neurons, 1)),
                                  synapse=inh_synapse)
Example #23
0
def test_copy_spa(Simulator):
    with spa.SPA() as original:
        original.state = spa.State(16)
        original.cortex = spa.Cortical(spa.Actions("state = A"))

    cp = original.copy()

    # Check that it still builds.
    with Simulator(cp):
        pass

    # check vocab instance param is set
    for node in cp.all_nodes:
        if node.label in ["input", "output"]:
            assert cp.config[node].vocab is not None
Example #24
0
def test_spa_verification(seed):
    d = 16

    model = spa.SPA(seed=seed)

    # building a normal model that shouldn't raise a warning
    with model:
        model.buf = spa.Buffer(d)
        model.input_node = spa.Input(buf='B')
        # make sure errors aren't fired for non-spa modules
        prod = nengo.networks.Product(10, 2)  # noqa: F841
        model.int_val = 1

        # reassignment is fine for non-modules
        model.int_val = 2

    # reassignment of modules should throw an error
    with pytest.raises(ValueError):
        with model:
            model.buf = spa.State(d, feedback=1)

    with pytest.raises(ValueError):
        model = spa.SPA(seed=seed)
        # build a model that should raise an error because no variable
        with model:
            model.buf = spa.Buffer(d)
            spa.Input(buf='B')

    with pytest.raises(ValueError):
        model = spa.SPA(seed=seed)
        # build a model that should raise an error because no input attribute
        with model:
            model.buf = spa.Buffer(d)
            input_node = spa.Input(buf='B')
            input_node.label = "woop"

    with pytest.raises(SpaModuleError):
        model = spa.SPA(seed=seed)
        # build a model that should raise an error because no buf attribute
        with model:
            buf = spa.Buffer(d)
            model.input_node = spa.Input(buf='B')
            buf.label = "woop"
Example #25
0
    def __init__(self,
                 mapping,
                 D_category=16,
                 D_items=64,
                 threshold=0.4,
                 learning_rate=1e-4):
        model = spa.SPA()
        self.model = model
        self.mapping = mapping
        self.vocab_category = spa.Vocabulary(D_category)
        self.vocab_items = spa.Vocabulary(D_items)
        for k in sorted(
                mapping.keys()):  #allocating verctors for categories name
            self.vocab_category.parse(k)
            for v in mapping[k]:  # allocating vectors to the items
                self.vocab_items.parse(v)

        with model:
            model.category = spa.State(D_category, vocab=self.vocab_category)

            model.items = spa.State(D_items, vocab=self.vocab_items)

            def learned(x):
                #cats = np.dot(self.vocab_category.vectors, x)
                #best_index = np.argmax(cats) #takes the category which has the largest projection of x
                #if cats[best_index] < threshold:
                #   return self.vocab_items.parse('0').v
                #else: #generate the sum vector
                #    k = self.vocab_category.keys[best_index]
                #    total = '+'.join(self.mapping[k])
                #    v = self.vocab_items.parse(total).v
                return self.vocab_items.parse('0').v  # v/(2*np.linalg.norm(v))

            c = nengo.Connection(
                model.category.all_ensembles[0],
                model.items.input,
                function=learned,
                learning_rule_type=nengo.PES(learning_rate=learning_rate))

            model.error = spa.State(D_items, vocab=self.vocab_items)
            nengo.Connection(model.items.output, model.error.input)
            nengo.Connection(model.error.output, c.learning_rule)

            self.stim_category_value = np.zeros(D_category)
            self.stim_category = nengo.Node(self.stim_category)
            nengo.Connection(self.stim_category,
                             model.category.input,
                             synapse=None)

            self.stim_correct_value = np.zeros(D_items)
            self.stim_correct = nengo.Node(self.stim_correct)
            nengo.Connection(self.stim_correct,
                             model.error.input,
                             synapse=None,
                             transform=-1)

            self.stim_stoplearn_value = np.zeros(1)
            self.stim_stoplearn = nengo.Node(self.stim_stoplearn)
            for ens in model.error.all_ensembles:
                nengo.Connection(self.stim_stoplearn,
                                 ens.neurons,
                                 synapse=None,
                                 transform=-10 * np.ones((ens.n_neurons, 1)))

            self.stim_justmemorize_value = np.zeros(1)
            self.stim_justmemorize = nengo.Node(self.stim_justmemorize)
            for ens in model.items.all_ensembles:
                nengo.Connection(self.stim_justmemorize,
                                 ens.neurons,
                                 synapse=None,
                                 transform=-10 * np.ones((ens.n_neurons, 1)))

            self.probe_items = nengo.Probe(model.items.output, synapse=0.01)

        self.sim = nengo.Simulator(self.model)
Example #26
0
def create_model(seed=None,
                 nengo_gui_on=False,
                 store_representations=False,
                 store_spikes_and_resources=False,
                 store_decisions=False,
                 uncued=False,
                 e_cued=None,
                 U_cued=None,
                 compressed_im_cued=None,
                 e_uncued=None,
                 U_uncued=None,
                 compressed_im_uncued=None,
                 memory_item_cued=None,
                 memory_item_uncued=None,
                 probe_cued=None,
                 probe_uncued=None,
                 Ns=None,
                 D=None,
                 Nm=None,
                 Nc=None,
                 Nd=None):

    # global model

    #create vocabulary to show representations in gui
    # if nengo_gui_on:
    #     vocab_angles = spa.Vocabulary(D)
    #     for name in [0, 3, 7, 12, 18, 25, 33, 42]:
    #         #vocab_angles.add('D' + str(name), np.linalg.norm(compressed_im_cued[name+90])) #take mean across phases
    #         v = compressed_im_cued[name+90]
    #         nrm = np.linalg.norm(v)
    #         if nrm > 0:
    #             v /= nrm
    #         vocab_angles.add('D' + str(name), v) #take mean across phases

    #     v = np.dot(imagearr[-1,:]/50, U_cued[:,:D])
    #     nrm = np.linalg.norm(v)
    #     if nrm > 0:
    #         v /= nrm
    #     vocab_angles.add('Impulse', v)

    #model = nengo.Network(seed=seed)
    model = spa.SPA(seed=seed)

    cued_input_partial = partial(input_func_cued,
                                 memory_item_cued=memory_item_cued,
                                 probe_cued=probe_cued)
    uncued_input_partial = partial(input_func_uncued,
                                   memory_item_uncued=memory_item_uncued,
                                   probe_uncued=probe_uncued)
    react_partial = partial(reactivate_func, Nm=Nm)

    with model:  # Again some weird global stuff happening..
        # So this defines the architecture of the model. We don't necessarily need to change this
        # This is with only a single memory cell, the uncued version has two memory ensembles??
        # TODO: Implement uncued
        #input nodes
        inputNode_cued = nengo.Node(cued_input_partial, label='input_cued')
        reactivate = nengo.Node(
            react_partial, label='reactivate'
        )  # Input here at CUE (CUE is not actually shown)
        # The two ensembles above have their functions directly specified
        # the reactivate ensemble gives a pulse to any ensembles it's connected to, and the input node receives images at specific times

        #sensory ensemble
        # Nengo ensemble creates a seemingly interconnected bunch ('ensemble') of Ns neurons
        # Each neuron is a single vector (inner) product, similar to ANNs.
        # e_cued here is the 'encoder' for the neurons (if seen as matrix, otherwise it's the collection of encoding vectors)
        # which follows from the trained? gabor filters.
        sensory_cued = nengo.Ensemble(Ns,
                                      D,
                                      encoders=e_cued,
                                      intercepts=Uniform(0.01, .1),
                                      radius=1,
                                      label='sensory_cued')
        # How does the encoder function work here? Does it use the e_cued matrix to convert the images into
        # SVD reduced versions? But how do the gabor filters work

        nengo.Connection(inputNode_cued,
                         sensory_cued,
                         transform=U_cued[:, :D].T)
        # Connection means a connection between two nodes/ensembles. Can include a transformation
        # Not sure what the SVD outcomes actually mean TODO

        #memory ensemble
        memory_cued = nengo.Ensemble(Nm,
                                     D,
                                     neuron_type=stpLIF(),
                                     intercepts=Uniform(0.01, .1),
                                     radius=1,
                                     label='memory_cued')
        nengo.Connection(
            reactivate,
            memory_cued.neurons)  #potential reactivation --> This is the CUE
        nengo.Connection(sensory_cued, memory_cued, transform=.1)  #.1)

        #recurrent STSP connection
        nengo.Connection(memory_cued,
                         memory_cued,
                         transform=1,
                         learning_rule_type=STP(),
                         solver=nengo.solvers.LstsqL2(weights=True))

        #comparison represents sin, cosine of theta of both sensory and memory ensemble
        comparison_cued = nengo.Ensemble(Nc,
                                         dimensions=4,
                                         radius=math.sqrt(2),
                                         intercepts=Uniform(.01, 1),
                                         label='comparison_cued')
        nengo.Connection(sensory_cued,
                         comparison_cued[:2],
                         eval_points=compressed_im_cued[0:-1],
                         function=sincos.T)
        nengo.Connection(memory_cued,
                         comparison_cued[2:],
                         eval_points=compressed_im_cued[0:-1],
                         function=sincos.T)

        #decision represents the difference in theta decoded from the sensory and memory ensembles
        decision_cued = nengo.Ensemble(n_neurons=Nd,
                                       dimensions=1,
                                       radius=45,
                                       label='decision_cued')
        nengo.Connection(comparison_cued,
                         decision_cued,
                         eval_points=ep,
                         scale_eval_points=False,
                         function=arctan_func)

        #same for uncued
        if uncued:
            inputNode_uncued = nengo.Node(uncued_input_partial,
                                          label='input_uncued')

            sensory_uncued = nengo.Ensemble(Ns,
                                            D,
                                            encoders=e_uncued,
                                            intercepts=Uniform(0.01, .1),
                                            radius=1,
                                            label='sensory_uncued')
            nengo.Connection(inputNode_uncued,
                             sensory_uncued,
                             transform=U_uncued[:, :D].T)

            memory_uncued = nengo.Ensemble(Nm,
                                           D,
                                           neuron_type=stpLIF(),
                                           intercepts=Uniform(0.01, .1),
                                           radius=1,
                                           label='memory_uncued')
            nengo.Connection(sensory_uncued, memory_uncued, transform=.1)

            nengo.Connection(memory_uncued,
                             memory_uncued,
                             transform=1,
                             learning_rule_type=STP(),
                             solver=nengo.solvers.LstsqL2(weights=True))

            comparison_uncued = nengo.Ensemble(Nd,
                                               dimensions=4,
                                               radius=math.sqrt(2),
                                               intercepts=Uniform(.01, 1),
                                               label='comparison_uncued')

            nengo.Connection(memory_uncued,
                             comparison_uncued[2:],
                             eval_points=compressed_im_uncued[0:-1],
                             function=sincos.T)
            nengo.Connection(sensory_uncued,
                             comparison_uncued[:2],
                             eval_points=compressed_im_uncued[0:-1],
                             function=sincos.T)

            decision_uncued = nengo.Ensemble(n_neurons=Nd,
                                             dimensions=1,
                                             radius=45,
                                             label='decision_uncued')
            nengo.Connection(comparison_uncued,
                             decision_uncued,
                             eval_points=ep,
                             scale_eval_points=False,
                             function=arctan_func)

        #decode for gui
        if nengo_gui_on:
            model.sensory_decode = spa.State(D,
                                             vocab=vocab_angles,
                                             subdimensions=12,
                                             label='sensory_decode')
            for ens in model.sensory_decode.all_ensembles:
                ens.neuron_type = nengo.Direct()
            nengo.Connection(sensory_cued,
                             model.sensory_decode.input,
                             synapse=None)

            model.memory_decode = spa.State(D,
                                            vocab=vocab_angles,
                                            subdimensions=12,
                                            label='memory_decode')
            for ens in model.memory_decode.all_ensembles:
                ens.neuron_type = nengo.Direct()
            nengo.Connection(memory_cued,
                             model.memory_decode.input,
                             synapse=None)

        #probes
        if not (nengo_gui_on):
            if store_representations:  #sim 1 trials 1-100
                #p_dtheta_cued=nengo.Probe(decision_cued, synapse=0.01)
                model.p_mem_cued = nengo.Probe(memory_cued, synapse=0.01)
                #p_sen_cued=nengo.Probe(sensory_cued, synapse=0.01)

                if uncued:
                    model.p_mem_uncued = nengo.Probe(memory_uncued,
                                                     synapse=0.01)

            if store_spikes_and_resources:  #sim 1 trial 1
                model.p_spikes_mem_cued = nengo.Probe(memory_cued.neurons,
                                                      'spikes')
                model.p_res_cued = nengo.Probe(memory_cued.neurons,
                                               'resources')
                model.p_cal_cued = nengo.Probe(memory_cued.neurons, 'calcium')

                if uncued:
                    model.p_spikes_mem_uncued = nengo.Probe(
                        memory_uncued.neurons, 'spikes')
                    model.p_res_uncued = nengo.Probe(memory_uncued.neurons,
                                                     'resources')
                    model.p_cal_uncued = nengo.Probe(memory_uncued.neurons,
                                                     'calcium')

            if store_decisions:  #sim 2
                model.p_dec_cued = nengo.Probe(decision_cued, synapse=0.01)
    return model
Example #27
0
    # 

    #Is there food?
    def food_check(food):
        if food >0.5:
            return 1
        else:
            return 0

    #nengo.Connection(food_input, food, function=food_check)
    """
    
    
    D = 64
    #step = nengo.Node(0)
    model.food = spa.State(D)
    model.now_state = spa.State(D)
    model.now_action = spa.State(D)
    model.subgoal = spa.State(D)
    def update_step():
        pass
        
    actions = spa.Actions(
        # Start when subgoal is SEARCH_BALL
        'dot(subgoal, SEARCH_BALL) -->now_state=START',
        'dot(now_state, START) --> subgoal=WAIT, now_state=SEARCH1',
        #Search, then turn
        'dot(now_state, SEARCH1) --> now_action=TURN',
        'dot(now_action, TURN) --> now_state=WAIT, now_action=DONE1',
        # Then search again and then move forward
        'dot(now_action, DONE1) --> now_state=SEARCH2',
    t = threading.Thread(target=tcp_client_routine)
    t.setDaemon(True)
    t.start()

    return stopper


tcp_client_routine_stopper = launch_tcp_client_routine()
udp_listener_routine_stopper = launch_udp_listener_routine()

model = spa.SPA()
D = 48

with model:
    model.vision = spa.State(D)

    model.motion = spa.State(D)

    actions = spa.Actions('dot(vision, RIGHT) --> motion=RIGHT',
                          'dot(vision, LEFT) --> motion=LEFT',
                          'dot(vision, NEAR)*0.6 --> motion=STOP',
                          'dot(vision, FAR)*0.6 --> motion=FORWARD',
                          'dot(vision, GREEN)*0.8 --> motion=FORWARD',
                          'dot(vision, RED)*0.4 --> motion=STOP',
                          '0.35 --> motion=FORWARD')

    model.bg = spa.BasalGanglia(actions)
    model.thalamus = spa.Thalamus(model.bg)

    def input_func(t):  # get vision
Example #29
0
with model:

    cfg = nengo.Config(nengo.Ensemble, nengo.Connection)
    if direct:
        cfg[nengo.Ensemble].neuron_type = nengo.Direct()
        cfg[nengo.Connection].synapse = None
    # Model of the external environment
    # Input: action semantic pointer
    # Output: current state semantic pointer
    #model.env = nengo.Node(Environment(vocab=vocab, time_interval=time_interval), size_in=DIM, size_out=DIM)
    model.env = nengo.Node(Agent(vocab=vocab, time_interval=time_interval),
                           size_in=2,
                           size_out=DIM * 3)

    with cfg:
        model.state = spa.State(DIM, vocab=vocab)
        model.action = spa.State(DIM, vocab=vocab)
        model.probability_left = spa.State(DIM, vocab=vocab)
        model.probability_right = spa.State(DIM, vocab=vocab)
        model.combined_probability = nengo.Ensemble(
            n_neurons=DIM * 2 * 50,
            dimensions=DIM * 2,
        )

    if initialized:
        function = correct_mapping
    else:
        function = initial_mapping
    if learning:
        model.state_ensemble = nengo.Ensemble(n_neurons=DIM * 50,
                                              dimensions=DIM)
Example #30
0
# To test the model, try presenting WRITE to the vision State.  The phrase
# should fill with WRITE*VERB.  Now change the vision input to HI.  The phrase
# should now fill with NOUN*HI.  If you get rid of the visual input (by setting
# the value to nothing), it shuold sent HI to the hand.  If you try the same
# thing with SAY it should send the result to speech.

import nengo
import nengo.spa as spa

D = 32  # the dimensionality of the vectors

model = spa.SPA()
with model:
    # perceptual input
    model.vision = spa.State(D)

    # linguistic components
    model.phrase = spa.State(D)
    model.verb = spa.State(D, feedback=1)
    model.noun = spa.State(D, feedback=1)

    # motor components
    model.speech = spa.State(D)
    model.hand = spa.State(D)

    actions = spa.Actions(
        'dot(vision, WRITE+SAY) --> verb=vision',
        'dot(vision, YES+NO+HI+BYE+OK) --> noun=vision',
        'dot(phrase, VERB*WRITE) - 2*dot(vision, WRITE+SAY+YES+NO+HI+BYE+OK)'
        '--> hand=phrase*~NOUN',