Exemple #1
0
def test_errors():
    # buffer2 does not exist
    with pytest.raises(NameError):
        with spa.SPA() as model:
            model.buffer = spa.Buffer(dimensions=16)
            model.cortical = spa.Cortical(spa.Actions('buffer2=buffer'))

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

    # dot products not implemented
    with pytest.raises(NotImplementedError):
        with spa.SPA() as model:
            model.scalar = spa.Buffer(dimensions=1, subdimensions=1)
            model.cortical = spa.Cortical(
                spa.Actions('scalar=dot(scalar, FOO)'))
Exemple #2
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.word = spa.State(dimensions=p.D)
            model.marker = spa.State(dimensions=p.D)
            model.memory = spa.State(dimensions=p.D, feedback=1)

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

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

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

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

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

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

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

        split.remove_outputless_passthrough(model)

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

        return model
def test_transform(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=16)
        model.cortical = spa.Cortical(spa.Actions("buffer2=buffer1*B"))
        model.input = spa.Input(buffer1="A")

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

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

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

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

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

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

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

    match = np.dot(sim.data[p], vocab.parse('A').v)
    assert match[199] > 0.8
    def model(self, p):
        model = spa.SPA()
        with model:
            model.word = spa.State(dimensions=p.D)
            model.marker = spa.State(dimensions=p.D)
            model.memory = spa.State(dimensions=p.D, feedback=1)
            model.motor = spa.State(dimensions=p.D)
            model.cue = spa.State(dimensions=p.D)

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

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

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

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

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

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

        return model
Exemple #6
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.memory = spa.State(p.D, feedback=1)
            model.cue = spa.State(p.D)

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

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

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

            self.p_memory = nengo.Probe(model.memory.output, synapse=0.03)
        self.vocab = model.get_output_vocab('memory')
        return model
def test_connect(Simulator, seed):
    with spa.SPA(seed=seed) as model:
        model.buffer1 = spa.Buffer(dimensions=16)
        model.buffer2 = spa.Buffer(dimensions=16)
        model.buffer3 = spa.Buffer(dimensions=16)
        model.cortical = spa.Cortical(
            spa.Actions("buffer2=buffer1", "buffer3=~buffer1"))
        model.input = spa.Input(buffer1="A")

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

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

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

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

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

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

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

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

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

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

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

    match1 = np.dot(sim.data[p1], vocab1.parse('A+C').v)
    match2 = np.dot(sim.data[p2], vocab2.parse('B+C').v)
    # both values should be near 1.0 since buffer1 is driven to both A and C
    # and buffer2 is driven to both B and C.
    assert match1[199] > 0.75
    assert match2[199] > 0.75
Exemple #10
0
    def model(self, p):
        model = spa.SPA()
        with model:
            model.inA = spa.Buffer(p.D, subdimensions=p.SD,
                                   neurons_per_dimension=p.n_per_d)
            model.inB = spa.Buffer(p.D, subdimensions=p.SD,
                                   neurons_per_dimension=p.n_per_d)

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

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

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

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

            ideal = nengo.Node(model.get_output_vocab('inA').parse('A*B').v)
            self.probe_ideal = nengo.Probe(ideal, synapse=None)

        return model
 def __init__(self):
     self.scalar = spa.Buffer(dimensions=1, subdimensions=1)
     self.cortical = spa.Cortical(
         spa.Actions('scalar=dot(scalar, FOO)'))
 def __init__(self):
     self.buffer = spa.Buffer(dimensions=16)
     self.cortical = spa.Cortical(
         spa.Actions('dot(buffer,A) --> buffer=buffer'))
 def __init__(self):
     self.buffer = spa.Buffer(dimensions=16)
     self.cortical = spa.Cortical(spa.Actions('buffer2=buffer'))
 def __init__(self):
     self.buffer1 = spa.Buffer(dimensions=16)
     self.buffer2 = spa.Buffer(dimensions=32)
     self.input = spa.Input(buffer1='A')
     self.cortical = spa.Cortical(spa.Actions('buffer2=buffer1'))
 def __init__(self):
     self.buffer1 = spa.Buffer(dimensions=16)
     self.buffer2 = spa.Buffer(dimensions=32)
     self.cortical = spa.Cortical(
         spa.Actions('buffer1=A', 'buffer2=B', 'buffer1=C, buffer2=C'))
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 = ''
Exemple #17
0
n_cconv = 200
seed = 6
dt = 0.001

model = spa.SPA(seed=seed)
with model:
    model.inA = spa.Buffer(D, subdimensions=SD,
                           neurons_per_dimension=n_per_d)
    model.inB = spa.Buffer(D, subdimensions=SD,
                           neurons_per_dimension=n_per_d)

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

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

    input_A = nengo.Node(None, size_in=D)
    input_B = nengo.Node(None, size_in=D)
    output = nengo.Node(None, size_in=D)
    nengo.Connection(input_A, model.inA.state.input, synapse=None)
    nengo.Connection(input_B, model.inB.state.input, synapse=None)
    nengo.Connection(model.result.state.output, output, synapse=None)
sim = nengo.Simulator(model, dt=dt)


# convert this model into cores and messages
import system
s = system.System(model, sim)
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.noun = spa.Memory(D, synapse=0.2)
    model.verb = spa.Memory(D, synapse=0.2)
    model.sentence = spa.Buffer(D)
    
    model.speech = spa.Buffer(D)
    model.hand = spa.Buffer(D)

    actions = spa.Actions(
        'dot(vision, WRITE+SAY) --> verb=vision',
        'dot(vision, A+B+C+D+E) --> noun=vision',
        'dot(sentence, VERB*WRITE) - dot(vision, WRITE+SAY+A+B+C+D+E)'
            '--> hand=sentence*~NOUN',
        'dot(sentence, VERB*SAY) - dot(vision, WRITE+SAY+A+B+C+D+E)'
            '--> speech=sentence*~NOUN',
        )
        
    model.bg = spa.BasalGanglia(actions)
    model.thalamus = spa.Thalamus(model.bg)
    
    model.cortical = spa.Cortical(spa.Actions(
        'sentence=verb*VERB+noun*NOUN',
        ))
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',
        )
Exemple #20
0
def test_convolution(Simulator, plt, seed):
    D = 5
    with spa.SPA(seed=seed) as model:
        model.inA = spa.Buffer(dimensions=D)
        model.inB = spa.Buffer(dimensions=D)
        model.outAB = spa.Buffer(dimensions=D)
        model.outABinv = spa.Buffer(dimensions=D)
        model.outAinvB = spa.Buffer(dimensions=D)
        model.outAinvBinv = spa.Buffer(dimensions=D)

        model.cortical = spa.Cortical(
            spa.Actions(
                'outAB = inA * inB',
                'outABinv = inA * ~inB',
                'outAinvB = ~inA * inB',
                'outAinvBinv = ~inA * ~inB',
            ))
        nengo.Connection(nengo.Node([0, 1, 0, 0, 0]), model.inA.state.input)
        nengo.Connection(nengo.Node([0, 0, 1, 0, 0]), model.inB.state.input)

        pAB = nengo.Probe(model.outAB.state.output, synapse=0.03)
        pABinv = nengo.Probe(model.outABinv.state.output, synapse=0.03)
        pAinvB = nengo.Probe(model.outAinvB.state.output, synapse=0.03)
        pAinvBinv = nengo.Probe(model.outAinvBinv.state.output, synapse=0.03)

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

    t = sim.trange()
    plt.subplot(4, 1, 1)
    plt.ylabel('A*B')
    plt.axhline(0.85, c='k')
    plt.plot(t, sim.data[pAB])
    plt.subplot(4, 1, 2)
    plt.ylabel('A*~B')
    plt.axhline(0.85, c='k')
    plt.plot(t, sim.data[pABinv])
    plt.subplot(4, 1, 3)
    plt.ylabel('~A*B')
    plt.axhline(0.85, c='k')
    plt.plot(t, sim.data[pAinvB])
    plt.subplot(4, 1, 4)
    plt.ylabel('~A*~B')
    plt.axhline(0.85, c='k')
    plt.plot(t, sim.data[pAinvBinv])

    # Check results.  Since A is [0,1,0,0,0] and B is [0,0,1,0,0], this means:
    #    ~A = [0,0,0,0,1]
    #    ~B = [0,0,0,1,0]
    #   A*B = [0,0,0,1,0]
    #  A*~B = [0,0,0,0,1]
    #  ~A*B = [0,1,0,0,0]
    # ~A*~B = [0,0,1,0,0]
    # (Remember that X*[1,0,0,0,0]=X (identity transform) and X*[0,1,0,0,0]
    #  is X rotated to the right once)

    # Ideal answer: A*B = [0,0,0,1,0]
    assert np.allclose(np.mean(sim.data[pAB][-10:], axis=0),
                       np.array([0, 0, 0, 1, 0]),
                       atol=0.15)

    # Ideal answer: A*~B = [0,0,0,0,1]
    assert np.allclose(np.mean(sim.data[pABinv][-10:], axis=0),
                       np.array([0, 0, 0, 0, 1]),
                       atol=0.15)

    # Ideal answer: ~A*B = [0,1,0,0,0]
    assert np.allclose(np.mean(sim.data[pAinvB][-10:], axis=0),
                       np.array([0, 1, 0, 0, 0]),
                       atol=0.15)

    # Ideal answer: ~A*~B = [0,0,1,0,0]
    assert np.allclose(np.mean(sim.data[pAinvBinv][-10:], axis=0),
                       np.array([0, 0, 1, 0, 0]),
                       atol=0.15)
Exemple #21
0
AGENT = vocab.parse('AGENT')
VERB = vocab.parse('VERB')
THEME = vocab.parse('THEME')


def conv_expression(parsed):
    S, V, O = parsed
    return "p = VERB * {} + AGENT * {} + THEME * {}".format(V, S, O)


model = spa.SPA(label=mysent, vocabs=[vocab])
with model:
    model.p = spa.State(dimensions=dim, label='p')
    # model.t = spa.State(dimensions=dim, label='t')
    # model.z = spa.State(dimensions=dim, label='z')

    model.out_agent = spa.State(dimensions=dim, label='out_agent')
    model.out_verb = spa.State(dimensions=dim, label='out_verb')
    model.out_theme = spa.State(dimensions=dim, label='out_theme')

    actions = spa.Actions(
        # 'p = VERB * CHASE + AGENT * DOG + THEME * BOY',
        conv_expression(custom_parser(mysent)),
        'out_agent = p * ~AGENT',
        'out_verb = p * ~VERB',
        'out_theme = p * ~THEME',
    )

    model.cortical = spa.Cortical(actions)
Exemple #22
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 __init__(self):
     self.unitary = spa.Buffer(dimensions=16)
     self.cortical = spa.Cortical(
         spa.Actions('unitary=unitary*unitary'))
Exemple #24
0
import nengo
import nengo.spa as spa

D = 128

model = spa.SPA()
with model:
    model.memory = spa.State(D, feedback=1)

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

    model.cue = spa.State(D)

    model.item = spa.State(D)

    model.cortical = spa.Cortical(
        spa.Actions(
            'item = memory*~cue',
            'memory = 0.5*item*cue',
        ))
    strength = 0.4

    model.subjx = spa.Compare(D_space)
    model.subjy = spa.Compare(D_space)
    model.objx = spa.Compare(D_space)
    model.objy = spa.Compare(D_space)

    for ens in model.all_ensembles:
        ens.neuron_type = nengo.Direct()

    model.cortical = spa.Cortical(
        spa.Actions(
            'subjx_A=rule*~S*X',
            'subjy_A=rule*~S*Y',
            'objx_A=rule*~O*X',
            'objy_A=rule*~O*Y',
            'subjx_B=objs',
            'subjy_B=objs',
            'objx_B=objs',
            'objy_B=objs',
        ))

    actions = spa.Actions(
        'dot(rule, (BELOW-ABOVE-LEFT-RIGHT)*V)*{strength} +'
        '(subjy - objy)+{separation} --> '
        'status=BAD, '
        'objs=-{speed}*Y*objs*~S + {speed}*Y*objs*~O'.format(**locals()),
        'dot(rule, (BELOW-ABOVE-LEFT-RIGHT)*V)*{strength} +'
        '(subjy - objy)-{separation} --> '
        'status=GOOD'.format(**locals()),
        'dot(rule, (ABOVE-BELOW-LEFT-RIGHT)*V)*{strength} +'
Exemple #26
0
import nengo
import nengo.spa as spa

D = 16

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

    model.input = spa.Input(
        a='A',
        b=(lambda t: 'C*~A' if (t % 0.1 < 0.05) else 'D*~A'),
    )

if __name__ == '__main__':
    import nengo_gui
    nengo_gui.GUI(__file__).start()
Exemple #27
0
            
            'dot(switch, ACT)      -->  ctrl=TOP_STACK, gate=1, switch=-1*ACT',
            'dot(ctrl, TOP_STACK)  -->  cleanup_action=stack, ctrl=POP_STACK',
            'dot(ctrl, POP_STACK)  -->  push=(stack-premotor)*~INDEX, ctrl=SET_STACK',
            'dot(ctrl, SET_STACK)  -->  stack=10*push, ctrl=TOP_STACK',
            'dot(sig, PLAN)        -->  ctrl=PLAN, gate=-0.45',
            'dot(switch, DONE)     -->  ctrl=DONE',
            '0.5                   -->  ')

        ct_actions = spa.Actions(
            'action_to_effect=action',
            'effect=action_to_effect',
            'premotor=cleanup_action')
        
        model.bg = spa.BasalGanglia(bg_actions)
        model.ct = spa.Cortical(ct_actions)
        model.thal = spa.Thalamus(model.bg)

        def set_goal(t):
            return 'WATER_BOILED'

        def set_location(t):
            return 'KITCHEN'
        
        def set_plan(t):
            if 0.03 < t < 0.07: return 'PLAN'
            else: return '0'

        model.start = spa.Input(m_goal=set_goal, ctrl=set_plan, location=set_location)

        model.motor = nengo.Node(motor_sys, size_in=D, size_out=1)