Exemple #1
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)

    with Simulator(model) as sim:
        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
Exemple #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
Exemple #3
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
 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')
Exemple #5
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
Exemple #6
0
def test_errors():
    # buffer2 does not exist
    with pytest.raises(NameError):
        with spa.SPA() as model:
            model.buffer = spa.Buffer(dimensions=16)
            model.cortical = spa.Cortical(spa.Actions('buffer2=buffer'))

    # conditional expressions not implemented
    with pytest.raises(NotImplementedError):
        with spa.SPA() as model:
            model.buffer = spa.Buffer(dimensions=16)
            model.cortical = spa.Cortical(
                spa.Actions('dot(buffer,A) --> buffer=buffer'))

    # dot products not implemented
    with pytest.raises(NotImplementedError):
        with spa.SPA() as model:
            model.scalar = spa.Buffer(dimensions=1, subdimensions=1)
            model.cortical = spa.Cortical(
                spa.Actions('scalar=dot(scalar, FOO)'))
Exemple #7
0
def test_routing_recurrency_compilation(Simulator, seed):
    D = 2
    model = spa.SPA(seed=seed)
    with model:
        model.buff1 = spa.Buffer(D, label="buff1")
        model.buff2 = spa.Buffer(D, label="buff2")
        actions = spa.Actions("0.5 --> buff2=buff1, buff1=buff2")
        model.bg = spa.BasalGanglia(actions)
        model.thal = spa.Thalamus(model.bg)

    with Simulator(model) as sim:
        assert sim
Exemple #8
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
Exemple #9
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
Exemple #10
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
Exemple #11
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
Exemple #12
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
Exemple #13
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
def test_transform(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=16)
        model.cortical = spa.Cortical(spa.Actions("buffer2=buffer1*B"))
        model.input = spa.Input(buffer1="A")

    output, vocab = model.get_module_output("buffer2")

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

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

    match = np.dot(sim.data[p], vocab.parse("A*B").v)
    assert match[199] > 0.7
Exemple #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()
Exemple #16
0
def test_translate(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=32)
        model.input = spa.Input(buffer1='A')
        model.cortical = spa.Cortical(spa.Actions('buffer2=buffer1'))

    output, vocab = model.get_module_output('buffer2')

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

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

    match = np.dot(sim.data[p], vocab.parse('A').v)
    assert match[199] > 0.8
    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
    def model(self, p):
        model = spa.SPA()
        with model:
            model.vision = spa.Buffer(dimensions=p.D)
            model.state = spa.Memory(dimensions=p.D)
            actions = ['dot(state, S%d) --> state=S%d' % (i,(i+1))
                       for i in range(p.n_actions - 1)]
            actions.append('dot(state, S%d) --> state=vision' %
                           (p.n_actions - 1))
            model.bg = spa.BasalGanglia(actions=spa.Actions(*actions))
            model.thal = spa.Thalamus(model.bg)

            model.input = spa.Input(vision='S%d' % p.start,
                                    state=lambda t: 'S%d' % p.start if
                                              t < 0.1 else '0')

            self.probe = nengo.Probe(model.thal.actions.output, synapse=0.03)

        return model
Exemple #19
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.vision = spa.Buffer(dimensions=p.D)
            model.state = spa.Memory(dimensions=p.D)
            actions = [
                'dot(state, S%d) --> state=S%d' % (i, (i + 1))
                for i in range(p.n_actions - 1)
            ]
            actions.append('dot(state, S%d) --> state=vision' %
                           (p.n_actions - 1))
            model.bg = spa.BasalGanglia(actions=spa.Actions(*actions))
            model.thal = spa.Thalamus(model.bg)

            model.input = spa.Input(vision='S%d' % p.start,
                                    state=lambda t: 'S%d' % p.start
                                    if t < 0.1 else '0')

            self.probe = nengo.Probe(model.thal.actions.output, synapse=0.03)

        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
Exemple #20
0
def test_basal_ganglia(Simulator, seed, plt):
    model = spa.SPA(seed=seed)

    with model:
        model.vision = spa.Buffer(dimensions=16)
        model.motor = spa.Buffer(dimensions=16)

        actions = spa.Actions(
            '0.5 --> motor=A',
            'dot(vision,CAT) --> motor=B',
            'dot(vision*CAT,DOG) --> motor=C',
            '2*dot(vision,CAT*0.5) --> motor=D',
            'dot(vision,CAT)+0.5-dot(vision,CAT) --> motor=E',
        )
        model.bg = spa.BasalGanglia(actions)

        def input(t):
            if t < 0.1:
                return '0'
            elif t < 0.2:
                return 'CAT'
            elif t < 0.3:
                return 'DOG*~CAT'
            else:
                return '0'

        model.input = spa.Input(vision=input)
        p = nengo.Probe(model.bg.input, 'output', synapse=0.03)

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

    plt.plot(t, sim.data[p])
    plt.title('Basal Ganglia output')

    assert 0.6 > sim.data[p][t == 0.1, 0] > 0.4
    assert sim.data[p][t == 0.2, 1] > 0.8
    assert sim.data[p][-1, 2] > 0.6

    assert np.allclose(sim.data[p][:, 1], sim.data[p][:, 3])
    assert np.allclose(sim.data[p][:, 0], sim.data[p][:, 4])
Exemple #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
Exemple #22
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.state = spa.Memory(dimensions=p.D)
            actions = [
                'dot(state, S%d) --> state=S%d' % (i, (i + 1) % p.n_actions)
                for i in range(p.n_actions)
            ]
            model.bg = spa.BasalGanglia(actions=spa.Actions(*actions))
            model.thal = spa.Thalamus(model.bg)

            def state_input(t):
                if t < 0.1:
                    return 'S0'
                else:
                    return '0'

            model.input = spa.Input(state=state_input)

            self.probe = nengo.Probe(model.thal.actions.output, synapse=0.03)
        return model
Exemple #23
0
    def __init__(self, num_actions):
        spa.SPA.__init__(self)

        #Specify the modules to be used
        self.cortex = spa.Buffer(dimensions=dimensions)

        # Generate names for actions
        def int_to_ascii(i):
            return chr(ord("A") + i)

        # Generate sequential action names
        self.action_names = [
            int_to_ascii((i / 26) % 26) + int_to_ascii(i % 26)
            for i in range(num_actions)
        ]

        # From this build action mapping
        action_mappings = [
            "dot(cortex, %s) --> cortex = %s" % (a, b)
            for (a, b) in zip(self.action_names, self.action_names[1:])
        ]
        action_mappings.append("dot(cortex, %s) --> cortex = %s" %
                               (self.action_names[-1], self.action_names[0]))

        # Build SPA actions
        actions = spa.Actions(*action_mappings)

        self.bg = spa.BasalGanglia(actions=actions)
        self.thal = spa.Thalamus(self.bg)

        #Specify the input
        def start(t):
            if t < 0.05:
                return self.action_names[0]
            else:
                return '0'

        self.input = spa.Input(cortex=start)
Exemple #24
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
def test_connect(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=16)
        model.buffer3 = spa.Buffer(dimensions=16)
        model.cortical = spa.Cortical(
            spa.Actions("buffer2=buffer1", "buffer3=~buffer1"))
        model.input = spa.Input(buffer1="A")

    output2, vocab = model.get_module_output("buffer2")
    output3, vocab = model.get_module_output("buffer3")

    with model:
        p2 = nengo.Probe(output2, "output", synapse=0.03)
        p3 = nengo.Probe(output3, "output", synapse=0.03)

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

    match = np.dot(sim.data[p2], vocab.parse("A").v)
    assert match[199] > 0.9
    match = np.dot(sim.data[p3], vocab.parse("~A").v)
    assert match[199] > 0.9
Exemple #26
0
def test_direct(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=32)
        model.cortical = spa.Cortical(
            spa.Actions('buffer1=A', 'buffer2=B', 'buffer1=C, buffer2=C'))

    output1, vocab1 = model.get_module_output('buffer1')
    output2, vocab2 = model.get_module_output('buffer2')

    with model:
        p1 = nengo.Probe(output1, 'output', synapse=0.03)
        p2 = nengo.Probe(output2, 'output', synapse=0.03)

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

    match1 = np.dot(sim.data[p1], vocab1.parse('A+C').v)
    match2 = np.dot(sim.data[p2], vocab2.parse('B+C').v)
    # both values should be near 1.0 since buffer1 is driven to both A and C
    # and buffer2 is driven to both B and C.
    assert match1[199] > 0.75
    assert match2[199] > 0.75
Exemple #27
0
def test_connect(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=16)
        model.buffer3 = spa.Buffer(dimensions=16)
        model.cortical = spa.Cortical(
            spa.Actions('buffer2=buffer1', 'buffer3=~buffer1'))
        model.input = spa.Input(buffer1='A')

    output2, vocab = model.get_module_output('buffer2')
    output3, vocab = model.get_module_output('buffer3')

    with model:
        p2 = nengo.Probe(output2, 'output', synapse=0.03)
        p3 = nengo.Probe(output3, 'output', synapse=0.03)

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

    match = np.dot(sim.data[p2], vocab.parse('A').v)
    assert match[199] > 0.9
    match = np.dot(sim.data[p3], vocab.parse('~A').v)
    assert match[199] > 0.9
Exemple #28
0
        def __init__(self):
            self.vision = spa.Buffer(dimensions=16)
            self.motor = spa.Buffer(dimensions=16)

            actions = spa.Actions(
                '0.5 --> motor=A',
                'dot(vision,CAT) --> motor=B',
                'dot(vision*CAT,DOG) --> motor=C',
                '2*dot(vision,CAT*0.5) --> motor=D',
                'dot(vision,CAT)+0.5-dot(vision,CAT) --> motor=E',
            )
            self.bg = spa.BasalGanglia(actions)

            def input(t):
                if t < 0.1:
                    return '0'
                elif t < 0.2:
                    return 'CAT'
                elif t < 0.3:
                    return 'DOG*~CAT'
                else:
                    return '0'

            self.input = spa.Input(vision=input)
import nengo
import nengo.spa as spa

D = 32  # the dimensionality of the vectors

model = spa.SPA()
with model:
    model.vision = spa.Buffer(D)

    model.speech = spa.Buffer(D)    

    actions = spa.Actions(
        'dot(vision, DOG) --> speech=BARK',
        'dot(vision, CAT) --> speech=MEOW',
        'dot(vision, RAT) --> speech=SQUEAK',
        'dot(vision, COW) --> speech=MOO',
        )
        
    model.bg = spa.BasalGanglia(actions)
    model.thalamus = spa.Thalamus(model.bg)
 #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',
     'dot(now_state, SEARCH2) --> now_action=FORWARD',
     'dot(now_action, FORWARD) --> now_state=WAIT, now_action=DONE2',
     # Then repeat
     'dot(now_action, DONE2) --> now_state=SEARCH1',
     # Stop when food is found
     'dot(food, ISFOOD) --> now_state=STOP',
     )
 
 def subgoal_input(t):
     if t < 0.1:
         return 'SEARCH_BALL'
     else:
         return '0'
 
 model.bg = spa.BasalGanglia(actions)