Example #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)

    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
Example #2
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
Example #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
Example #4
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
Example #5
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
Example #6
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
Example #7
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
Example #8
0
def test_errors():
    # motor does not exist
    with pytest.raises(SpaParseError):
        with spa.SPA() as model:
            model.vision = spa.Buffer(dimensions=16)
            actions = spa.Actions("0.5 --> motor=A")
            model.bg = spa.BasalGanglia(actions)

    # dot products not implemented
    with pytest.raises(NotImplementedError):
        with spa.SPA() as model:
            model.scalar = spa.Buffer(dimensions=16, subdimensions=1)
            actions = spa.Actions("0.5 --> scalar=dot(scalar, FOO)")
            model.bg = spa.BasalGanglia(actions)
            model.thalamus = spa.Thalamus(model.bg)
Example #9
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
Example #10
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
Example #11
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
Example #12
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)

        return model
Example #13
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
Example #14
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
Example #15
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)
Example #16
0
def create_model():

    #print trial_info
    print('---- INTIALIZING MODEL ----')
    global model

    model = spa.SPA()
    with model:

        #display current stimulus pair (not part of model)
        if nengo_gui_on and True:
            model.pair_input = nengo.Node(present_pair)
            model.pair_display = nengo.Node(
                display_func,
                size_in=model.pair_input.size_out)  # to show input
            nengo.Connection(model.pair_input,
                             model.pair_display,
                             synapse=None)

        # control
        model.control_net = nengo.Network()
        with model.control_net:
            #assuming the model knows which hand to use (which was blocked)
            model.hand_input = nengo.Node(get_hand)
            model.target_hand = spa.State(Dmid, vocab=vocab_motor, feedback=1)
            nengo.Connection(model.hand_input,
                             model.target_hand.input,
                             synapse=None)

            model.attend = spa.State(D, vocab=vocab_attend,
                                     feedback=.5)  # vocab_attend
            model.goal = spa.State(Dlow, vocab=vocab_goal,
                                   feedback=.7)  # current goal

        ### vision ###

        # set up network parameters
        n_vis = X_train.shape[1]  # nr of pixels, dimensions of network
        n_hid = 1000  # nr of gabor encoders/neurons

        # random state to start
        rng = np.random.RandomState(9)
        encoders = Gabor().generate(
            n_hid, (4, 4), rng=rng)  # gabor encoders, 11x11 apparently, why?
        encoders = Mask(
            (14, 90)).populate(encoders, rng=rng,
                               flatten=True)  # use them on part of the image

        model.visual_net = nengo.Network()
        with model.visual_net:

            #represent currently attended item
            model.attended_item = nengo.Node(present_item2, size_in=D)
            nengo.Connection(model.attend.output, model.attended_item)

            model.vision_gabor = nengo.Ensemble(
                n_hid,
                n_vis,
                eval_points=X_train,
                neuron_type=nengo.LIF(),
                intercepts=nengo.dists.Uniform(-0.1, 0.1),
                #intercepts=nengo.dists.Choice([-0.5]), #should we comment this out? not sure what's happening
                #max_rates=nengo.dists.Choice([100]),
                encoders=encoders)
            #recurrent connection (time constant 500 ms)
            # strength = 1 - (100/500) = .8

            zeros = np.zeros_like(X_train)
            nengo.Connection(
                model.vision_gabor,
                model.vision_gabor,
                synapse=0.005,  #.1
                eval_points=np.vstack(
                    [X_train, zeros,
                     np.random.randn(*X_train.shape)]),
                transform=.5)

            model.visual_representation = nengo.Ensemble(n_hid,
                                                         dimensions=Dmid)

            model.visconn = nengo.Connection(
                model.vision_gabor,
                model.visual_representation,
                synapse=0.005,  #was .005
                eval_points=X_train,
                function=train_targets,
                solver=nengo.solvers.LstsqL2(reg=0.01))
            nengo.Connection(model.attended_item,
                             model.vision_gabor,
                             synapse=.02)  #.03) #synapse?

            # display attended item, only in gui
            if nengo_gui_on:
                # show what's being looked at
                model.display_attended = nengo.Node(
                    display_func,
                    size_in=model.attended_item.size_out)  # to show input
                nengo.Connection(model.attended_item,
                                 model.display_attended,
                                 synapse=None)
                #add node to plot total visual activity
                model.visual_activation = nengo.Node(None, size_in=1)
                nengo.Connection(model.vision_gabor.neurons,
                                 model.visual_activation,
                                 transform=np.ones((1, n_hid)),
                                 synapse=None)

        ### central cognition ###

        # concepts
        model.concepts = spa.AssociativeMemory(
            vocab_all_words,  #vocab_concepts,
            wta_output=True,
            wta_inhibit_scale=1,  #was 1
            #default_output_key='NONE', #what to say if input doesn't match
            threshold=0.3
        )  # how strong does input need to be for it to recognize
        nengo.Connection(
            model.visual_representation,
            model.concepts.input,
            transform=.8 * vision_mapping
        )  #not too fast to concepts, might have to be increased to have model react faster to first word.

        #concepts accumulator
        model.concepts_evidence = spa.State(
            1, feedback=1, feedback_synapse=0.005
        )  #the lower the synapse, the faster it accumulates (was .1)
        concepts_evidence_scale = 2.5
        nengo.Connection(model.concepts.am.elem_output,
                         model.concepts_evidence.input,
                         transform=concepts_evidence_scale * np.ones(
                             (1, model.concepts.am.elem_output.size_out)),
                         synapse=0.005)

        #concepts switch
        model.do_concepts = spa.AssociativeMemory(vocab_reset,
                                                  default_output_key='CLEAR',
                                                  threshold=.2)
        nengo.Connection(
            model.do_concepts.am.ensembles[-1],
            model.concepts_evidence.all_ensembles[0].neurons,
            transform=np.ones(
                (model.concepts_evidence.all_ensembles[0].n_neurons, 1)) * -10,
            synapse=0.005)

        # pair representation
        model.vis_pair = spa.State(
            D, vocab=vocab_all_words, feedback=1.0, feedback_synapse=.05
        )  #was 2, 1.6 works ok, but everything gets activated.

        #learned words
        model.dm_learned_words = spa.AssociativeMemory(
            vocab_learned_words, threshold=.2
        )  #default_output_key='NONE' familiarity should be continuous over all items, so no wta
        nengo.Connection(model.dm_learned_words.output,
                         model.dm_learned_words.input,
                         transform=.4,
                         synapse=.02)

        # this stores the accumulated evidence for or against familiarity
        model.familiarity = spa.State(
            1, feedback=.9,
            feedback_synapse=0.1)  #fb syn influences speed of acc
        familiarity_scale = 0.2  #keep stable for negative fam
        #nengo.Connection(model.dm_learned_words.am.ensembles[-1], model.familiarity.input, transform=-(familiarity_scale+0.8)) #accumulate to -1
        nengo.Connection(
            model.dm_learned_words.am.elem_output,
            model.familiarity.input,  #am.element_output == all outputs, we sum
            transform=(familiarity_scale + .1) * np.ones(
                (1, model.dm_learned_words.am.elem_output.size_out))
        )  #accumulate to 1

        model.do_fam = spa.AssociativeMemory(vocab_reset,
                                             default_output_key='CLEAR',
                                             threshold=.2)
        nengo.Connection(
            model.do_fam.am.ensembles[-1],
            model.familiarity.all_ensembles[0].neurons,
            transform=np.ones(
                (model.familiarity.all_ensembles[0].n_neurons, 1)) * -10,
            synapse=0.005)
        #negative accumulator
        nengo.Connection(
            model.do_fam.am.elem_output,
            model.familiarity.input,
            transform=-familiarity_scale * np.ones(
                (1, model.do_fam.am.elem_output.size_out)))  #accumulate to -1

        #fam model.dm_pairs = spa.AssociativeMemory(vocab_learned_pairs, input_keys=list_of_pairs,wta_output=True)
        #fam nengo.Connection(model.dm_pairs.output,model.dm_pairs.input,transform=.5)

        #this works:
        #fam model.representation = spa.AssociativeMemory(vocab_learned_pairs, input_keys=list_of_pairs, wta_output=True)
        #fam nengo.Connection(model.representation.output, model.representation.input, transform=2)
        #fam model.rep_filled = spa.State(1,feedback_synapse=.005) #no fb syn specified
        #fam nengo.Connection(model.representation.am.elem_output,model.rep_filled.input, #am.element_output == all outputs, we sum
        #fam                  transform=.8*np.ones((1,model.representation.am.elem_output.size_out)),synapse=0)

        #this doesn't:
        #model.representation = spa.State(D,feedback=1)
        #model.rep_filled = spa.State(1,feedback_synapse=.005) #no fb syn specified
        #nengo.Connection(model.representation.output,model.rep_filled.input, #am.element_output == all outputs, we sum
        #                 transform=.8*np.ones((1,model.representation.output.size_out)),synapse=0)

        # this shouldn't really be fixed I think
        #fam model.comparison = spa.Compare(D, vocab=vocab_concepts)

        #motor
        model.motor_net = nengo.Network()
        with model.motor_net:

            #input multiplier
            model.motor_input = spa.State(Dmid, vocab=vocab_motor)

            #higher motor area (SMA?)
            model.motor = spa.State(Dmid, vocab=vocab_motor, feedback=.7)

            #connect input multiplier with higher motor area
            nengo.Connection(model.motor_input.output,
                             model.motor.input,
                             synapse=.1,
                             transform=2)

            #finger area
            model.fingers = spa.AssociativeMemory(
                vocab_fingers,
                input_keys=['L1', 'L2', 'R1', 'R2'],
                wta_output=True)
            nengo.Connection(model.fingers.output,
                             model.fingers.input,
                             synapse=0.1,
                             transform=0.3)  #feedback

            #conncetion between higher order area (hand, finger), to lower area
            nengo.Connection(model.motor.output,
                             model.fingers.input,
                             transform=.25 * motor_mapping)  #was .2

            #finger position (spinal?)
            model.finger_pos = nengo.networks.EnsembleArray(n_neurons=50,
                                                            n_ensembles=4)
            nengo.Connection(model.finger_pos.output,
                             model.finger_pos.input,
                             synapse=0.1,
                             transform=0.8)  #feedback

            #connection between finger area and finger position
            nengo.Connection(model.fingers.am.elem_output,
                             model.finger_pos.input,
                             transform=1.0 *
                             np.diag([0.55, .54, .56, .55]))  #fix these

        model.bg = spa.BasalGanglia(
            spa.Actions(
                #wait & start
                a_aa_wait='dot(goal,WAIT) - .9 --> goal=0',
                a_attend_item1=
                'dot(goal,DO_TASK) - .1 --> goal=RECOG, attend=ITEM1, do_concepts=GO',

                #attend words
                b_attending_item1=
                'dot(goal,RECOG) + dot(attend,ITEM1) - concepts_evidence - .3 --> goal=RECOG, attend=ITEM1, do_concepts=GO',  # vis_pair=2.5*(ITEM1*concepts)',
                c_attend_item2=
                'dot(goal,RECOG) + dot(attend,ITEM1) + concepts_evidence - 1.6 --> goal=RECOG2, attend=ITEM2, vis_pair=3*(ITEM1*concepts)',
                d_attending_item2=
                'dot(goal,RECOG2+RECOG) + dot(attend,ITEM2) - concepts_evidence - .4 --> goal=RECOG2, attend=ITEM2, do_concepts=GO, dm_learned_words=1.0*(~ITEM1*vis_pair)',  #vis_pair=1.2*(ITEM2*concepts)
                e_judge_familiarity=
                'dot(goal,RECOG2) + dot(attend,ITEM2) + concepts_evidence - 1.8 --> goal=FAMILIARITY, do_fam=GO, vis_pair=1.9*(ITEM2*concepts), dm_learned_words=2.0*(~ITEM1*vis_pair+~ITEM2*vis_pair)',

                #judge familiarity
                f_judge_familiarity=
                'dot(goal,FAMILIARITY) - .1 --> goal=FAMILIARITY, do_fam=GO, dm_learned_words=.8*(~ITEM1*vis_pair+~ITEM2*vis_pair)',
                g_respond_unfamiliar=
                'dot(goal,FAMILIARITY) - familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RESPOND, do_fam=GO, motor_input=1.6*(target_hand+MIDDLE)',
                h_respond_familiar=
                'dot(goal,FAMILIARITY) + familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RESPOND, do_fam=GO, motor_input=1.6*(target_hand+INDEX)',
                x_response_done=
                '1.1*dot(goal,RESPOND) + 1.5*dot(fingers,L1+L2+R1+R2) - .7--> goal=2*END',
                y_end='dot(goal,END)-.1 --> goal=END',
                z_threshold='.05 --> goal=0'

                #possible to match complete buffer, ie is representation filled?
                # motor_input=1.5*target_hand+MIDDLE,
            ))

        #'dot(attention, W1) - evidence - 0.8 --> motor=NO, attention=W1',
        #'dot(attention, W1) + evidence - 0.8 --> attention=W2, reset=EVIDENCE',
        #'dot(attention, W1) --> attention=W1',  # if we don't set attention it goes back to 0
        #'dot(attention, W2) - evidence - 0.8 --> motor=NO, attention=W2',
        #'dot(attention, W2) + evidence - 0.8 --> motor=YES, attention=W2',
        #'dot(attention, W2) --> attention=W2',  # option might be feedback on attention, then no rule 3/6 but default rule

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

        model.cortical = spa.Cortical(  # cortical connection: shorthand for doing everything with states and connections
            spa.Actions(
                #  'motor_input = .04*target_hand',
                # 'dm_learned_words = .1*vis_pair',
                #'dm_pairs = 2*stimulus'
                #'vis_pair = 2*attend*concepts+concepts',
                #fam 'comparison_A = 2*vis_pair',
                #fam 'comparison_B = 2*representation*~attend',
            ))

        #probes
        model.pr_motor_pos = nengo.Probe(
            model.finger_pos.output,
            synapse=.01)  #raw vector (dimensions x time)
        model.pr_motor = nengo.Probe(model.fingers.output, synapse=.01)
        #model.pr_motor1 = nengo.Probe(model.motor.output, synapse=.01)

        if not nengo_gui_on:
            model.pr_vision_gabor = nengo.Probe(
                model.vision_gabor.neurons, synapse=.005
            )  #do we need synapse, or should we do something with the spikes
            model.pr_familiarity = nengo.Probe(
                model.dm_learned_words.am.elem_output,
                synapse=.01)  #element output, don't include default
            model.pr_concepts = nengo.Probe(
                model.concepts.am.elem_output,
                synapse=.01)  # element output, don't include default

        #multiply spikes with the connection weights

        #input
        model.input = spa.Input(goal=goal_func)

        #print(sum(ens.n_neurons for ens in model.all_ensembles))

        #return model

        #to show select BG rules
        # get names rules
        if nengo_gui_on and False:
            vocab_actions = spa.Vocabulary(model.bg.output.size_out)
            for i, action in enumerate(model.bg.actions.actions):
                vocab_actions.add(action.name.upper(),
                                  np.eye(model.bg.output.size_out)[i])
            model.actions = spa.State(model.bg.output.size_out,
                                      vocab=vocab_actions)
            nengo.Connection(model.thalamus.output, model.actions.input)

            for net in model.networks:
                if net.label is not None and net.label.startswith('channel'):
                    net.label = ''
def main():

    model = spa.SPA(label="Vector Storage")
    with model:

        # Dimensionality of each representation
        num_dimensions = 2
        sub_dimensions = 2

        # Create the vocabulary
        vocab = Vocabulary(num_dimensions, randomize=False)

        # Form the inputs
        stored_value_1 = [1] * num_dimensions
        stored_value_1 = [
            s / np.linalg.norm(stored_value_1) for s in stored_value_1
        ]
        vocab.add("Stored_value_1", stored_value_1)

        stored_value_2 = [(-1**i) for i in range(num_dimensions)]
        stored_value_2 = [
            s / np.linalg.norm(stored_value_2) for s in stored_value_2
        ]
        vocab.add("Stored_value_2", stored_value_2)

        def first_input(t):
            if t < 10:
                return "Stored_value_2"
            else:
                return "Stored_value_1"

        def second_input(t):
            if t < 5:
                return "Stored_value_1"
            else:
                return "Stored_value_2"

        # Buffers to store the input
        model.buffer1 = spa.Buffer(dimensions=num_dimensions,
                                   subdimensions=sub_dimensions,
                                   neurons_per_dimension=200,
                                   direct=True)
        model.buffer2 = spa.Buffer(dimensions=num_dimensions,
                                   subdimensions=sub_dimensions,
                                   neurons_per_dimension=200,
                                   direct=True)

        # Probe to visualize the values stored in the buffers
        buffer_1_probe = nengo.Probe(model.buffer1.state.output)
        buffer_2_probe = nengo.Probe(model.buffer2.state.output)

        # Connect up the inputs
        model.input = spa.Input(buffer1=first_input)
        model.input = spa.Input(buffer2=second_input)

        # Buffer to store the output
        model.buffer3 = spa.Buffer(dimensions=num_dimensions,
                                   subdimensions=sub_dimensions,
                                   neurons_per_dimension=200,
                                   direct=True)
        buffer_3_probe = nengo.Probe(model.buffer3.state.output)

        # Control system
        actions = spa.Actions(
            'dot(buffer1, Stored_value_2) --> buffer3=Stored_value_2',
            'dot(buffer1, Stored_value_1) --> buffer3=Stored_value_1+Stored_value_2'
        )
        model.bg = spa.BasalGanglia(actions)
        model.thalamus = spa.Thalamus(model.bg)

    # Start the simulator
    sim = nengo.Simulator(model)

    # Dynamic plotting
    plt.ion()  # Dynamic updating of plots
    fig = plt.figure(figsize=(15, 8))
    plt.show()
    ax = fig.gca()
    ax.set_title("Vector Storage")

    while True:
        sim.run(1)  # Run for an additional 1 second
        plt.clf()  # Clear the figure
        plt.plot(sim.trange(),
                 similarity(sim.data, buffer_1_probe, vocab),
                 label="Buffer 1 Value")  # Plot the entire dataset so far
        plt.plot(sim.trange(),
                 similarity(sim.data, buffer_2_probe, vocab),
                 label="Buffer 2 Value")
        plt.plot(sim.trange(),
                 similarity(sim.data, buffer_3_probe, vocab),
                 label="Buffer 3 Value")
        print sim.data[buffer_1_probe][-1]
        print sim.data[buffer_2_probe][-1]
        print sim.data[buffer_3_probe][-1]
        plt.legend(vocab.keys * 3, loc=2)
        plt.draw()  # Re-draw
Example #18
0
def Spaun():
    model = spa.SPA(label='Spaun', seed=cfg.seed)
    with model:
        model.config[nengo.Ensemble].max_rates = cfg.max_rates
        model.config[nengo.Ensemble].neuron_type = cfg.neuron_type
        model.config[nengo.Ensemble].n_neurons = cfg.n_neurons_ens
        model.config[nengo.Connection].synapse = cfg.pstc

        model.stim = Stimulus()
        model.vis = Vision()
        model.ps = ProdSys()
        model.reward = RewardEval()
        model.enc = InfoEnc()
        model.mem = Memory()
        model.trfm = TrfmSys()
        model.dec = InfoDec()
        model.mtr = Motor()
        model.monitor = Monitor()

        model.learn_conns = []

        if hasattr(model, 'vis') and hasattr(model, 'ps'):
            copy_draw_action = \
                ['0.5 * (dot(ps_task, X) + dot(vis, ZER)) --> ps_task = W, ps_state = TRANS0, ps_dec = FWD',  # noqa
                 'dot(ps_task, W-DEC) - dot(vis, QM) --> ps_state = ps_state']  # noqa
            recog_action = \
                ['0.5 * (dot(ps_task, X) + dot(vis, ONE)) --> ps_task = R, ps_state = TRANS0, ps_dec = FWD',  # noqa
                 'dot(ps_task, R-DEC) - dot(vis, QM) --> ps_state = ps_state']  # noqa

            learn_state_action = ['0.5 * (dot(ps_task, X) + dot(vis, TWO)) - dot(vis, QM) --> ps_task = L, ps_state = LEARN, ps_dec = FWD']  # noqa
                                  # '0.5 * (dot(ps_task, L) + dot(vis, {:s} + {:s})) --> ps_state = LEARN'.format(*vocab.reward_sp_strs)]  # noqa
            if hasattr(model, 'reward'):
                learn_action = ['0.5 * dot(ps_task, L) - dot(vis, QM) --> ps_action = %s, ps_state = LEARN, ps_dec = NONE' %  # noqa
                                s for s in vocab.ps_action_learn_sp_strs]
            else:
                learn_action = []

            mem_action = \
                ['0.5 * (dot(ps_task, X) + dot(vis, THR)) --> ps_task = M, ps_state = TRANS0, ps_dec = FWD',  # noqa
                 'dot(ps_task, M-DEC) - dot(vis, F + R + QM) --> ps_state = ps_state',  # noqa
                 '0.5 * (dot(ps_task, M-DEC) + dot(vis, F)) - dot(vis, QM) --> ps_dec = FWD',  # noqa
                 '0.5 * (dot(ps_task, M-DEC) + dot(vis, R)) - dot(vis, QM) --> ps_dec = REV']  # noqa

            if hasattr(model, 'trfm'):
                count_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, FOR)) --> ps_task = C, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, C-DEC) + dot(ps_state, TRANS0)) - dot(vis, QM) --> ps_state = CNT0',  # noqa
                     '0.5 * (dot(ps_task, C-DEC) + dot(ps_state, CNT0)) - dot(vis, QM) --> ps_state = CNT1',  # noqa
                     '(0.25 * (dot(ps_task, DEC) + dot(ps_state, CNT1)) + 0.5 * dot(trfm_compare, NO_MATCH)) + (dot(ps_dec, CNT) - 1) - dot(vis, QM) --> ps_dec = CNT, ps_state = CNT1',  # noqa
                     '(0.25 * (dot(ps_task, DEC) + dot(ps_state, CNT1)) + 0.5 * dot(trfm_compare, MATCH)) + (dot(ps_dec, CNT) - 1) - dot(vis, QM) --> ps_dec = FWD, ps_state = TRANS0']  # noqa
                qa_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, FIV)) --> ps_task = A, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     'dot(ps_task, A-DEC) - dot(vis, K + P + QM) --> ps_state = ps_state',  # noqa
                     '0.5 * (dot(ps_task, A-DEC) + dot(vis, K)) - dot(vis, QM) --> ps_state = QAK',  # noqa
                     '0.5 * (dot(ps_task, A-DEC) + dot(vis, P)) - dot(vis, QM) --> ps_state = QAP']  # noqa

                rvc_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, SIX)) --> ps_task = V, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, V-DEC) + dot(ps_state, TRANS0)) - dot(vis, QM) --> ps_state = TRANS1',  # noqa
                     '0.5 * (dot(ps_task, V-DEC) + dot(ps_state, TRANS1)) - dot(vis, QM) --> ps_state = TRANS0']  # noqa
                fi_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, SEV)) --> ps_task = F, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, F-DEC) + dot(ps_state, TRANS0)) - dot(vis, QM) --> ps_state = TRANS1',  # noqa
                     '0.5 * (dot(ps_task, F-DEC) + dot(ps_state, TRANS1)) - dot(vis, QM) --> ps_state = TRANS2',  # noqa
                     '0.5 * (dot(ps_task, F-DEC) + dot(ps_state, TRANS2)) - dot(vis, QM) --> ps_state = TRANS0']  # noqa

            else:
                count_action = []
                qa_action = []
                rvc_action = []
                fi_action = []

            decode_action = \
                ['dot(vis, QM) - 0.75 * dot(ps_task, W + C + V + F + L) --> ps_task = DEC, ps_state = ps_state, ps_dec = ps_dec',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, W)) --> ps_task = DEC, ps_state = ps_state, ps_dec = DECW',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, C)) --> ps_task = DEC, ps_state = ps_state, ps_dec = CNT',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, V + F)) --> ps_task = DEC, ps_state = ps_state, ps_dec = DECI',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, L)) --> ps_task = L, ps_state = LEARN, ps_dec = FWD',  # noqa
                 'dot(ps_task, DEC) - dot(ps_dec, CNT) - dot(vis, QM + A) --> ps_task = ps_task, ps_state = ps_state, ps_dec = ps_dec']  # noqa
            default_action = \
                []

            # List learning task spa actions first, so we know the precise
            # indicies of the learning task actions (i.e. the first N)
            all_actions = (learn_action + learn_state_action +
                           copy_draw_action + recog_action + mem_action +
                           count_action + qa_action + rvc_action + fi_action +
                           decode_action + default_action)

            actions = spa.Actions(*all_actions)
            model.bg = spa.BasalGanglia(actions=actions, input_synapse=0.008,
                                        label='Basal Ganglia')
            model.thal = spa.Thalamus(model.bg, subdim_channel=1,
                                      mutual_inhibit=1, route_inhibit=5.0,
                                      label='Thalamus')

        # ----- Set up connections (and save record of modules) -----
        if hasattr(model, 'vis'):
            model.vis.setup_connections(model)
        if hasattr(model, 'ps'):
            model.ps.setup_connections(model)
        if hasattr(model, 'bg'):
            if hasattr(model, 'reward'):
                with model.bg:
                    # Generate random biases for each learn action, so that
                    # there is some randomness to the initial action choice
                    bias_node = nengo.Node(1)
                    bias_ens = nengo.Ensemble(cfg.n_neurons_ens, 1,
                                              label='BG Bias Ensemble')
                    nengo.Connection(bias_node, bias_ens)

                    for i in range(len(learn_action)):
                        init_trfm = (np.random.random() *
                                     cfg.learn_init_trfm_max)
                        trfm_val = cfg.learn_init_trfm_bias + init_trfm
                        model.learn_conns.append(
                            nengo.Connection(bias_ens, model.bg.input[i],
                                             transform=trfm_val))
                        cfg.learn_init_transforms.append(trfm_val)
                logger.write("# learn_init_trfms: %s\n" %
                             (str(cfg.learn_init_transforms)))
        if hasattr(model, 'thal'):
            pass
        if hasattr(model, 'reward'):
            model.reward.setup_connections(model, model.learn_conns)
        if hasattr(model, 'enc'):
            model.enc.setup_connections(model)
        if hasattr(model, 'mem'):
            model.mem.setup_connections(model)
        if hasattr(model, 'trfm'):
            model.trfm.setup_connections(model)
        if hasattr(model, 'dec'):
            model.dec.setup_connections(model)
        if hasattr(model, 'mtr'):
            model.mtr.setup_connections(model)
        if hasattr(model, 'monitor'):
            model.monitor.setup_connections(model)

    return model
Example #19
0
def _test_sequence(Simulator, plt, seed, outfile, prune_passthrough):
    dimensions = 32
    subdimensions = 16
    T = 4.0
    seq_length = 6

    with spa.SPA(seed=seed) as model:
        model.state = spa.Memory(dimensions=dimensions,
                                 subdimensions=subdimensions)

        seq_actions = [
            'dot(state,A%d) --> state=A%d' % (i, (i + 1) % seq_length)
            for i in range(seq_length)
        ]

        model.bg = spa.BasalGanglia(spa.Actions(*seq_actions))
        model.thal = spa.Thalamus(model.bg)

        def stim_state(t):
            if t < 0.1:
                return 'A0'
            else:
                return '0'

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

        p_state = nengo.Probe(model.state.state.output, synapse=0.01)

        if 'spinnaker' in Simulator.__module__:
            nengo_spinnaker.add_spinnaker_params(model.config)
            model.config[
                model.input.input_nodes['state']].function_of_time = True

    vocab = model.get_input_vocab('state')

    if prune_passthrough:
        model = remove_passthrough_nodes(model)

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

    t = sim.trange()
    data = sim.data[p_state]
    ideal = np.array([vocab.parse('A%d' % i).v for i in range(seq_length)])
    dotp = np.dot(data, ideal.T)

    best = np.argmax(dotp, axis=1)
    delta = np.diff(best)
    indexes = np.where(delta != 0)
    # [:, 1:] ignores the first transition, which is meaningless
    delta_t = np.diff(indexes)[:, 1:] * 0.001

    mean = np.mean(delta_t)
    std = np.std(delta_t)

    outfile.write('"n_neurons": %d,\n' % sum(e.n_neurons
                                             for e in model.all_ensembles))
    outfile.write('"simtime": %f,\n' % T)
    outfile.write('"timing_mean": %f,\n' % mean)
    outfile.write('"timing_std": %f,\n' % std)

    figsize = (onecolumn, 4.0) if horizontal else (onecolumn * 2, 3.0)
    setup(figsize=figsize,
          palette_args={
              'palette': "cubehelix",
              'n_colors': 6
          })
    plt.plot(t[t < 1.0], dotp[t < 1.0])
    for transition in t[indexes[0]]:
        plt.axvline(transition, c='0.5', lw=1, ls=':')
    plt.ylabel('Similarity to item')
    plt.xlabel('Time (s)')
    plt.xlim(right=1.0)
    sns.despine()
    if prune_passthrough:
        plt.saveas = 'results-4.svg'
    else:
        plt.saveas = 'results-5.svg'

    if hasattr(sim, 'close'):
        sim.close()
def main():

    model = spa.SPA(label="Vector Storage")
    with model:

        # Dimensionality of each representation
        num_dimensions = 30
        sub_dimensions = 1

        # Create the vocabulary
        vocab = Vocabulary(num_dimensions, randomize=False)

        # Form the inputs manually by directly defining their vectors
        stored_value_1 = np.random.rand(num_dimensions) - [0.5
                                                           ] * num_dimensions
        stored_value_1 = [
            s / np.linalg.norm(stored_value_1) for s in stored_value_1
        ]
        vocab.add("Stored_value_1", stored_value_1)

        stored_value_2 = np.random.rand(num_dimensions) - [0.5
                                                           ] * num_dimensions
        stored_value_2 = [
            s / np.linalg.norm(stored_value_2) for s in stored_value_2
        ]
        vocab.add("Stored_value_2", stored_value_2)

        stored_value_3 = np.random.rand(num_dimensions) - [0.5
                                                           ] * num_dimensions
        stored_value_3 = [
            s / np.linalg.norm(stored_value_3) for s in stored_value_3
        ]
        vocab.add("Stored_value_3", stored_value_3)

        # Create a semantic pointer corresponding to the "correct" answer for the operation
        sum_vector = np.subtract(np.add(stored_value_1, stored_value_2),
                                 stored_value_3)
        sum_vector = sum_vector / np.linalg.norm(sum_vector)
        vocab.add("Sum", sum_vector)

        # Define the control signal inputs as random vectors
        r1 = [1] * num_dimensions
        r1 = r1 / np.linalg.norm(r1)
        r2 = [(-1)**k for k in range(num_dimensions)]
        r2 = r2 / np.linalg.norm(r2)
        vocab.add("Hold_signal", r1)
        vocab.add("Start_signal", r2)

        # Control when the vector operation takes place
        def control_input(t):
            if t < 1:
                return "Hold_signal"
            else:
                return "Start_signal"

        # inputs to the input word buffers
        def first_input(t):
            return "Stored_value_1"

        def second_input(t):
            return "Stored_value_2"

        def third_input(t):
            return "Stored_value_3"

        # Control buffer
        model.control = spa.Buffer(dimensions=num_dimensions,
                                   subdimensions=sub_dimensions,
                                   neurons_per_dimension=200,
                                   direct=True,
                                   vocab=vocab)
        control_probe = nengo.Probe(model.control.state.output)

        # Buffers to store the inputs: e.g., King, Man, Woman
        model.word_buffer1 = spa.Buffer(dimensions=num_dimensions,
                                        subdimensions=sub_dimensions,
                                        neurons_per_dimension=200,
                                        direct=True,
                                        vocab=vocab)
        model.word_buffer2 = spa.Buffer(dimensions=num_dimensions,
                                        subdimensions=sub_dimensions,
                                        neurons_per_dimension=200,
                                        direct=True,
                                        vocab=vocab)
        model.word_buffer3 = spa.Buffer(dimensions=num_dimensions,
                                        subdimensions=sub_dimensions,
                                        neurons_per_dimension=200,
                                        direct=True,
                                        vocab=vocab)

        # Probe to visualize the values stored in the buffers
        buffer_1_probe = nengo.Probe(model.word_buffer1.state.output)
        buffer_2_probe = nengo.Probe(model.word_buffer2.state.output)
        buffer_3_probe = nengo.Probe(model.word_buffer3.state.output)

        # Buffer to hold the result: e.g. Queen
        model.result = spa.Buffer(dimensions=num_dimensions,
                                  subdimensions=sub_dimensions,
                                  neurons_per_dimension=200,
                                  direct=True,
                                  vocab=vocab)
        result_probe = nengo.Probe(model.result.state.output)

        # Control system
        actions = spa.Actions(
            'dot(control, Start_signal) --> result = word_buffer1 + word_buffer2 - word_buffer3'
        )
        model.bg = spa.BasalGanglia(actions)
        model.thalamus = spa.Thalamus(model.bg, subdim_channel=sub_dimensions)

        # Connect up the inputs
        model.input = spa.Input(control=control_input,
                                word_buffer1=first_input,
                                word_buffer2=second_input,
                                word_buffer3=third_input)

    # Start the simulator
    sim = nengo.Simulator(model)

    # Dynamic plotting
    plt.ion()  # Dynamic updating of plots
    fig = plt.figure(figsize=(15, 8))
    plt.show()
    ax = fig.gca()
    ax.set_title("Vector Storage")

    while True:
        sim.run(1)  # Run for an additional 1 second
        plt.clf()  # Clear the figure
        plt.plot(sim.trange(),
                 similarity(sim.data, result_probe, vocab),
                 label="Buffer 3 Value")
        plt.legend(vocab.keys * 3, loc=2)
        plt.draw()  # Re-draw

        print sim.data[buffer_1_probe][-1]
        print sim.data[buffer_2_probe][-1]
        print sim.data[buffer_3_probe][-1]
        print sim.data[result_probe][-1]
        print "\n"

    plt.show()
Example #21
0
        e_store_result_and_update_count =   'dot(goal,COUNT) + letter_memory_status - .4 --> goal=COUNT, answer=%i*~NEXT*letter_memory, number_memory=6*IDENTITY*count' % store_weight, 
        f_store_count_and_compare =    'dot(goal,COUNT) + 1.5*number_memory_status - .4 --> goal=1.4*COMPARE_COUNT+.6*COUNT, count=%i*~NEXT*number_memory, comparison_cleanA=arg2, comparison_cleanB=~NEXT*number_memory' % store_weight,
        g_update_result =    'dot(goal,COMPARE_COUNT) - 2*comparison_status -.9 --> goal=COUNT, letter_memory=3*IDENTITY*answer',
        h_answer_counted =   'dot(goal,COMPARE_COUNT) + 2*comparison_status - .3 --> goal=1.4*COMPARE-COUNT',
        
        #Compare and respond
        v_compare =           'dot(goal,COMPARE) --> goal=(.8*COMPARE)+RESPOND, comparison2_cleanA=2*target, comparison2_cleanB=2*answer, do_learn=GO, in_layer=ITEM1*arg1+ITEM2*arg2+RESULT*answer, correct=ITEM1*arg1+ITEM2*arg2+RESULT*answer',
        w_respond_match =     'dot(goal,RESPOND) + comparison2_status - 0.3  --> goal=DONE-COMPARE, motor=YES',
        x_respond_mismatch =  'dot(goal,RESPOND) - comparison2_status - 0.3 --> goal=DONE-COMPARE, motor=NO',
        
        y_done =              'dot(goal,DONE) + dot(motor,YES+NO) - .5 --> goal=2*DONE',
        z_threshold = '0.1 -->'      
        )

    model.bg = spa.BasalGanglia(actions)
    model.thalamus = spa.Thalamus(model.bg, synapse_channel = .04)

    #to show BG/thalamus rules
    vocab_actions = spa.Vocabulary(model.bg.output.size_out)
    for i, action in enumerate(model.bg.actions.actions):
        vocab_actions.add(action.name.upper(), np.eye(model.bg.output.size_out)[i])
    model.actions = spa.State(model.bg.output.size_out,subdimensions=model.bg.output.size_out,
                          vocab=vocab_actions)
    nengo.Connection(model.thalamus.output, model.actions.input,synapse=None)

    for net in model.networks:
        if net.label is not None and net.label.startswith('channel'):
            net.label = ''
    

Example #22
0
 def __init__(self):
     self.scalar = spa.Buffer(dimensions=16, subdimensions=1)
     actions = spa.Actions('0.5 --> scalar=dot(scalar, FOO)')
     self.bg = spa.BasalGanglia(actions)
     self.thalamus = spa.Thalamus(self.bg)
Example #23
0
def create_model():

    #print trial_info
    print('---- INTIALIZING MODEL ----')
    global model

    model = spa.SPA()
    with model:

        #display current stimulus pair (not part of model)
        if nengo_gui_on and True:
            model.pair_input = nengo.Node(present_pair)
            model.pair_display = nengo.Node(
                display_func,
                size_in=model.pair_input.size_out)  # to show input
            nengo.Connection(model.pair_input,
                             model.pair_display,
                             synapse=None)

        # control
        model.control_net = nengo.Network()
        with model.control_net:
            #assuming the model knows which hand to use (which was blocked)
            model.hand_input = nengo.Node(get_hand)
            model.target_hand = spa.State(Dmid, vocab=vocab_motor, feedback=1)
            nengo.Connection(model.hand_input,
                             model.target_hand.input,
                             synapse=None)

            model.attend = spa.State(D, vocab=vocab_attend,
                                     feedback=.5)  # vocab_attend
            model.goal = spa.State(Dlow, vocab=vocab_goal,
                                   feedback=.7)  # current goal

        ### vision ###

        # set up network parameters
        n_vis = X_train.shape[1]  # nr of pixels, dimensions of network
        n_hid = 1000  # nr of gabor encoders/neurons

        # random state to start
        rng = np.random.RandomState(9)
        encoders = Gabor().generate(
            n_hid, (4, 4), rng=rng)  # gabor encoders, 11x11 apparently, why?
        encoders = Mask(
            (14, 90)).populate(encoders, rng=rng,
                               flatten=True)  # use them on part of the image

        model.visual_net = nengo.Network()
        with model.visual_net:

            #represent currently attended item
            model.attended_item = nengo.Node(present_item2, size_in=D)
            nengo.Connection(model.attend.output, model.attended_item)

            model.vision_gabor = nengo.Ensemble(
                n_hid,
                n_vis,
                eval_points=X_train,
                #    neuron_type=nengo.LIF(),
                neuron_type=nengo.AdaptiveLIF(
                    tau_n=.01, inc_n=.05
                ),  #to get a better fit, use more realistic neurons that adapt to input
                intercepts=nengo.dists.Uniform(-0.1, 0.1),
                #intercepts=nengo.dists.Choice([-0.5]), #should we comment this out? not sure what's happening
                #max_rates=nengo.dists.Choice([100]),
                encoders=encoders)
            #recurrent connection (time constant 500 ms)
            # strength = 1 - (100/500) = .8

            zeros = np.zeros_like(X_train)
            nengo.Connection(
                model.vision_gabor,
                model.vision_gabor,
                synapse=0.005,  #.1
                eval_points=np.vstack(
                    [X_train, zeros,
                     np.random.randn(*X_train.shape)]),
                transform=.5)

            model.visual_representation = nengo.Ensemble(n_hid,
                                                         dimensions=Dmid)

            model.visconn = nengo.Connection(
                model.vision_gabor,
                model.visual_representation,
                synapse=0.005,  #was .005
                eval_points=X_train,
                function=train_targets,
                solver=nengo.solvers.LstsqL2(reg=0.01))
            nengo.Connection(model.attended_item,
                             model.vision_gabor,
                             synapse=.02)  #.03) #synapse?

            # display attended item, only in gui
            if nengo_gui_on:
                # show what's being looked at
                model.display_attended = nengo.Node(
                    display_func,
                    size_in=model.attended_item.size_out)  # to show input
                nengo.Connection(model.attended_item,
                                 model.display_attended,
                                 synapse=None)
                #add node to plot total visual activity
                model.visual_activation = nengo.Node(None, size_in=1)
                nengo.Connection(model.vision_gabor.neurons,
                                 model.visual_activation,
                                 transform=np.ones((1, n_hid)),
                                 synapse=None)

        ### central cognition ###

        ##### Concepts #####
        model.concepts = spa.AssociativeMemory(
            vocab_all_words,  #vocab_concepts,
            wta_output=True,
            wta_inhibit_scale=1,  #was 1
            #default_output_key='NONE', #what to say if input doesn't match
            threshold=0.3
        )  # how strong does input need to be for it to recognize
        nengo.Connection(
            model.visual_representation,
            model.concepts.input,
            transform=.8 * vision_mapping
        )  #not too fast to concepts, might have to be increased to have model react faster to first word.

        #concepts accumulator
        model.concepts_evidence = spa.State(
            1, feedback=1, feedback_synapse=0.005
        )  #the lower the synapse, the faster it accumulates (was .1)
        concepts_evidence_scale = 2.5
        nengo.Connection(model.concepts.am.elem_output,
                         model.concepts_evidence.input,
                         transform=concepts_evidence_scale * np.ones(
                             (1, model.concepts.am.elem_output.size_out)),
                         synapse=0.005)

        #concepts switch
        model.do_concepts = spa.AssociativeMemory(vocab_reset,
                                                  default_output_key='CLEAR',
                                                  threshold=.2)
        nengo.Connection(
            model.do_concepts.am.ensembles[-1],
            model.concepts_evidence.all_ensembles[0].neurons,
            transform=np.ones(
                (model.concepts_evidence.all_ensembles[0].n_neurons, 1)) * -10,
            synapse=0.005)

        ###### Visual Representation ######
        model.vis_pair = spa.State(
            D, vocab=vocab_all_words, feedback=1.0, feedback_synapse=.05
        )  #was 2, 1.6 works ok, but everything gets activated.

        ##### Familiarity #####

        # Assoc Mem with Learned Words
        # - familiarity signal should be continuous over all items, so no wta
        model.dm_learned_words = spa.AssociativeMemory(vocab_learned_words,
                                                       threshold=.2)
        nengo.Connection(model.dm_learned_words.output,
                         model.dm_learned_words.input,
                         transform=.4,
                         synapse=.02)

        # Familiarity Accumulator

        model.familiarity = spa.State(
            1, feedback=.9,
            feedback_synapse=0.1)  #fb syn influences speed of acc
        #familiarity_scale = 0.2 #keep stable for negative fam

        # familiarity accumulator switch
        model.do_fam = spa.AssociativeMemory(vocab_reset,
                                             default_output_key='CLEAR',
                                             threshold=.2)
        # reset
        nengo.Connection(
            model.do_fam.am.ensembles[-1],
            model.familiarity.all_ensembles[0].neurons,
            transform=np.ones(
                (model.familiarity.all_ensembles[0].n_neurons, 1)) * -10,
            synapse=0.005)

        #first a sum to represent summed similarity
        model.summed_similarity = nengo.Ensemble(n_neurons=100, dimensions=1)
        nengo.Connection(
            model.dm_learned_words.am.elem_output,
            model.summed_similarity,
            transform=np.ones(
                (1,
                 model.dm_learned_words.am.elem_output.size_out)))  #take sum

        #then a connection to accumulate this summed sim
        def familiarity_acc_transform(summed_sim):
            fam_scale = .5
            fam_threshold = 0  #actually, kind of bias
            fam_max = 1
            return fam_scale * (2 * ((summed_sim - fam_threshold) /
                                     (fam_max - fam_threshold)) - 1)

        nengo.Connection(model.summed_similarity,
                         model.familiarity.input,
                         function=familiarity_acc_transform)

        ##### Recollection & Representation #####

        model.dm_pairs = spa.AssociativeMemory(
            vocab_learned_pairs, wta_output=True)  #input_keys=list_of_pairs
        nengo.Connection(model.dm_pairs.output,
                         model.dm_pairs.input,
                         transform=.5,
                         synapse=.05)

        #representation
        rep_scale = 0.5
        model.representation = spa.State(D,
                                         vocab=vocab_all_words,
                                         feedback=1.0)
        model.rep_filled = spa.State(
            1, feedback=.9,
            feedback_synapse=.1)  #fb syn influences speed of acc
        model.do_rep = spa.AssociativeMemory(vocab_reset,
                                             default_output_key='CLEAR',
                                             threshold=.2)
        nengo.Connection(
            model.do_rep.am.ensembles[-1],
            model.rep_filled.all_ensembles[0].neurons,
            transform=np.ones(
                (model.rep_filled.all_ensembles[0].n_neurons, 1)) * -10,
            synapse=0.005)

        nengo.Connection(model.representation.output,
                         model.rep_filled.input,
                         transform=rep_scale *
                         np.reshape(sum(vocab_learned_pairs.vectors),
                                    ((1, D))))

        ###### Comparison #####

        model.comparison = spa.Compare(D,
                                       vocab=vocab_all_words,
                                       neurons_per_multiply=500,
                                       input_magnitude=.3)

        #turns out comparison is not an accumulator - we also need one of those.
        model.comparison_accumulator = spa.State(
            1, feedback=.9,
            feedback_synapse=0.05)  #fb syn influences speed of acc
        model.do_compare = spa.AssociativeMemory(vocab_reset,
                                                 default_output_key='CLEAR',
                                                 threshold=.2)

        #reset
        nengo.Connection(
            model.do_compare.am.ensembles[-1],
            model.comparison_accumulator.all_ensembles[0].neurons,
            transform=np.ones(
                (model.comparison_accumulator.all_ensembles[0].n_neurons, 1)) *
            -10,
            synapse=0.005)

        #error because we apply a function to a 'passthrough' node, inbetween ensemble as a solution:
        model.comparison_result = nengo.Ensemble(n_neurons=100, dimensions=1)
        nengo.Connection(model.comparison.output, model.comparison_result)

        def comparison_acc_transform(comparison):
            comparison_scale = .6
            comparison_threshold = 0  #actually, kind of bias
            comparison_max = .6
            return comparison_scale * (2 * (
                (comparison - comparison_threshold) /
                (comparison_max - comparison_threshold)) - 1)

        nengo.Connection(model.comparison_result,
                         model.comparison_accumulator.input,
                         function=comparison_acc_transform)

        #motor
        model.motor_net = nengo.Network()
        with model.motor_net:

            #input multiplier
            model.motor_input = spa.State(Dmid, vocab=vocab_motor)

            #higher motor area (SMA?)
            model.motor = spa.State(Dmid, vocab=vocab_motor, feedback=.7)

            #connect input multiplier with higher motor area
            nengo.Connection(model.motor_input.output,
                             model.motor.input,
                             synapse=.1,
                             transform=2)

            #finger area
            model.fingers = spa.AssociativeMemory(
                vocab_fingers,
                input_keys=['L1', 'L2', 'R1', 'R2'],
                wta_output=True)
            nengo.Connection(model.fingers.output,
                             model.fingers.input,
                             synapse=0.1,
                             transform=0.3)  #feedback

            #conncetion between higher order area (hand, finger), to lower area
            nengo.Connection(model.motor.output,
                             model.fingers.input,
                             transform=.25 * motor_mapping)  #was .2

            #finger position (spinal?)
            model.finger_pos = nengo.networks.EnsembleArray(n_neurons=50,
                                                            n_ensembles=4)
            nengo.Connection(model.finger_pos.output,
                             model.finger_pos.input,
                             synapse=0.1,
                             transform=0.8)  #feedback

            #connection between finger area and finger position
            nengo.Connection(model.fingers.am.elem_output,
                             model.finger_pos.input,
                             transform=1.0 *
                             np.diag([0.55, .54, .56, .55]))  #fix these

        model.bg = spa.BasalGanglia(
            spa.Actions(
                #wait & start
                a_aa_wait='dot(goal,WAIT) - .9 --> goal=0',
                a_attend_item1=
                'dot(goal,DO_TASK) - .0 --> goal=RECOG, attend=ITEM1, do_concepts=GO',

                #attend words
                b_attending_item1=
                'dot(goal,RECOG) + dot(attend,ITEM1) - concepts_evidence - .3 --> goal=RECOG, attend=ITEM1, do_concepts=GO',  # vis_pair=2.5*(ITEM1*concepts)',
                c_attend_item2=
                'dot(goal,RECOG) + dot(attend,ITEM1) + concepts_evidence - 1.6 --> goal=RECOG2, attend=ITEM2, vis_pair=3*(ITEM1*concepts)',
                d_attending_item2=
                'dot(goal,RECOG2+RECOG) + dot(attend,ITEM2) - concepts_evidence - .4 --> goal=RECOG2, attend=ITEM2, do_concepts=GO, dm_learned_words=1.0*(~ITEM1*vis_pair)',  #vis_pair=1.2*(ITEM2*concepts)
                e_start_familiarity=
                'dot(goal,RECOG2) + dot(attend,ITEM2) + concepts_evidence - 1.8 --> goal=FAMILIARITY, do_fam=GO, vis_pair=1.9*(ITEM2*concepts), dm_learned_words=2.0*(~ITEM1*vis_pair+~ITEM2*vis_pair)',

                #judge familiarity
                f_accumulate_familiarity=
                '1.1*dot(goal,FAMILIARITY) - 0.2 --> goal=FAMILIARITY-RECOG2, do_fam=GO, dm_learned_words=.8*(~ITEM1*vis_pair+~ITEM2*vis_pair)',
                g_respond_unfamiliar=
                'dot(goal,FAMILIARITY) - familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RESPOND_MISMATCH-FAMILIARITY, do_fam=GO, motor_input=1.6*(target_hand+MIDDLE)',
                #g2_respond_familiar =   'dot(goal,FAMILIARITY) + familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RESPOND, do_fam=GO, motor_input=1.6*(target_hand+INDEX)',

                #recollection & representation
                h_recollection=
                'dot(goal,FAMILIARITY) + familiarity - .5*dot(fingers,L1+L2+R1+R2) - .6 --> goal=RECOLLECTION-FAMILIARITY, dm_pairs = vis_pair',
                i_representation=
                'dot(goal,RECOLLECTION) - rep_filled - .1 --> goal=RECOLLECTION, dm_pairs = vis_pair, representation=3*dm_pairs, do_rep=GO',

                #comparison & respond
                j_10_compare_word1=
                'dot(goal,RECOLLECTION+1.4*COMPARE_ITEM1) + rep_filled - .9 --> goal=COMPARE_ITEM1-RECOLLECTION, do_rep=GO, do_compare=GO, comparison_A = ~ITEM1*vis_pair, comparison_B = ~ITEM1*representation',
                k_11_match_word1=
                'dot(goal,COMPARE_ITEM1) + comparison_accumulator - .7 --> goal=COMPARE_ITEM2-COMPARE_ITEM1, do_rep=GO, comparison_A = ~ITEM1*vis_pair, comparison_B = ~ITEM1*representation',
                l_12_mismatch_word1=
                'dot(goal,COMPARE_ITEM1) + .4 * dot(goal,RESPOND_MISMATCH) - comparison_accumulator - .7 --> goal=RESPOND_MISMATCH-COMPARE_ITEM1, do_rep=GO, motor_input=1.6*(target_hand+MIDDLE), do_compare=GO, comparison_A = ~ITEM1*vis_pair, comparison_B = ~ITEM1*representation',
                compare_word2=
                'dot(goal,COMPARE_ITEM2) - .5 --> goal=COMPARE_ITEM2, do_compare=GO, comparison_A = ~ITEM2*vis_pair, comparison_B = ~ITEM2*representation',
                m_match_word2=
                'dot(goal,COMPARE_ITEM2) + comparison_accumulator - .7 --> goal=RESPOND_MATCH-COMPARE_ITEM2, motor_input=1.6*(target_hand+INDEX), do_compare=GO, comparison_A = ~ITEM2*vis_pair, comparison_B = ~ITEM2*representation',
                n_mismatch_word2=
                'dot(goal,COMPARE_ITEM2) - comparison_accumulator - dot(fingers,L1+L2+R1+R2)- .7 --> goal=RESPOND_MISMATCH-COMPARE_ITEM2, motor_input=1.6*(target_hand+MIDDLE),do_compare=GO, comparison_A = ~ITEM2*vis_pair, comparison_B = ~ITEM2*representation',

                #respond
                o_respond_match=
                'dot(goal,RESPOND_MATCH) - .1 --> goal=RESPOND_MATCH, motor_input=1.6*(target_hand+INDEX)',
                p_respond_mismatch=
                'dot(goal,RESPOND_MISMATCH) - .1 --> goal=RESPOND_MISMATCH, motor_input=1.6*(target_hand+MIDDLE)',

                #finish
                x_response_done=
                'dot(goal,RESPOND_MATCH) + dot(goal,RESPOND_MISMATCH) + 2*dot(fingers,L1+L2+R1+R2) - .7 --> goal=2*END',
                y_end=
                'dot(goal,END)-.1 --> goal=END-RESPOND_MATCH-RESPOND_MISMATCH',
                z_threshold='.05 --> goal=0'

                #possible to match complete buffer, ie is representation filled?
                # motor_input=1.5*target_hand+MIDDLE,
            ))

        print(model.bg.actions.count)
        #print(model.bg.dimensions)

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

        model.cortical = spa.Cortical(  # cortical connection: shorthand for doing everything with states and connections
            spa.Actions(
                #  'motor_input = .04*target_hand',
                # 'dm_learned_words = .1*vis_pair',
                #'dm_pairs = 2*stimulus'
                #'vis_pair = 2*attend*concepts+concepts',
                #fam 'comparison_A = 2*vis_pair',
                #fam 'comparison_B = 2*representation*~attend',
            ))

        #probes
        model.pr_motor_pos = nengo.Probe(
            model.finger_pos.output,
            synapse=.01)  #raw vector (dimensions x time)
        model.pr_motor = nengo.Probe(model.fingers.output, synapse=.01)
        #model.pr_motor1 = nengo.Probe(model.motor.output, synapse=.01)

        if not nengo_gui_on:
            model.pr_vision_gabor = nengo.Probe(
                model.vision_gabor.neurons, synapse=.005
            )  #do we need synapse, or should we do something with the spikes
            model.pr_familiarity = nengo.Probe(
                model.dm_learned_words.am.elem_output,
                synapse=.01)  #element output, don't include default
            model.pr_concepts = nengo.Probe(
                model.concepts.am.elem_output,
                synapse=.01)  # element output, don't include default

        #multiply spikes with the connection weights

        #input
        model.input = spa.Input(goal=goal_func)

        #print(sum(ens.n_neurons for ens in model.all_ensembles))

        #return model

        #to show select BG rules
        # get names rules
        if nengo_gui_on:
            vocab_actions = spa.Vocabulary(model.bg.output.size_out)
            for i, action in enumerate(model.bg.actions.actions):
                vocab_actions.add(action.name.upper(),
                                  np.eye(model.bg.output.size_out)[i])
            model.actions = spa.State(model.bg.output.size_out,
                                      subdimensions=model.bg.output.size_out,
                                      vocab=vocab_actions)
            nengo.Connection(model.thalamus.output, model.actions.input)

            for net in model.networks:
                if net.label is not None and net.label.startswith('channel'):
                    net.label = ''
Example #24
0
    def run(self):
        n_neurons = 40
        subdim = 16
        direct = False
        output = self.stimuli.output  # vector output from coherence score
        threshold = self.stimuli.threshold  # for correct answer on inf task
        g_transform = np.transpose(np.ones((1, self.dimensions)))  # mem gate

        model = spa.SPA(label='ConceptModel', seed=self.seed)

        with model:

            # Vision Component
            model.vision = spa.Buffer(dimensions=self.dimensions,
                                      subdimensions=subdim,
                                      neurons_per_dimension=n_neurons,
                                      vocab=self.main_voc,
                                      direct=direct)

            # Memory Component
            model.sp_mem = spa.Buffer(dimensions=self.dimensions,
                                      subdimensions=subdim,
                                      neurons_per_dimension=n_neurons,
                                      vocab=self.main_voc,
                                      direct=direct)
            model.context_mem = spa.Memory(dimensions=self.dimensions,
                                           subdimensions=subdim,
                                           neurons_per_dimension=n_neurons,
                                           vocab=self.main_voc,
                                           tau=0.01 / 0.3,
                                           direct=direct)

            # Inferential Evaluation Subsystem
            model.inference = spa.Buffer(dimensions=self.dimensions,
                                         subdimensions=subdim,
                                         neurons_per_dimension=n_neurons,
                                         vocab=self.feat_voc,
                                         direct=direct)
            model.score = spa.Memory(dimensions=1,
                                     neurons_per_dimension=3000,
                                     synapse=0.05,
                                     direct=direct)
            model.gatesignal = spa.Memory(dimensions=1,
                                          neurons_per_dimension=500,
                                          synapse=0.05,
                                          direct=direct)
            model.cleanup1 = spa.AssociativeMemory(
                input_vocab=self.feat_voc,
                output_vocab=self.weight_voc,
                n_neurons_per_ensemble=250,
                threshold=0.25)

            # Perceptual Evaluation Subsystem
            model.cleanup2 = spa.AssociativeMemory(input_vocab=self.label_voc,
                                                   output_vocab=self.label_voc,
                                                   n_neurons_per_ensemble=1000,
                                                   threshold=threshold)

            # Shared Component
            model.decision = spa.Memory(dimensions=self.dimensions,
                                        subdimensions=subdim,
                                        neurons_per_dimension=n_neurons,
                                        vocab=self.label_voc,
                                        tau=0.01 / 0.3,
                                        synapse=0.05,
                                        direct=direct)

            # Motor Component
            model.motor = spa.Memory(dimensions=self.dimensions,
                                     subdimensions=subdim,
                                     neurons_per_dimension=n_neurons,
                                     vocab=self.label_voc,
                                     synapse=0.1,
                                     direct=direct)

            # Convenient probing of rule applications
            if self.raster:
                model.apps = spa.Buffer(dimensions=self.dimensions,
                                        subdimensions=subdim,
                                        neurons_per_dimension=n_neurons,
                                        vocab=self.feat_voc,
                                        direct=direct)

                self.pApps = nengo.Probe(model.apps.state.output, synapse=0.03)
                self.appSpikes = nengo.Probe(model.apps.state.ea_ensembles[3],
                                             'spikes')

                nengo.Connection(model.cleanup1.output, model.apps.state.input)

            # Action definitions
            actions = spa.Actions(
                'dot(vision, POSNER) --> context_mem=VIS',
                'dot(vision, BROOKS) --> context_mem=VIS',
                'dot(vision, MURPHY) --> context_mem=INFER',
                'dot(context_mem, VIS)   --> gatesignal=2, context_mem=R5',
                'dot(context_mem, INFER) --> context_mem=R1',
                'dot(context_mem, R1)    --> inference=sp_mem*~R1, context_mem=R2',
                'dot(context_mem, R2)    --> inference=sp_mem*~R2, context_mem=R3',
                'dot(context_mem, R3)    --> inference=sp_mem*~R3, context_mem=R4',
                'dot(context_mem, R4)    --> inference=sp_mem*~R4, context_mem=R5',
                'dot(context_mem, R5)    --> motor=decision')

            # Basal ganglia and thalamus
            model.bg = spa.BasalGanglia(actions)
            model.thal = spa.Thalamus(model.bg)

            # Subnetworks defined outside of SPA
            model.product = Product(600, self.dimensions)
            model.vis_gate = Product(300, self.dimensions)
            model.mem_gate = Product(300, self.dimensions)
            model.conv = CircularConvolution(250,
                                             self.dimensions,
                                             invert_a=True)

            # Connections for gate with memory for perceptual evaluation tasks
            nengo.Connection(model.vision.state.output, model.vis_gate.B)
            nengo.Connection(model.gatesignal.state.output,
                             model.vis_gate.A,
                             transform=g_transform)
            nengo.Connection(model.vis_gate.output, model.conv.A)
            nengo.Connection(model.sp_mem.state.output, model.mem_gate.B)
            nengo.Connection(model.gatesignal.state.output,
                             model.mem_gate.A,
                             transform=g_transform)
            nengo.Connection(model.mem_gate.output, model.conv.B)
            nengo.Connection(model.conv.output, model.decision.state.input)

            # Connections for inferential evaluation tasks
            nengo.Connection(model.inference.state.output,
                             model.cleanup1.input)
            nengo.Connection(model.cleanup1.output, model.product.A)
            nengo.Connection(model.vision.state.output, model.product.B)
            nengo.Connection(model.cleanup2.output, model.decision.state.input)
            nengo.Connection(model.product.output,
                             model.score.state.input,
                             transform=model.product.dot_product_transform())
            nengo.Connection(model.score.state.output,
                             model.cleanup2.input,
                             transform=np.transpose(output))

            # Input to visual buffer
            def vision(t):
                if t < 0.08: return self.stimuli.task
                if 0.0801 <= t < 1: return self.probe
                else: return '0'

            model.input = spa.Input(vision=vision, sp_mem=self.stimuli.memory)

            # Inhibit the gate with inference actions
            actions = [2, 4, 5, 6, 7, 8]
            target = model.gatesignal
            for action in actions:
                for e in target.all_ensembles:
                    nengo.Connection(model.thal.actions.ensembles[action],
                                     e.neurons,
                                     transform=[[-2]] * e.n_neurons)

            # Set radius for ensemble computing scalar coherence score.
            for ens in model.score.state.ensembles:
                ens.radius = 2

            # Define probes for semantic pointer plots
            self.pVision = nengo.Probe(model.vision.state.output, synapse=0.03)
            self.pMotor = nengo.Probe(model.motor.state.output, synapse=0.03)
            self.pMemory = nengo.Probe(model.sp_mem.state.output, synapse=0.03)
            self.pContext = nengo.Probe(model.context_mem.state.output,
                                        synapse=0.03)
            self.pScore = nengo.Probe(model.score.state.output, synapse=0.03)
            self.pDecision = nengo.Probe(model.decision.state.output,
                                         synapse=0.03)
            self.pActions = nengo.Probe(model.thal.actions.output,
                                        synapse=0.01)
            self.pUtility = nengo.Probe(model.bg.input, synapse=0.01)

            # Define probes for spike rasters
            self.visSpikes = nengo.Probe(model.vision.state.ea_ensembles[3],
                                         'spikes')
            self.conSpikes = nengo.Probe(
                model.context_mem.state.ea_ensembles[5], 'spikes')
            self.memSpikes = nengo.Probe(model.sp_mem.state.ea_ensembles[7],
                                         'spikes')
            self.motSpikes = nengo.Probe(model.motor.state.ea_ensembles[1],
                                         'spikes')
            self.scoSpikes = nengo.Probe(model.score.state.ea_ensembles[0],
                                         'spikes')

        # Run the model
        sim = nengo.Simulator(model)
        sim.run(0.45)

        # Save graph if plotting chosen
        if self.raster:
            graph = Plotter(self, sim)
            if self.stimuli.task == 'MURPHY':
                graph.plot_spikes_inf()
            else:
                graph.plot_spikes_vis()

        # Assess correctness of output
        self.measure_output(sim)
def create_model(
        trial_info=('Target', 1, 'Short', 'CARGO', 'HOOD'), hand='LEFT',
        seedin=1):

    initialize_vocabs()
    print trial_info

    global global_item1
    global global_item2

    global_item1 = trial_info[3]
    global_item2 = trial_info[4]

    global model
    model = spa.SPA(seed=seedin)
    with model:

        #display current stimulus pair
        model.pair_input = nengo.Node(present_pair)
        model.pair_display = nengo.Node(
            display_func, size_in=model.pair_input.size_out)  # to show input
        nengo.Connection(model.pair_input, model.pair_display, synapse=None)

        #visual
        model.visual_net = nengo.Network()
        with model.visual_net:
            if not extended_visual:
                model.stimulus = spa.State(D, vocab=vocab_concepts, feedback=1)
                model.stim = spa.Input(stimulus=present_item_simple)
            else:

                #represent currently attended item
                model.attended_item = nengo.Node(present_item)
                model.vision_process = nengo.Ensemble(
                    n_hid,
                    n_vis,
                    eval_points=X_train,
                    neuron_type=nengo.LIFRate(),
                    intercepts=nengo.dists.Choice([-0.5
                                                   ]),  #can switch these off
                    max_rates=nengo.dists.Choice([100]),  # why?
                    encoders=encoders)
                #  1000 neurons, nrofpix = dimensions
                # visual_representation = nengo.Node(size_in=Dmid) #output, in this case 466 outputs
                model.visual_representation = nengo.Ensemble(
                    n_hid, dimensions=Dmid)  # output, in this case 466 outputs

                model.visconn = nengo.Connection(
                    model.vision_process,
                    model.visual_representation,
                    synapse=0.005,
                    eval_points=X_train,
                    function=train_targets,
                    solver=nengo.solvers.LstsqL2(reg=0.01))
                nengo.Connection(model.attended_item,
                                 model.vision_process,
                                 synapse=None)

                # display attended item
                model.display_node = nengo.Node(
                    display_func,
                    size_in=model.attended_item.size_out)  # to show input
                nengo.Connection(model.attended_item,
                                 model.display_node,
                                 synapse=None)

        #control
        model.control_net = nengo.Network()
        with model.control_net:
            model.attend = spa.State(
                D, vocab=vocab_concepts,
                feedback=.5)  #if attend item, goes to concepts
            model.goal = spa.State(D, vocab_goal, feedback=1)  #current goal
            model.target_hand = spa.State(Dmid, vocab=vocab_motor, feedback=1)

        # concepts
        model.concepts = spa.AssociativeMemory(vocab_concepts,
                                               wta_output=True,
                                               wta_inhibit_scale=1)
        if not extended_visual:
            nengo.Connection(model.stimulus.output, model.concepts.input)
        else:
            nengo.Connection(model.visual_representation,
                             model.concepts.input,
                             transform=vision_mapping)

        # pair representation
        model.vis_pair = spa.State(D, vocab=vocab_concepts, feedback=1)

        model.dm_items = spa.AssociativeMemory(
            vocab_items
        )  #familiarity should be continuous over all items, so no wta
        nengo.Connection(model.dm_items.output,
                         model.dm_items.input,
                         transform=.5,
                         synapse=.01)

        model.familiarity = spa.State(
            1, feedback_synapse=.01)  #no fb syn specified
        nengo.Connection(
            model.dm_items.am.elem_output,
            model.familiarity.input,  #am.element_output == all outputs, we sum
            transform=.8 * np.ones(
                (1, model.dm_items.am.elem_output.size_out)))

        #model.dm_pairs = spa.AssociativeMemory(vocab_concepts, input_keys=list_of_pairs,wta_output=True)
        #nengo.Connection(model.dm_items.output,model.dm_pairs.input)

        #motor
        model.motor_net = nengo.Network()
        with model.motor_net:

            #input multiplier
            model.motor_input = spa.State(Dmid, vocab=vocab_motor)

            #higher motor area (SMA?)
            model.motor = spa.State(Dmid, vocab=vocab_motor, feedback=1)

            #connect input multiplier with higher motor area
            nengo.Connection(model.motor_input.output,
                             model.motor.input,
                             synapse=.1,
                             transform=10)

            #finger area
            model.fingers = spa.AssociativeMemory(
                vocab_fingers,
                input_keys=['L1', 'L2', 'R1', 'R2'],
                wta_output=True)

            #conncetion between higher order area (hand, finger), to lower area
            nengo.Connection(model.motor.output,
                             model.fingers.input,
                             transform=.4 * motor_mapping)

            #finger position (spinal?)
            model.finger_pos = nengo.networks.EnsembleArray(n_neurons=50,
                                                            n_ensembles=4)
            nengo.Connection(model.finger_pos.output,
                             model.finger_pos.input,
                             synapse=0.1,
                             transform=0.3)  #feedback

            #connection between finger area and finger position
            nengo.Connection(model.fingers.am.elem_output,
                             model.finger_pos.input,
                             transform=np.diag([0.55, .53, .57,
                                                .55]))  #fix these

        model.bg = spa.BasalGanglia(
            spa.Actions(
                'dot(goal,DO_TASK)-.5 --> dm_items=vis_pair, goal=RECOG, attend=ITEM1',
                'dot(goal,RECOG)+dot(attend,ITEM1)+familiarity-2 --> goal=RECOG2, dm_items=vis_pair, attend=ITEM2',
                'dot(goal,RECOG)+dot(attend,ITEM1)+(1-familiarity)-2 --> goal=RECOG2, attend=ITEM2',  #motor_input=1.5*target_hand+MIDDLE,
                'dot(goal,RECOG2)+dot(attend,ITEM2)+familiarity-1.3 --> goal=RESPOND, dm_items=vis_pair,motor_input=1.2*target_hand+INDEX, attend=ITEM2',
                'dot(goal,RECOG2)+dot(attend,ITEM2)+(1-familiarity)-1.3 --> goal=RESPOND, motor_input=1.5*target_hand+MIDDLE, attend=ITEM2',
                'dot(goal,RESPOND)+dot(motor,MIDDLE+INDEX)-1.4 --> goal=END',
                'dot(goal,END) --> goal=END',
                #'.6 -->',
            ))
        model.thalamus = spa.Thalamus(model.bg)

        model.cortical = spa.Cortical(  # cortical connection: shorthand for doing everything with states and connections
            spa.Actions(
                #  'motor_input = .04*target_hand',
                #'dm_items = .8*concepts', #.5
                #'dm_pairs = 2*stimulus'
                'vis_pair = attend*concepts+concepts', ))

        #probes
        #model.pr_goal = nengo.Probe(model.goal.output,synapse=.01) #sample_every=.01 seconds, etc...
        model.pr_motor_pos = nengo.Probe(
            model.finger_pos.output,
            synapse=.01)  #raw vector (dimensions x time)
        model.pr_motor = nengo.Probe(model.fingers.output, synapse=.01)
        model.pr_motor1 = nengo.Probe(model.motor.output, synapse=.01)
        #model.pr_target = nengo.Probe(model.target_hand.output, synapse=.01)

        #input
        model.input = spa.Input(
            goal=lambda t: 'DO_TASK' if t < 0.05 else '0',
            target_hand=hand,
            #attend=lambda t: 'ITEM1' if t < 0.1 else 'ITEM2',
        )
Example #26
0
def main():

    print "Loading Word2Vec model..."
    word2vec_model = gensim.models.Word2Vec.load("word2vec_model_1_cleaned")
    word2vec_model.init_sims(replace=True)
    word2vec_vocab = word2vec_model.index2word

    import readline
    readline.parse_and_bind("tab: complete")

    def complete(text, state):
        results = [x for x in word2vec_vocab if x.startswith(text)] + [None]
        return results[state]

    readline.set_completer(complete)

    print "This program uses an SPA network in Nengo to perform vector operations on a semantically structured word-vector space *learned* from a sentence corpus."
    print "When trained on a large corpus of English sentences, for example, it should produce: Vector[king] - Vector[man] + Vector[woman] = Vector[king]"

    print "For now, it just does subtraction..."
    print "\nPress <tab> twice to see all your autocomplete options."
    print "_______________________________________________________"
    line1 = raw_input('\nFirst word:> ')
    line2 = raw_input('\nSecond word:> ')

    if line1 and line2 in word2vec_vocab:
        val1 = word2vec_model[line1]
        val2 = word2vec_model[line2]
        diff = val1 - val2
        dot_products = [
            np.dot(word2vec_model[word2vec_model.index2word[i]], diff)
            for i in range(len(word2vec_model.index2word))
        ]
        closest_word = word2vec_model.index2word[dot_products.index(
            max(dot_products))]
        print "\nWhat the Nengo model SHOULD return is something like: %s" % closest_word

    print "\nDefining SPA network..."
    model = spa.SPA(label="Vector Storage")
    with model:

        # Dimensionality of each representation
        num_dimensions = 100
        sub_dimensions = 1

        # Create the vocabulary
        vocab = Vocabulary(num_dimensions, randomize=False)

        stored_value_1 = val1
        vocab.add("Stored_value_1", stored_value_1)

        stored_value_2 = val2
        vocab.add("Stored_value_2", stored_value_2)

        # Create a semantic pointer corresponding to the "correct" answer for the operation
        sum_vector = np.subtract(stored_value_1, stored_value_2)
        sum_vector = sum_vector / np.linalg.norm(sum_vector)
        vocab.add("Correct_target", sum_vector)

        # Define the control signal inputs as random vectors
        r1 = [1] * num_dimensions
        r1 = r1 / np.linalg.norm(r1)
        r2 = [(-1)**k for k in range(num_dimensions)]
        r2 = r2 / np.linalg.norm(r2)
        vocab.add("Hold_signal", r1)
        vocab.add("Start_signal", r2)

        # Control when the vector operation takes place
        def control_input(t):
            if t < 1:
                return "Hold_signal"
            else:
                return "Start_signal"

        # Control buffer
        model.control = spa.Buffer(dimensions=num_dimensions,
                                   subdimensions=sub_dimensions,
                                   neurons_per_dimension=200,
                                   direct=True,
                                   vocab=vocab)
        control_probe = nengo.Probe(model.control.state.output)

        # Inputs to the word input buffers
        def first_input(t):
            return "Stored_value_1"

        def second_input(t):
            return "Stored_value_2"

        # Buffers to store the inputs:
        model.word_buffer1 = spa.Buffer(dimensions=num_dimensions,
                                        subdimensions=sub_dimensions,
                                        neurons_per_dimension=200,
                                        direct=True,
                                        vocab=vocab)
        model.word_buffer2 = spa.Buffer(dimensions=num_dimensions,
                                        subdimensions=sub_dimensions,
                                        neurons_per_dimension=200,
                                        direct=True,
                                        vocab=vocab)

        # Probe to visualize the values stored in the buffers
        buffer_1_probe = nengo.Probe(model.word_buffer1.state.output)
        buffer_2_probe = nengo.Probe(model.word_buffer2.state.output)

        # Buffer to hold the result:
        model.result = spa.Buffer(dimensions=num_dimensions,
                                  subdimensions=sub_dimensions,
                                  neurons_per_dimension=200,
                                  direct=True,
                                  vocab=vocab)
        result_probe = nengo.Probe(model.result.state.output)

        # Control system
        actions = spa.Actions(
            'dot(control, Start_signal) --> result = word_buffer1 - word_buffer2',
            'dot(control, Hold_signal) --> result = Hold_signal')
        model.bg = spa.BasalGanglia(actions)
        model.thalamus = spa.Thalamus(model.bg, subdim_channel=sub_dimensions)

        # Connect up the inputs
        model.input = spa.Input(control=control_input,
                                word_buffer1=first_input,
                                word_buffer2=second_input)

    # Start the simulator
    sim = nengo.Simulator(model)

    # Dynamic plotting
    plt.ion()  # Dynamic updating of plots
    fig = plt.figure(figsize=(15, 8))
    plt.show()
    ax = fig.gca()
    ax.set_title("Vector Storage")

    while True:
        sim.run(0.5)  # Run for an additional 1 second
        plt.clf()  # Clear the figure
        plt.plot(sim.trange(),
                 similarity(sim.data, result_probe, vocab),
                 label="Buffer 3 Value")
        legend_symbols = vocab.keys
        plt.legend(legend_symbols, loc=2)
        plt.draw()  # Re-draw

        # Go back to our manually-stored vocabulary and see how well it did
        diff = sim.data[result_probe][-1]
        dot_products = [
            np.dot(word2vec_model[word2vec_model.index2word[i]], diff)
            for i in range(len(word2vec_model.index2word))
        ]
        closest_word = word2vec_model.index2word[dot_products.index(
            max(dot_products))]
        print "Time: %f" % sim.trange()[-1]
        print "\nWhat the Nengo model DID return is something like: %s" % closest_word
        print "\n"

    plt.show()
Example #27
0
File: sim.py Project: Zarchan/bg
def simulate(lg=.2, le=.2, save_dir=None, sim_time=6):
    nengo.rc.set('progress', 'progress_bar',
                 'nengo.utils.progress.TerminalProgressBar')
    d = 64
    n = 50 * d

    # class for delayed connection
    delay_t = 0.2  # 200 ms pro Silbe

    dt = 0.001
    delay = Delay(dimensions=d, timesteps=int(delay_t / dt))

    # SPA-Model:
    model = spa.SPA()
    with model:
        # --------------------- SPA representations ----------------

        # High level SPs
        model.visual = spa.Buffer(dimensions=d)
        model.phonemic = spa.Buffer(dimensions=d)
        model.premotor = spa.Buffer(dimensions=d)
        model.audiexpect = spa.Buffer(dimensions=d)
        model.motor = spa.Buffer(dimensions=d)
        model.motor1 = nengo.Ensemble(n, dimensions=d)
        model.motor2 = nengo.Ensemble(n, dimensions=d)
        model.somato = spa.Buffer(dimensions=d)

        model.delaynode = nengo.Node(delay.step, size_in=d, size_out=d)

        # High level routing
        actions = spa.Actions(
            'dot(visual, BA) --> phonemic=BA',
            '0.2 --> phonemic=NEUTRAL, premotor=NEUTRAL, audiexpect=NEUTRAL, motor=NEUTRAL',
            'dot(phonemic, BA) --> premotor=BA, motor=BA_EXEC, audiexpect=BA',
            'dot(phonemic, DA) --> premotor=DA, motor=DA_EXEC, audiexpect=DA',
            'dot(phonemic, GA) --> premotor=GA, motor=GA_EXEC, audiexpect=GA',
            'dot(phonemic, PA) --> premotor=PA, motor=PA_EXEC, audiexpect=PA',
            'dot(phonemic, TA) --> premotor=TA, motor=TA_EXEC, audiexpect=TA',
            'dot(phonemic, KA) --> premotor=KA, motor=KA_EXEC, audiexpect=KA',
            'dot(somato, BA_EXEC) --> phonemic=DA',
            'dot(somato, DA_EXEC) --> phonemic=GA',
            'dot(somato, GA_EXEC) --> phonemic=PA',
            'dot(somato, PA_EXEC) --> phonemic=TA',
            'dot(somato, TA_EXEC) --> phonemic=KA',
            'dot(somato, KA_EXEC) --> phonemic=BA',
        )
        nengo.networks.actionselection.Weights.lg = lg
        nengo.networks.actionselection.Weights.le = le
        nengo.networks.actionselection.Weights.wt = 1.0
        nengo.networks.actionselection.Weights.wp = 0.9
        nengo.networks.actionselection.Weights.wp_snr = 0.9  # not used yet

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

        # model delay:
        nengo.Connection(model.motor.state.output, model.motor1, synapse=0.01)

        # no delay:
        #nengo.Connection(model.motor1, model.motor2, synapse=0.01)

        # delay:
        nengo.Connection(model.motor1, model.delaynode)
        nengo.Connection(model.delaynode, model.motor2)

        nengo.Connection(model.motor2, model.somato.state.input, synapse=0.01)

        # Provide input to high-level
        def start(t):
            if t < 0.25:
                return 'ZERO'
            elif t < 0.45:
                return 'BA'
            else:
                return 'ZERO'

        model.input = spa.Input(visual=start)

        # set up probes
        visual = nengo.Probe(model.visual.state.output, synapse=0.03)
        phonemic = nengo.Probe(model.phonemic.state.output, synapse=0.03)
        premotor = nengo.Probe(model.premotor.state.output, synapse=0.03)
        audiexpect = nengo.Probe(model.audiexpect.state.output, synapse=0.03)
        motor = nengo.Probe(model.motor.state.output, synapse=0.03)
        somato = nengo.Probe(model.somato.state.output, synapse=0.03)

    with nengo.Simulator(model) as sim:
        sim.run(sim_time)

    plt.figure(figsize=(18, 16))
    plt.subplot(6, 1, 1)
    plt.plot(sim.trange(), model.similarity(sim.data, visual))
    plt.legend(model.get_input_vocab('visual').keys, fontsize='x-small')
    plt.ylabel('Visual')
    plt.subplot(6, 1, 2)
    plt.plot(sim.trange(), model.similarity(sim.data, phonemic))
    plt.legend(model.get_input_vocab('phonemic').keys, fontsize='x-small')
    plt.ylabel('Phonemic')
    plt.subplot(6, 1, 3)
    plt.plot(sim.trange(), model.similarity(sim.data, audiexpect))
    plt.legend(model.get_input_vocab('audiexpect').keys, fontsize='x-small')
    plt.ylabel('Audi_expect')
    plt.subplot(6, 1, 4)
    plt.plot(sim.trange(), model.similarity(sim.data, premotor))
    plt.legend(model.get_input_vocab('premotor').keys, fontsize='x-small')
    plt.ylabel('Premotor')
    plt.subplot(6, 1, 5)
    plt.plot(sim.trange(), model.similarity(sim.data, motor))
    plt.legend(model.get_input_vocab('motor').keys, fontsize='x-small')
    plt.ylabel('Motor')
    plt.subplot(6, 1, 6)
    plt.plot(sim.trange(), model.similarity(sim.data, somato))
    plt.legend(model.get_input_vocab('somato').keys, fontsize='x-small')
    plt.ylabel('Somato')

    if save_dir is not None:
        plt.savefig(save_dir)

    plt.close()
Example #28
0
def Spaun():
    model = spa.SPA(label='Spaun', seed=cfg.seed)
    with model:
        model.config[nengo.Ensemble].max_rates = cfg.max_rates
        model.config[nengo.Ensemble].neuron_type = cfg.neuron_type
        model.config[nengo.Connection].synapse = cfg.pstc

        if 'S' in cfg.spaun_modules:
            model.stim = Stimulus()
            model.instr_stim = InstrStimulus()
            model.monitor = Monitor()
        if 'V' in cfg.spaun_modules:
            model.vis = Vision()
        if 'P' in cfg.spaun_modules:
            model.ps = ProdSys()
        if 'R' in cfg.spaun_modules:
            model.reward = RewardEval()
        if 'E' in cfg.spaun_modules:
            model.enc = InfoEnc()
        if 'W' in cfg.spaun_modules:
            model.mem = Memory()
        if 'T' in cfg.spaun_modules:
            model.trfm = TrfmSys()
        if 'D' in cfg.spaun_modules:
            model.dec = InfoDec()
        if 'M' in cfg.spaun_modules:
            model.mtr = Motor()
        if 'I' in cfg.spaun_modules:
            model.instr = InstrProcess()

        model.learn_conns = []

        if hasattr(model, 'vis') and hasattr(model, 'ps'):
            copy_draw_action = \
                ['0.5 * (dot(ps_task, X) + dot(vis, ZER)) --> ps_task = W, ps_state = TRANS0, ps_dec = FWD',  # noqa
                 'dot(ps_task, W-DEC) - dot(vis, QM) --> ps_state = ps_state']  # noqa
            # Copy drawing task format: A0[r]?X

            recog_action = \
                ['0.5 * (dot(ps_task, X) + dot(vis, ONE)) --> ps_task = R, ps_state = TRANS0, ps_dec = FWD',  # noqa
                 'dot(ps_task, R-DEC) - dot(vis, QM) --> ps_state = ps_state']  # noqa
            # Digit recognition task format: A1[r]?X

            learn_state_action = ['0.5 * (dot(ps_task, X) + dot(vis, TWO)) - dot(vis, QM) --> ps_task = L, ps_state = LEARN, ps_dec = FWD']  # noqa
            # Learning task format: A2?X<REWARD>?X<REWARD>?X<REWARD>?X<REWARD>?X<REWARD>    # noqa
            if hasattr(model, 'reward'):
                learn_action = ['0.5 * (dot(ps_task, 2*L) - 1) - dot(vis, QM) --> ps_action = %s, ps_state = LEARN, ps_dec = NONE' %  # noqa
                                s for s in vocab.ps_action_learn_sp_strs]
            else:
                learn_action = []

            mem_action = \
                ['0.5 * (dot(ps_task, X) + dot(vis, THR)) --> ps_task = M, ps_state = TRANS0, ps_dec = FWD',  # noqa
                 'dot(ps_task, M-DEC) - dot(vis, F + R + QM) --> ps_state = ps_state',  # noqa
                 '0.5 * (dot(ps_task, M) + dot(vis, F)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_dec = FWD',  # noqa
                 '0.5 * (dot(ps_task, M) + dot(vis, R)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_dec = REV']  # noqa
            # Working memory task format: A3[rr..rr]?XXX
            # Reverse recall task format: A3[rr..rr]R?XXX

            if hasattr(model, 'trfm'):
                count_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, FOR)) --> ps_task = C, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, C) + dot(ps_state, TRANS0)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = CNT0',  # noqa
                     '0.5 * (dot(ps_task, C) + dot(ps_state, CNT0)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = CNT1',  # noqa
                     '(0.25 * (dot(ps_task, DEC) + dot(ps_state, CNT1)) + 0.5 * dot(trfm_compare, NO_MATCH)) + (dot(ps_dec, CNT) - 1) - dot(vis, QM) --> ps_dec = CNT, ps_state = CNT1',  # noqa
                     '(0.25 * (dot(ps_task, DEC) + dot(ps_state, CNT1)) + 0.5 * dot(trfm_compare, MATCH)) + (dot(ps_dec, CNT) - 1) - dot(vis, QM) --> ps_dec = FWD, ps_state = TRANS0']  # noqa
                # Counting task format: A4[START_NUM][NUM_COUNT]?X..X
                qa_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, FIV)) --> ps_task = A, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     'dot(ps_task, A-DEC) - dot(vis, K + P + QM) --> ps_state = ps_state',  # noqa
                     '0.5 * (dot(ps_task, A) + dot(vis, K)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = QAK',  # noqa
                     '0.5 * (dot(ps_task, A) + dot(vis, P)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = QAP']  # noqa
                # Question answering task format: A5[rr..rr]P[r]?X (probing item in position)           # noqa
                #                                 A5[rr..rr]K[r]?X (probing position of item (kind))    # noqa
                rvc_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, SIX)) --> ps_task = V, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, V) + dot(ps_state, TRANS0)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANS1',  # noqa
                     '0.5 * (dot(ps_task, V) + dot(ps_state, TRANS1)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANS0']  # noqa
                # Rapid variable creation task format: A6{[rr..rr][rr..rr]:NUM_EXAMPLES}?XX..XX     # noqa
                fi_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, SEV)) --> ps_task = F, ps_state = TRANS0, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, F) + dot(ps_state, TRANS0)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANS1',  # noqa
                     '0.5 * (dot(ps_task, F) + dot(ps_state, TRANS1)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANS2',  # noqa
                     '0.5 * (dot(ps_task, F) + dot(ps_state, TRANS2)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANS0']  # noqa
                # Fluid intelligence task format: A7[CELL1_1][CELL1_2][CELL1_3][CELL2_1][CELL2_2][CELL2_3][CELL3_1][CELL3_2]?XX..XX     # noqa

                # Reaction task
                react_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, EIG)) --> ps_task = REACT, ps_state = DIRECT, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, REACT) + dot(vis_mem, ONE)) --> trfm_input = POS1*THR',  # noqa
                     '0.5 * (dot(ps_task, REACT) + dot(vis_mem, TWO)) --> trfm_input = POS1*FOR']  # noqa
                # Stimulus response (hardcoded reaction) task format: A8?1X<expected 3>?2X<expected 4>    # noqa

                # Compare task -- See two items, check if their class matches each other # noqa
                match_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, C)) --> ps_task = CMP, ps_state = TRANS1, ps_dec = FWD',  # noqa
                     '0.5 * (dot(ps_task, CMP) + dot(ps_state, TRANS1)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANS2',  # noqa
                     '0.5 * (dot(ps_task, CMP) + dot(ps_state, TRANS2)) - dot(vis, QM) - dot(ps_task, DEC) --> ps_state = TRANSC']  # noqa
                # List / item matching task format: AC[r][r]?X<expected 1 if match, 0 if not>                                                      # noqa
                #                                   AC[rr..rr][rr..rr]?X<expected 1 if any item in first list appears in second list, 0 if not>    # noqa
            else:
                count_action = []
                qa_action = []
                rvc_action = []
                fi_action = []
                react_action = []
                match_action = []

            if hasattr(model, 'trfm') and hasattr(model, 'instr'):
                instr_action = \
                    ['0.5 * (dot(ps_task, X) + dot(vis, NIN)) --> ps_task = INSTR, ps_state = DIRECT, ps_dec = FWD',  # noqa
                     # 'dot(ps_task, INSTR) - dot(vis, QM + A) --> trfm_input = instr_data',  # noqa - Note: this is the bg 'instr' rule in it's simplified form
                     'dot(ps_task, INSTR) - dot(vis, QM + A + M + P + CLOSE) - dot(ps_state, INSTRP) --> instr_en = ENABLE, ps_task = instr_task, ps_state = instr_state, ps_dec = instr_dec, trfm_input = instr_data',  # noqa
                     '1.5 * dot(vis, M + V) --> ps_task = INSTR, ps_state = TRANS0, ps_dec = FWD',   # noqa - Note: V no longer keeps state, dec information from before. Need to set in instruction
                     '0.5 * (dot(ps_task, INSTR) + dot(vis, P)) --> ps_task = INSTR, ps_state = INSTRP',   # noqa
                     '0.5 * (dot(ps_task, INSTR) + dot(ps_state, INSTRP)) --> ps_task = INSTR, ps_state = TRANS0',   # noqa
                     # 'dot(instr_util, INSTR) - dot(ps_task, INSTR) --> instr_en = ENABLE, ps_task = instr_task, ps_state = instr_state, ps_dec = instr_dec, trfm_input = instr_data',  # noqa - Note: this is the untested 'use output of IPS to set PS states' implementation
                     ]  # noqa
                # Instructed tasks task formats:
                # Instructed stimulus response task format: A9?rX<expected answer from instruction>?rX<expected answer from instruction>    # noqa
                # Instructed custom task format: M<0-9>[INSTRUCTED TASK FORMAT]?XX..XX                                                      # noqa
                # Instructed positional task formats: MP<0-9>[INSTRUCTED TASK FORMAT]?XX..XX V[INSTRUCTED TASK FORMAT]?XX..XX               # noqa
                #     - P<0-9> selects appropriate instruction from list of instructions                                                    # noqa
                #     - V increments instruction position by 1
            else:
                instr_action = []

            decode_action = \
                ['dot(vis, QM) - 0.6 * dot(ps_task, W+C+V+F+L+REACT) --> ps_task = ps_task + DEC, ps_state = ps_state + 0.5 * TRANS0, ps_dec = ps_dec + 0.5 * FWD',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, W-DEC)) --> ps_task = W + DEC, ps_state = ps_state, ps_dec = DECW',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, C-DEC)) --> ps_task = C + DEC, ps_state = ps_state, ps_dec = CNT',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, V+F-DEC)) --> ps_task = ps_task + DEC, ps_state = ps_state, ps_dec = DECI',  # noqa
                 '0.7 * dot(vis, QM) + 0.3 * dot(ps_task, L) --> ps_task = L + DEC, ps_state = LEARN, ps_dec = FWD',  # noqa
                 '0.5 * (dot(vis, QM) + dot(ps_task, REACT)) --> ps_task = REACT + DEC, ps_state = DIRECT, ps_dec = FWD',  # noqa
                 '0.75 * dot(ps_task, DEC-REACT-INSTR) - dot(ps_state, LEARN) - dot(ps_dec, CNT) - dot(vis, QM + A + M) --> ps_task = ps_task, ps_state = ps_state, ps_dec = ps_dec']  # noqa
            default_action = \
                []

            # List learning task spa actions first, so we know the precise
            # indicies of the learning task actions (i.e. the first N)
            all_actions = (learn_action + learn_state_action +
                           copy_draw_action + recog_action + mem_action +
                           count_action + qa_action + rvc_action + fi_action +
                           decode_action + default_action + react_action +
                           instr_action + match_action)

            actions = spa.Actions(*all_actions)
            model.bg = spa.BasalGanglia(actions=actions, input_synapse=0.008,
                                        label='Basal Ganglia')
            model.thal = spa.Thalamus(model.bg, subdim_channel=1,
                                      mutual_inhibit=1, route_inhibit=5.0,
                                      label='Thalamus')

        # ----- Set up connections (and save record of modules) -----
        if hasattr(model, 'vis'):
            model.vis.setup_connections(model)
        if hasattr(model, 'ps'):
            model.ps.setup_connections(model)
            # Modify any 'channel' ensemble arrays to have
            # get_optimal_sp_radius radius sizes
            for net in model.ps.all_networks:
                if net.label is not None and net.label[:7] == 'channel':
                    for ens in net.all_ensembles:
                        ens.radius = cfg.get_optimal_sp_radius()
        if hasattr(model, 'bg'):
            if hasattr(model, 'reward'):
                # Clear learning transforms
                del cfg.learn_init_transforms[:]

                with model.bg:
                    # Generate random biases for each learn action, so that
                    # there is some randomness to the initial action choice
                    bias_node = nengo.Node(1)
                    bias_ens = nengo.Ensemble(cfg.n_neurons_ens, 1,
                                              label='BG Bias Ensemble')
                    nengo.Connection(bias_node, bias_ens)

                    for i in range(len(learn_action)):
                        init_trfm = (np.random.random() *
                                     cfg.learn_init_trfm_max)
                        trfm_val = cfg.learn_init_trfm_bias + init_trfm
                        model.learn_conns.append(
                            nengo.Connection(bias_ens, model.bg.input[i],
                                             transform=trfm_val))
                        cfg.learn_init_transforms.append(trfm_val)
                logger.write("# learn_init_trfms: %s\n" %
                             (str(cfg.learn_init_transforms)))
        if hasattr(model, 'thal'):
            pass
        if hasattr(model, 'reward'):
            model.reward.setup_connections(model, model.learn_conns)
        if hasattr(model, 'enc'):
            model.enc.setup_connections(model)
        if hasattr(model, 'mem'):
            model.mem.setup_connections(model)
        if hasattr(model, 'trfm'):
            model.trfm.setup_connections(model)
            # Modify any 'channel' ensemble arrays to have
            # get_optimal_sp_radius radius sizes
            for net in model.trfm.all_networks:
                if net.label is not None and net.label[:7] == 'channel':
                    for ens in net.all_ensembles:
                        ens.radius = cfg.get_optimal_sp_radius()
        if hasattr(model, 'dec'):
            model.dec.setup_connections(model)
        if hasattr(model, 'mtr'):
            model.mtr.setup_connections(model)
        if hasattr(model, 'instr'):
            model.instr.setup_connections(model)
        if hasattr(model, 'monitor'):
            model.monitor.setup_connections(model)

    return model
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)
                                    feedback_synapse=0.01)
    model.vision = spa.State(dimensions=dimensions) 
    
    # Specify the action mapping
    actions = spa.Actions(
        'dot(vision, START) --> state = vision',
        'dot(state, A) --> state = B',
        'dot(state, B) --> state = C',
        'dot(state, C) --> state = D',
        'dot(state, D) --> state = E',
        'dot(state, E) --> state = A'
    )
    
    #Creating the BG and Thalamus components that confirm to the specified rules
    model.BG = spa.BasalGanglia(actions=actions)
    model.thal = spa.Thalamus(model.BG)
    
    #Change the seed of this RNG to change the vocabulary
    rng = np.random.RandomState(0)
    vocab = Vocabulary(dimensions=dimensions)

    #Create the transformation matrix (pd) and the cleanup ensemble (cleanupA) 
    pd = [vocab['A'].v.tolist()] 
    model.cleanup = spa.State(neurons_per_dimension=100, dimensions=1)
    
    #Function that provides the model with an initial input semantic pointer.
    def start(t):
        if t < 0.4:
            return '0.8*START+D'
        else:
            return '0'