Exemplo n.º 1
0
def test_spa_verification(Simulator, seed, plt):
    d = 16

    model = spa.SPA(seed=seed)
    # build 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

    with pytest.raises(ValueError):
        model = spa.SPA(seed=seed)
        # build a model that should raise a warning 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 a warning because no attribute
        # for the input
        with model:
            model.buf = spa.Buffer(d)
            input_node = spa.Input(buf='B')
            input_node.label = "woop"

    with pytest.raises(ValueError):
        model = spa.SPA(seed=seed)
        # build a model that should raise a warning because no attribute
        # for the buffer
        with model:
            buf = spa.Buffer(d)
            model.input_node = spa.Input(buf='B')
            buf.label = "woop"
Exemplo n.º 2
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
Exemplo n.º 3
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.vision = spa.Buffer(dimensions=p.D)
            model.phrase = spa.Buffer(dimensions=p.D)
            model.motor = spa.Buffer(dimensions=p.D)
            model.noun = spa.Memory(dimensions=p.D, synapse=0.1)
            model.verb = spa.Memory(dimensions=p.D, synapse=0.1)

            model.bg = spa.BasalGanglia(
                spa.Actions(
                    'dot(vision, WRITE) --> verb=vision',
                    'dot(vision, ONE+TWO+THREE) --> noun=vision',
                    '0.5*(dot(vision, NONE-WRITE-ONE-TWO-THREE) + '
                    'dot(phrase, WRITE*VERB))'
                    '--> motor=phrase*~NOUN',
                ))
            model.thal = spa.Thalamus(model.bg)

            model.cortical = spa.Cortical(
                spa.Actions(
                    'phrase=noun*NOUN',
                    'phrase=verb*VERB',
                ))

            def vision_input(t):
                index = int(t / p.time_per_word) % 3
                return ['WRITE', 'ONE', 'NONE'][index]

            model.input = spa.Input(vision=vision_input)

            self.motor_vocab = model.get_output_vocab('motor')
            self.p_thal = nengo.Probe(model.thal.actions.output, synapse=0.03)
            self.p_motor = nengo.Probe(model.motor.state.output, synapse=0.03)
        return model
Exemplo n.º 4
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.inA = spa.Buffer(p.D,
                                   subdimensions=p.SD,
                                   neurons_per_dimension=p.n_per_d)
            model.inB = spa.Buffer(p.D,
                                   subdimensions=p.SD,
                                   neurons_per_dimension=p.n_per_d)

            model.result = spa.Buffer(p.D,
                                      subdimensions=p.SD,
                                      neurons_per_dimension=p.n_per_d)

            model.cortical = spa.Cortical(spa.Actions('result = inA * inB'),
                                          synapse=p.pstc,
                                          neurons_cconv=p.n_cconv)

            model.input = spa.Input(inA='A', inB='B')

            self.probe = nengo.Probe(model.result.state.output, synapse=p.pstc)

            ideal = nengo.Node(model.get_output_vocab('inA').parse('A*B').v)
            self.probe_ideal = nengo.Probe(ideal, synapse=None)
        return model
Exemplo n.º 5
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
Exemplo n.º 6
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)

    with Simulator(model) as sim:
        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
Exemplo n.º 7
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)

    with Simulator(model) as sim:
        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
Exemplo n.º 8
0
def test_run(Simulator, seed, plt):
    with spa.SPA(seed=seed) as model:
        model.memory = spa.Memory(dimensions=32)

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

        model.input = spa.Input(memory=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
Exemplo n.º 9
0
def test_run_decay(Simulator, plt, seed):
    with spa.SPA(seed=seed) as model:
        model.memory = spa.Memory(dimensions=32, tau=0.05)

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

        model.input = spa.Input(memory=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
Exemplo n.º 10
0
def test_run(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer = spa.Buffer(dimensions=32)

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

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

    with model:
        p = nengo.Probe(buffer, '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
Exemplo n.º 11
0
def test_thalamus(Simulator, plt, seed):
    model = spa.SPA(seed=seed)

    with model:
        model.vision = spa.Buffer(dimensions=16, neurons_per_dimension=80)
        model.vision2 = spa.Buffer(dimensions=16, neurons_per_dimension=80)
        model.motor = spa.Buffer(dimensions=16, neurons_per_dimension=80)
        model.motor2 = spa.Buffer(dimensions=32, neurons_per_dimension=80)

        actions = spa.Actions(
            'dot(vision, A) --> motor=A, motor2=vision*vision2',
            'dot(vision, B) --> motor=vision, motor2=vision*A*~B',
            'dot(vision, ~A) --> motor=~vision, motor2=~vision*vision2')
        model.bg = spa.BasalGanglia(actions)
        model.thalamus = spa.Thalamus(model.bg)

        def input_f(t):
            if t < 0.1:
                return 'A'
            elif t < 0.3:
                return 'B'
            elif t < 0.5:
                return '~A'
            else:
                return '0'

        model.input = spa.Input(vision=input_f, vision2='B*~A')

        input, vocab = model.get_module_input('motor')
        input2, vocab2 = model.get_module_input('motor2')
        p = nengo.Probe(input, 'output', synapse=0.03)
        p2 = nengo.Probe(input2, 'output', synapse=0.03)

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

    t = sim.trange()
    data = vocab.dot(sim.data[p].T)
    data2 = vocab2.dot(sim.data[p2].T)

    plt.subplot(2, 1, 1)
    plt.plot(t, data.T)
    plt.subplot(2, 1, 2)
    plt.plot(t, data2.T)

    # Action 1
    assert data[0, t == 0.1] > 0.8
    assert data[1, t == 0.1] < 0.2
    assert data2[0, t == 0.1] < 0.35
    assert data2[1, t == 0.1] > 0.4
    # Action 2
    assert data[0, t == 0.3] < 0.2
    assert data[1, t == 0.3] > 0.8
    assert data2[0, t == 0.3] > 0.5
    assert data2[1, t == 0.3] < 0.3
    # Action 3
    assert data[0, t == 0.5] > 0.8
    assert data[1, t == 0.5] < 0.2
    assert data2[0, t == 0.5] < 0.5
    assert data2[1, t == 0.5] > 0.4
Exemplo n.º 12
0
def thalamus_net(d=2, n=20, seed=None):
    model = spa.SPA(seed=seed)

    with model:
        model.vision = spa.Buffer(dimensions=d, neurons_per_dimension=n)
        model.vision2 = spa.Buffer(dimensions=d, neurons_per_dimension=n)
        model.motor = spa.Buffer(dimensions=d, neurons_per_dimension=n)
        model.motor2 = spa.Buffer(dimensions=d * 2, neurons_per_dimension=n)

        actions = spa.Actions(
            "dot(vision, A) --> motor=A, motor2=vision*vision2",
            "dot(vision, B) --> motor=vision, motor2=vision*A*~B",
            "dot(vision, ~A) --> motor=~vision, motor2=~vision*vision2",
        )
        model.bg = spa.BasalGanglia(actions)
        model.thalamus = spa.Thalamus(model.bg)

        def input_f(t):
            if t < 0.1:
                return "A"
            elif t < 0.3:
                return "B"
            elif t < 0.5:
                return "~A"
            else:
                return "0"

        model.input = spa.Input(vision=input_f, vision2="B*~A")

    return model
Exemplo n.º 13
0
 def __init__(self):
     self.buffer1 = spa.Buffer(dimensions=16)
     self.buffer2 = spa.Buffer(dimensions=16)
     self.buffer3 = spa.Buffer(dimensions=16)
     self.cortical = spa.Cortical(
         spa.Actions('buffer2=buffer1', 'buffer3=~buffer1'))
     self.input = spa.Input(buffer1='A')
Exemplo n.º 14
0
def test_run(Simulator, seed):
    rng = np.random.RandomState(seed)
    vocab = spa.Vocabulary(16, rng=rng)

    with spa.SPA(seed=seed, vocabs=[vocab]) as model:
        model.bind = spa.Bind(dimensions=16)

        def inputA(t):
            if 0 <= t < 0.1:
                return "A"
            else:
                return "B"

        model.input = spa.Input(bind_A=inputA, bind_B="A")

    bind, vocab = model.get_module_output("bind")

    with model:
        p = nengo.Probe(bind, "output", synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.2)

    error = rmse(vocab.parse("B*A").v, sim.data[p][-1])
    assert error < 0.1

    error = rmse(vocab.parse("A*A").v, sim.data[p][100])
    assert error < 0.1
Exemplo n.º 15
0
def test_routing(Simulator, seed, plt):
    D = 3
    model = spa.SPA(seed=seed)
    with model:
        model.ctrl = spa.Buffer(16, label="ctrl")

        def input_func(t):
            if t < 0.2:
                return "A"
            elif t < 0.4:
                return "B"
            else:
                return "C"

        model.input = spa.Input(ctrl=input_func)

        model.buff1 = spa.Buffer(D, label="buff1")
        model.buff2 = spa.Buffer(D, label="buff2")
        model.buff3 = spa.Buffer(D, label="buff3")

        node1 = nengo.Node([0, 1, 0])
        node2 = nengo.Node([0, 0, 1])

        nengo.Connection(node1, model.buff1.state.input)
        nengo.Connection(node2, model.buff2.state.input)

        actions = spa.Actions(
            "dot(ctrl, A) --> buff3=buff1",
            "dot(ctrl, B) --> buff3=buff2",
            "dot(ctrl, C) --> buff3=buff1*buff2",
        )
        model.bg = spa.BasalGanglia(actions)
        model.thal = spa.Thalamus(model.bg)

        buff3_probe = nengo.Probe(model.buff3.state.output, synapse=0.03)

    with Simulator(model) as sim:
        sim.run(0.6)

    data = sim.data[buff3_probe]

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

    valueA = np.mean(data[150:200], axis=0)  # should be [0, 1, 0]
    valueB = np.mean(data[350:400], axis=0)  # should be [0, 0, 1]
    valueC = np.mean(data[550:600], axis=0)  # should be [1, 0, 0]

    assert valueA[0] < 0.2
    assert valueA[1] > 0.8
    assert valueA[2] < 0.2

    assert valueB[0] < 0.2
    assert valueB[1] < 0.2
    assert valueB[2] > 0.8

    assert valueC[0] > 0.8
    assert valueC[1] < 0.2
    assert valueC[2] < 0.2
Exemplo n.º 16
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.vision = spa.Buffer(dimensions=p.D)
            model.phrase = spa.Buffer(dimensions=p.D)
            model.motor = spa.Buffer(dimensions=p.D)
            model.noun = spa.Memory(dimensions=p.D, synapse=0.1)
            model.verb = spa.Memory(dimensions=p.D, synapse=0.1)

            model.bg = spa.BasalGanglia(
                spa.Actions(
                    'dot(vision, WRITE) --> verb=vision',
                    'dot(vision, ONE+TWO+THREE) --> noun=vision',
                    '0.5*(dot(vision, NONE-WRITE-ONE-TWO-THREE) + '
                    'dot(phrase, WRITE*VERB))'
                    '--> motor=phrase*~NOUN',
                ))
            model.thal = spa.Thalamus(model.bg)

            model.cortical = spa.Cortical(
                spa.Actions(
                    'phrase=noun*NOUN',
                    'phrase=verb*VERB',
                ))

            def vision_input(t):
                index = int(t / p.time_per_word) % 3
                return ['WRITE', 'ONE', 'NONE'][index]

            model.input = spa.Input(vision=vision_input)

            self.motor_vocab = model.get_output_vocab('motor')
            self.p_thal = nengo.Probe(model.thal.actions.output, synapse=0.03)
            self.p_motor = nengo.Probe(model.motor.state.output, synapse=0.03)

        split.split_input_nodes(model, max_dim=64)
        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)

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

        return model
Exemplo n.º 17
0
        def __init__(self):
            self.memory = spa.Memory(dimensions=32, tau=0.05)

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

            self.input = spa.Input(memory=input)
Exemplo n.º 18
0
        def __init__(self):
            self.compare = spa.Compare(dimensions=16)

            def inputA(t):
                if 0 <= t < 0.1:
                    return 'A'
                else:
                    return 'B'

            self.input = spa.Input(compare_A=inputA, compare_B='A')
Exemplo n.º 19
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"
Exemplo n.º 20
0
        def __init__(self):
            self.buffer = spa.Buffer(dimensions=32)

            def input(t):
                if 0 <= t < 0.2:
                    return 'A'
                elif 0.2 <= t < 0.4:
                    return 'B'
                else:
                    return '0'
            self.input = spa.Input(buffer=input)
Exemplo n.º 21
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
Exemplo n.º 22
0
def test_fixed(allclose):
    with spa.SPA() as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=8, subdimensions=2)
        model.input = spa.Input(buffer1="A", buffer2="B")

    input1, vocab1 = model.get_module_input("buffer1")
    input2, vocab2 = model.get_module_input("buffer2")

    assert allclose(model.input.input_nodes["buffer1"].output, vocab1.parse("A").v)
    assert allclose(model.input.input_nodes["buffer2"].output, vocab2.parse("B").v)
Exemplo n.º 23
0
def test_fixed():
    with spa.SPA() as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=8, subdimensions=2)
        model.input = spa.Input(buffer1='A', buffer2='B')

    input1, vocab1 = model.get_module_input('buffer1')
    input2, vocab2 = model.get_module_input('buffer2')

    assert np.allclose(model.input.input_nodes['buffer1'].output,
                       vocab1.parse('A').v)
    assert np.allclose(model.input.input_nodes['buffer2'].output,
                       vocab2.parse('B').v)
Exemplo n.º 24
0
        def __init__(self):
            self.buffer = spa.Buffer(dimensions=16)
            self.buffer2 = spa.Buffer(dimensions=16)

            def input(t):
                if t < 0.1:
                    return 'A'
                elif t < 0.2:
                    return 'B'
                else:
                    return '0'

            self.input = spa.Input(buffer=input, buffer2='B')
Exemplo n.º 25
0
def test_thalamus(Simulator):
    model = spa.SPA(seed=30)

    with model:
        model.vision = spa.Buffer(dimensions=16, neurons_per_dimension=80)
        model.vision2 = spa.Buffer(dimensions=16, neurons_per_dimension=80)
        model.motor = spa.Buffer(dimensions=16, neurons_per_dimension=80)
        model.motor2 = spa.Buffer(dimensions=32, neurons_per_dimension=80)

        actions = spa.Actions(
            'dot(vision, A) --> motor=A, motor2=vision*vision2',
            'dot(vision, B) --> motor=vision, motor2=vision*A*~B',
            'dot(vision, ~A) --> motor=~vision, motor2=~vision*vision2'
        )
        model.bg = spa.BasalGanglia(actions)
        model.thalamus = spa.Thalamus(model.bg)

        def input_f(t):
            if t < 0.1:
                return 'A'
            elif t < 0.3:
                return 'B'
            elif t < 0.5:
                return '~A'
            else:
                return '0'
        model.input = spa.Input(vision=input_f, vision2='B*~A')

        input, vocab = model.get_module_input('motor')
        input2, vocab2 = model.get_module_input('motor2')
        p = nengo.Probe(input, 'output', synapse=0.03)
        p2 = nengo.Probe(input2, 'output', synapse=0.03)

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

    data = vocab.dot(sim.data[p].T)
    data2 = vocab2.dot(sim.data[p2].T)

    assert 0.8 < data[0, 100] < 1.1  # Action 1
    assert -0.2 < data2[0, 100] < 0.35
    assert -0.25 < data[1, 100] < 0.2
    assert 0.4 < data2[1, 100] < 0.6
    assert -0.2 < data[0, 299] < 0.2  # Action 2
    assert 0.6 < data2[0, 299] < 0.95
    assert 0.8 < data[1, 299] < 1.1
    assert -0.2 < data2[1, 299] < 0.2
    assert 0.8 < data[0, 499] < 1.0  # Action 3
    assert 0.0 < data2[0, 499] < 0.5
    assert -0.2 < data[1, 499] < 0.2
    assert 0.4 < data2[1, 499] < 0.7
Exemplo n.º 26
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
Exemplo n.º 27
0
def test_routing(Simulator):
    D = 2
    model = spa.SPA(seed=123)
    with model:
        model.ctrl = spa.Buffer(D, label='ctrl')

        def input_func(t):
            if t < 0.25:
                return 'A'
            else:
                return 'B'
        model.input = spa.Input(ctrl=input_func)

        model.buff1 = spa.Buffer(D, label='buff1')
        model.buff2 = spa.Buffer(D, label='buff2')
        model.buff3 = spa.Buffer(D, label='buff3')

        node1 = nengo.Node([1, 0])
        node2 = nengo.Node([0, 1])

        nengo.Connection(node1, model.buff1.state.input)
        nengo.Connection(node2, model.buff2.state.input)

        actions = spa.Actions('dot(ctrl, A) --> buff3=buff1',
                              'dot(ctrl, B) --> buff3=buff2')
        model.bg = spa.BasalGanglia(actions)
        model.thal = spa.Thalamus(model.bg)

        buff1_probe = nengo.Probe(model.buff1.state.output, synapse=0.03)
        buff2_probe = nengo.Probe(model.buff2.state.output, synapse=0.03)
        buff3_probe = nengo.Probe(model.buff3.state.output, synapse=0.03)
        thal_probe = nengo.Probe(model.thal.output, synapse=0.03)

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

    data1 = sim.data[buff1_probe]
    data2 = sim.data[buff2_probe]
    data3 = sim.data[buff3_probe]
    data_thal = sim.data[thal_probe]
    trange = sim.trange()

    assert abs(np.mean(data1[trange < 0.25] - data3[trange < 0.25])) < 0.15
    assert abs(np.mean(data2[trange > 0.25] - data3[trange > 0.25])) < 0.15
    assert data_thal[249][1] < 0.01
    assert data_thal[249][0] > 0.8
    assert data_thal[499][1] > 0.8
    assert data_thal[499][0] < 0.01
Exemplo n.º 28
0
def test_nondefault_routing(Simulator, seed, plt):
    D = 3
    model = spa.SPA(seed=seed)
    with model:
        model.ctrl = spa.Buffer(16, label='ctrl')

        def input_func(t):
            if t < 0.2:
                return 'A'
            elif t < 0.4:
                return 'B'
            else:
                return 'C'
        model.input = spa.Input(ctrl=input_func)

        model.buff1 = spa.Buffer(D, label='buff1')
        model.buff2 = spa.Buffer(D, label='buff2')
        model.cmp = spa.Compare(D)

        node1 = nengo.Node([0, 1, 0])
        node2 = nengo.Node([0, 0, 1])

        nengo.Connection(node1, model.buff1.state.input)
        nengo.Connection(node2, model.buff2.state.input)

        actions = spa.Actions('dot(ctrl, A) --> cmp_A=buff1, cmp_B=buff1',
                              'dot(ctrl, B) --> cmp_A=buff1, cmp_B=buff2',
                              'dot(ctrl, C) --> cmp_A=buff2, cmp_B=buff2',
                              )
        model.bg = spa.BasalGanglia(actions)
        model.thal = spa.Thalamus(model.bg)

        compare_probe = nengo.Probe(model.cmp.output, synapse=0.03)

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

    vocab = model.get_output_vocab('cmp')
    data = sim.data[compare_probe]
    similarity = np.dot(data, vocab.parse('YES').v)

    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
Exemplo n.º 29
0
def test_nondefault_routing(Simulator, seed):
    D = 3
    model = spa.SPA(seed=seed)
    with model:
        model.ctrl = spa.Buffer(16, label="ctrl")

        def input_func(t):
            if t < 0.2:
                return "A"
            elif t < 0.4:
                return "B"
            else:
                return "C"

        model.input = spa.Input(ctrl=input_func)

        model.buff1 = spa.Buffer(D, label="buff1")
        model.buff2 = spa.Buffer(D, label="buff2")
        model.cmp = spa.Compare(D)

        node1 = nengo.Node([0, 1, 0])
        node2 = nengo.Node([0, 0, 1])

        nengo.Connection(node1, model.buff1.state.input)
        nengo.Connection(node2, model.buff2.state.input)

        actions = spa.Actions(
            "dot(ctrl, A) --> cmp_A=buff1, cmp_B=buff1",
            "dot(ctrl, B) --> cmp_A=buff1, cmp_B=buff2",
            "dot(ctrl, C) --> cmp_A=buff2, cmp_B=buff2",
        )
        model.bg = spa.BasalGanglia(actions)
        model.thal = spa.Thalamus(model.bg)

        compare_probe = nengo.Probe(model.cmp.output, synapse=0.03)

    with Simulator(model) 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
Exemplo n.º 30
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()