Exemple #1
0
def set_score(score, non_evo_storage_manager=None, _genome=None):
    global evolution_genome

    if _genome is not None:
        evolution_genome = _genome

    if evolution_genome is not None:
        if 'score' in evolution_genome:  #only when setscore is called multiple times on accident...
            evolution_genome.pop('score')
        if 'evo_name' in evolution_genome and 'gen' in evolution_genome and 'id' in evolution_genome:
            sm = StorageManager(main_folder_name=get_gene('evo_name', None),
                                folder_name=get_gene_file(evolution_genome),
                                print_msg=False,
                                add_new_when_exists=False)
            evolution_genome['score'] = score
            sm.save_param_dict(evolution_genome)
            print('evolution score set to #' + str(score) + '#')
        else:
            print('cannot save score', str(score),
                  'to file: "evo_name", "gen" or "id" not in genome')
    else:
        print('score=' + str(score) + ' (no genome found)')
        if non_evo_storage_manager is not None:
            non_evo_storage_manager.save_param('score', score)

    # reset and reload when next get_gene is called

    evolution_genome = None
Exemple #2
0
def run(tag='hierarchical', ind=[], par={'N_e': 900}):
    sm = StorageManager(tag, random_nr=True, print_msg=display)
    sm.save_param_dict(par)

    #source = FDTGrammarActivator_New(tag='grammar_act', random_blocks=True, input_density=10/par['N_e'])#.plot_char_input_statistics()#output_size=par['N_e']#15
    #source = LongDelayGrammar(tag='grammar_act', output_size=par['N_e'], random_blocks=True, mode=['very simple'], input_density=10/par['N_e'])#.print_test().plot_char_input_statistics()
    #print(source.get_text_score('. parrot likes trees. wolf wolf wolf..'))
    source = Line_Patterns(tag='image_act',
                           group_possibility=1.0,
                           grid_width=30,
                           grid_height=30,
                           center_x=list(range(30)),
                           center_y=30 / 2,
                           degree=90,
                           line_length=60)
    #source = Line_Patterns(tag='image_act', group_possibility=1.0, grid_width=10, grid_height=10, center_x=10 / 2, center_y=list(range(10)), degree=0, line_length=20)
    #source = Line_Patterns(tag='image_act', group_possibility=1.0, grid_width=10, grid_height=10, center_x=10 / 2, center_y=10 / 2, degree=list(np.arange(0, 360, 20)), line_length=20)
    #source = MNIST_Patterns(tag='image_act', group_possibility=1.0, repeat_same_label_time=1)
    #source = TNAP_Image_Patches(tag='image_act', image_path='../../../Images/pexels-photo-275484.jpeg', grid_width=20, grid_height=20, dimensions=['on_center_white'], patch_norm=True)#'red', 'green', 'blue', 'gray', '255-red', '255-green', '255-blue', '255-gray',, 'rgbw', '255-rgbw''off_center_white',

    SORN = Network([], [], initialize=False)

    for timecale in [1]:  #2

        e_ng = NeuronGroup(
            net=SORN,
            tag='main_exc_group,ts={}'.format(timecale),
            size=get_squared_dim(int(par['N_e'])),
            behaviour={
                1:
                STDP_simple(exponent='[3.0#1]',
                            post_learn_value='[6.55#2]'),  # 8.92 #6.55
                2:
                TemporalWeightCache(decay=1,
                                    strength=1,
                                    GLU_density=0.5,
                                    set_weights=True),
                # 3: RandomWeightFluctuation(beta=4*0.2, gamma=3*0.2),
                3:
                GlutamateCacheConvergeAndNormalization(
                    norm_value='0.1;0.6'),  # ,#20.0 #(norm_value=0.001)#40
                5:
                InterGammaGlutamate(),  #0.005
                6:
                IntraGammaGlutamate(),
                7:
                IntraGammaGABA(GABA_density=1.0,
                               GABA_random_factor=10.0,
                               GABA_Norm='[3.8#0]'),  #3.4 #3.8
                8:
                ActivityBuffering(
                    activity_multiplyer=0.0,
                    firetreshold='0.1;+-0%'),  #todo warning: relu deactivated
                9:
                HomeostaticMechanism(range_end='[550#3];+-30%',
                                     inc='[0.184#4];+-10%',
                                     dec='[0.492#5];+-10%',
                                     pattern_chance='[0.0086#6];+-30%'
                                     ),  #target_max=20.0#0.0086 #,'
                10:
                RandomLeakInput(random_strength='[0.5#7]'),
                11:
                additional(),
                100:
                Recorder(['n.output'], tag='exc_out_rec')
            })

        #i_ng['structure', 0].stretch_to_equal_size(e_ng)

        SynapseGroup(
            net=SORN, src=e_ng, dst=e_ng, tag='GLU,e->e'
        )  #.partition([10, 10], [6, 6]) #, connectivity='(np.abs(sx-dx)<10)*(np.abs(sy-dy)<10)'
        SynapseGroup(
            net=SORN, src=e_ng, dst=e_ng, tag='GABA,e->e'
        )  #.partition([10, 10], [6, 6]) #, connectivity='(np.abs(sx-dx)<10)*(np.abs(sy-dy)<10)',

        #SynapseGroup(net=SORN, src=e_ng, dst=i_ng, connectivity='(np.abs(sx-dx)<10)*(np.abs(sy-dy)<10)', tag='GLU,e->i').partition([10, 10], [4, 4])
        #SynapseGroup(net=SORN, src=i_ng, dst=e_ng, connectivity='(np.abs(sx-dx)<10)*(np.abs(sy-dy)<10)', tag='GABA,i->e')
        #SynapseGroup(net=SORN, src=i_ng, dst=i_ng, connectivity='(np.abs(sx-dx)<10)*(np.abs(sy-dy)<10)', tag='GABA,i->i')

        e_ng.add_behaviour(
            4, TREN_external_input(strength=0.5, pattern_groups=[source]))
        # i_ng.add_behaviour(10, SORN_external_input(strength=0.3, pattern_groups=[source]))
        #if timecale == 1:
        e_ng.add_behaviour(101, Recorder(['n.pattern_index'], tag='inp_rec'))
        #else:
        #    #forward synapses
        #    SynapseGroup(net=SORN, src=last_e_ng, dst=e_ng, tag='GLU,e->e(+1)').partition([10, 10], [6, 6])
        #    SynapseGroup(net=SORN, src=last_e_ng, dst=e_ng, tag='GABA,e->i(+1)').partition([10, 10], [2, 2])
        #    #backward synapses
        #    SynapseGroup(net=SORN, src=e_ng, dst=last_e_ng, tag='GLU,e(+1)->e').partition([10, 10], [6, 6])
        #    SynapseGroup(net=SORN, src=e_ng, dst=last_e_ng, tag='GABA,e(+1)->i').partition([10, 10], [2, 2])

        #last_e_ng = e_ng
        #last_i_ng = i_ng

    SORN.set_marked_variables(ind, info=(ind == []))
    SORN.initialize(info=False)

    ############################################################################################################################################################

    score = 0

    #SORN.simulate_iterations(100, 100, measure_block_time=True, disable_recording=True)

    import Exploration.Network_UI.Network_UI as SUI
    SUI.Network_UI(SORN,
                   label='T',
                   exc_group_name='main_exc_group',
                   inh_group_name='main_inh_group',
                   storage_manager=sm).show()

    score += get_evolution_score_words(
        SORN,
        15000,
        5000,
        2000,
        display=False,
        stdp_off=True,
        same_timestep_without_feedback_loop=True,
        steps_recovery=15000)  #, steps_recovery=15000
    #score += get_oscillation_score_hierarchical(SORN, 0, 5000)
    return score
def run(
    attrs={
        'name': 'KWTA',
        'ind': [],
        'N_e': 900,
        'TS': [1],
        'ff': True,
        'fb': True,
        'plastic': 15000
    }):
    so = True

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    sm = StorageManager(attrs['name'], random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    source = FewSentencesGrammar(tag='grammar_act',
                                 output_size=attrs['N_e'],
                                 random_blocks=True,
                                 input_density=0.015)
    #source = SingleWordGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)
    #source = FewLongSentencesGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)
    #source.plot_char_frequency_histogram(20)

    SORN = Network()

    e_ng = NeuronGroup(
        net=SORN,
        tag='PC_{},prediction_source'.format(1),
        size=get_squared_dim(attrs['N_e']),
        behaviour={
            2:
            SORN_init_neuron_vars_no_reset(timescale=1),
            #3: SORN_init_afferent_synapses(transmitter='GLU', density='90%', distribution='uniform(0.5,1.0)', normalize=True),
            5:
            SORN_init_afferent_synapses(transmitter='GLU_cluster',
                                        density='90%',
                                        distribution='uniform(0.9,1.0)',
                                        normalize=True),

            #10.0: SORN_slow_syn(transmitter='GLU', strength='1.0', so=so),
            10.1:
            SORN_IP_WTA_apply(),
            10.15:
            WTA_refrac_apply(strengthfactor=0.1),
            #10.2: SORN_generate_output_K_WTA_partitioned(K='0.24'),
            8.3:
            SORN_slow_syn_simple(transmitter='GLU_cluster', strength='1.0'),
            8.4:
            SORN_generate_output_K_WTA_partitioned(
                K='0.04', filter_temporal_output=False),

            #10.5: SORN_slow_syn_simple(transmitter='GLU_cluster', strength='1.0'),
            #10.6: SORN_generate_output_K_WTA_partitioned(K='0.08', filter_temporal_output=True),

            #10.7: SORN_slow_syn_simple(transmitter='GLU_cluster', strength='1.0'),
            #10.8: SORN_generate_output_K_WTA_partitioned(K='0.04', filter_temporal_output=True),
            15:
            SORN_buffer_variables(random_temporal_output_shift=False),
            18:
            WTA_refrac(),
            20:
            SORN_IP_WTA(h_ip='lognormal_real_mean(0.04, 0.2944)',
                        eta_ip='0.007',
                        clip_min=None),
            #21.1: SORN_STDP(transmitter='GLU', eta_stdp='0.00015', STDP_F={-1: 1, 1: -1}),#, 0: 1 #[0.00015#7]
            21.2:
            SORN_STDP(transmitter='GLU_cluster',
                      eta_stdp='0.00015',
                      STDP_F={0: 0.5}),  #[0.00015#7]
            #22: SORN_SN(syn_type='GLU', behaviour_norm_factor=1.0),
            23:
            SORN_SN(syn_type='GLU_cluster', behaviour_norm_factor=0.1)
        })

    SynapseGroup(net=SORN,
                 src=e_ng,
                 dst=e_ng,
                 tag='syn,GLU',
                 connectivity='(s_id!=d_id)*in_box(10)')  #, partition=True)
    #SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='syn,GLU_cluster', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

    #SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU', connectivity='(s_id!=d_id)*in_box(10)')#, partition=True)
    SynapseGroup(
        net=SORN,
        src=e_ng,
        dst=e_ng,
        tag='GLU_cluster',
        connectivity=
        '(s_id!=d_id)*((sy<0)*(dy<0)*(sx<0)*(dx<0)+(sy>=0)*(dy>=0)*(sx>=0)*(dx>=0)+(sy<0)*(dy<0)*(sx>=0)*(dx>=0)+(sy>=0)*(dy>=0)*(sx<0)*(dx<0))'
    )
    '''
    '(s_id!=d_id)*('+
                                                                               '(s_id>=35*0)*(d_id>=35*0)*(s_id<35*1)*(d_id<35*1)+' +
                                                                               '(s_id>=35*1)*(d_id>=35*1)*(s_id<35*2)*(d_id<35*2)+' +
                                                                               '(s_id>=35*2)*(d_id>=35*2)*(s_id<35*3)*(d_id<35*3)+' +
                                                                               '(s_id>=35*3)*(d_id>=35*3)*(s_id<35*4)*(d_id<35*4)+' +
                                                                               '(s_id>=35*4)*(d_id>=35*4)*(s_id<35*5)*(d_id<35*5)+' +
                                                                               '(s_id>=35*5)*(d_id>=35*5)*(s_id<35*6)*(d_id<35*6)+' +
                                                                               '(s_id>=35*6)*(d_id>=35*6)*(s_id<35*7)*(d_id<35*7)+' +
                                                                               '(s_id>=35*7)*(d_id>=35*7)*(s_id<35*8)*(d_id<35*8)+' +
                                                                               '(s_id>=35*8)*(d_id>=35*8)*(s_id<35*9)*(d_id<35*9)+' +
                                                                               '(s_id>=35*9)*(d_id>=35*9)*(s_id<35*10)*(d_id<35*10)+' +
                                                                               '(s_id>=35*10)*(d_id>=35*10)*(s_id<35*11)*(d_id<35*11)+' +
                                                                               '(s_id>=35*11)*(d_id>=35*11)*(s_id<35*12)*(d_id<35*12)+' +
                                                                               '(s_id>=35*12)*(d_id>=35*12)*(s_id<35*13)*(d_id<35*13)+' +
                                                                               '(s_id>=35*13)*(d_id>=35*13)*(s_id<35*14)*(d_id<35*14)+' +
                                                                               '(s_id>=35*14)*(d_id>=35*14)*(s_id<35*15)*(d_id<35*15)+' +
                                                                               '(s_id>=35*15)*(d_id>=35*15)*(s_id<35*16)*(d_id<35*16)+' +
                                                                               '(s_id>=35*16)*(d_id>=35*16)*(s_id<35*17)*(d_id<35*17)+' +
                                                                               '(s_id>=35*17)*(d_id>=35*17)*(s_id<35*18)*(d_id<35*18)+' +
                                                                               '(s_id>=35*18)*(d_id>=35*18)*(s_id<35*19)*(d_id<35*19)+' +
                                                                               '(s_id>=35*19)*(d_id>=35*19)*(s_id<35*20)*(d_id<35*20)+' +
                                                                               '(s_id>=35*20)*(d_id>=35*20)*(s_id<35*21)*(d_id<35*21)+' +
                                                                               '(s_id>=35*21)*(d_id>=35*21)*(s_id<35*22)*(d_id<35*22)+' +
                                                                               '(s_id>=35*22)*(d_id>=35*22)*(s_id<35*23)*(d_id<35*23)+' +
                                                                               '(s_id>=35*23)*(d_id>=35*23)*(s_id<35*24)*(d_id<35*24)+' +
                                                                               '(s_id>=35*24)*(d_id>=35*24)*(s_id<35*25)*(d_id<35*25)+' +
                                                                               '(s_id>=35*25)*(d_id>=35*25)*(s_id<35*26)*(d_id<35*26)+' +
                                                                               '(s_id>=35*26)*(d_id>=35*26)*(s_id<35*27)*(d_id<35*27)+' +
                                                                               '(s_id>=35*27)*(d_id>=35*27)*(s_id<35*28)*(d_id<35*28)+' +
                                                                               '(s_id>=35*28)*(d_id>=35*28)*(s_id<35*29)*(d_id<35*29)+' +
                                                                               '(s_id>=35*29)*(d_id>=35*29)*(s_id<35*30)*(d_id<35*30)+' +
                                                                               '(s_id>=35*30)*(d_id>=35*30)*(s_id<35*31)*(d_id<35*31)+' +
                                                                               '(s_id>=35*31)*(d_id>=35*31)*(s_id<35*32)*(d_id<35*32)+' +
                                                                               '(s_id>=35*32)*(d_id>=35*32)*(s_id<35*33)*(d_id<35*33)+' +
                                                                               '(s_id>=35*33)*(d_id>=35*33)*(s_id<35*34)*(d_id<35*34)+' +
                                                                               '(s_id>=35*34)*(d_id>=35*34)*(s_id<35*35)*(d_id<35*35)+' +
                                                                               '(s_id>=35*35)*(d_id>=35*35)*(s_id<35*36)*(d_id<35*36)+' +
                                                                               '(s_id>=35*36)*(d_id>=35*36)*(s_id<35*37)*(d_id<35*37)+' +
                                                                               '(s_id>=35*37)*(d_id>=35*37)*(s_id<35*38)*(d_id<35*38)+' +
                                                                               '(s_id>=35*38)*(d_id>=35*38)*(s_id<35*39)*(d_id<35*39)+' +
                                                                               '(s_id>=35*39)*(d_id>=35*39)*(s_id<35*40)*(d_id<35*40)' +
                                                                               ')')#, partition=True)#

    
                                                                                   '(s_id>=140*0)*(d_id>=140*0)*(s_id<140*1)*(d_id<140*1)+' +
                                                                               '(s_id>=140*1)*(d_id>=140*1)*(s_id<140*2)*(d_id<140*2)+' +
                                                                               '(s_id>=140*2)*(d_id>=140*2)*(s_id<140*3)*(d_id<140*3)+' +
                                                                               '(s_id>=140*3)*(d_id>=140*3)*(s_id<140*4)*(d_id<140*4)+' +
                                                                               '(s_id>=140*4)*(d_id>=140*4)*(s_id<140*5)*(d_id<140*5)+' +
                                                                               '(s_id>=140*5)*(d_id>=140*5)*(s_id<140*6)*(d_id<140*6)+' +
                                                                               '(s_id>=140*6)*(d_id>=140*6)*(s_id<140*7)*(d_id<140*7)+' +
                                                                               '(s_id>=140*7)*(d_id>=140*7)*(s_id<140*8)*(d_id<140*8)+' +
                                                                               '(s_id>=140*8)*(d_id>=140*8)*(s_id<140*9)*(d_id<140*9)+' +
                                                                               '(s_id>=140*9)*(d_id>=140*9)*(s_id<140*10)*(d_id<140*10)' +'''

    #SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU_cluster', connectivity='(s_id!=d_id)*(sy>=20)*(dy>=20)')

    e_ng.add_behaviour(9, SORN_K_random_input())

    if __name__ == '__main__' and attrs.get('UI', False):
        e_ng.color = get_color(0, 1)

    SORN.set_marked_variables(info=print_info, storage_manager=sm)
    SORN.initialize(info=False)

    #for i, syn in enumerate(SORN['syn']):
    #    syn.enabled = np.load('Data/E{}.npy'.format(i))
    #    syn.W = np.load('Data/W{}.npy'.format(i))

    ###################################################################################################################

    if __name__ == '__main__' and attrs.get('UI', False):
        Network_UI(SORN,
                   label='SORN UI K_WTA',
                   storage_manager=sm,
                   group_display_count=1,
                   reduced_layout=False).show()

    score = 0
    plastic_steps = attrs.get('plastic', 20000)

    for i in range(1):
        sm = StorageManager(attrs['name'] + '[{:03d}]'.format(i + 1),
                            random_nr=True,
                            print_msg=print_info)
        sm.save_param_dict(attrs)
        score += train_and_generate_text(
            SORN,
            plastic_steps,
            5000,
            1000,
            display=print_info,
            stdp_off=True,
            same_timestep_without_feedback_loop=True,
            steps_recovery=1000,
            storage_manager=sm)

    print('score=', score)

    return score
def run(attrs={'name':'PV_SOM', 'ind':[], 'N_e':900, 'TS':[1]}):
    so = True

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    sm = StorageManager(attrs['name'], random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    #source = FDTGrammarActivator_New(tag='grammar_act', random_blocks=True, input_density=15/par['N_e'])#.plot_char_input_statistics()#output_size=par['N_e']#15
    #print(len(source.alphabet))
    source = LongDelayGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, mode=['simple'], input_density=0.015)#.print_test().plot_char_input_statistics()#10/par['N_e']
    #source.plot_char_input_statistics()
    #print(len(source.alphabet))
    #source = Line_Patterns(tag='image_act', group_possibility=1.0, grid_width=30, grid_height=30, center_x=list(range(30)), center_y=30 / 2, degree=90, line_length=60)
    #source = Line_Patterns(tag='image_act', group_possibility=1.0, grid_width=30, grid_height=30, center_x=list(range(30)), center_y=30 / 2, degree=90, line_length=60)
    #print(source.get_text_score('. parrot likes trees. wolf wolf wolf..'))

    SORN = Network()#[], [], initialize=False
    last_PC = None

    for layer, timescale in enumerate(attrs['TS']):

        PC = NeuronGroup(net=SORN, tag='Pyramidal Cell {},prediction_source'.format(timescale), size=get_squared_dim(int(attrs['N_e'])), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='[50#0]%', distribution='lognormal(0,0.6)', normalize=True, partition_compensation=True), #lognormal(0,0.6)
            4: SORN_init_afferent_synapses(transmitter='GABA_Dendrite', density='[30#1]%', distribution='uniform(0.1,0.11)', normalize=True),
            5: SORN_init_afferent_synapses(transmitter='GABA_Soma', density='[30#2]%', distribution='uniform(0.1,0.11)', normalize=True),
            6: SORN_init_afferent_synapses(transmitter='GABA_AIS', density='[30#3]%', distribution='uniform(0.1,0.11)', normalize=True),
            #7: SORN_init_afferent_synapses(transmitter='GABA_NOX', density='full', distribution=None, normalize=True),

            12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#4]', so=so),
            13: SORN_slow_syn(transmitter='GABA_Dendrite', strength='-0.1', so=False),

            17: SORN_fast_syn(transmitter='GABA_Soma', strength='-0.1', so=False),
            17.1: SORN_fast_syn(transmitter='GABA_AIS', strength='-0.1', so=False),
            #17.2: SORN_fast_syn(transmitter='GABA_NOX', strength='-0.1', so=False),
            18: SORN_generate_output(init_TH='0.1;+-100%'),
            19: SORN_buffer_variables(),

            20: SORN_Refractory_Digital(factor='[0.5#5];+-50%'),
            #20: SORN_Refractory_Analog(factor='[0.5#5];+-50%'),
            21: SORN_STDP(eta_stdp='[0.0015#6]'),#0.1#todo: test!!!, prune_stdp=False, excitation_punishment=0.0
            22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=1.0),

            23: SORN_IP_TI(mp='n.output', h_ip='lognormal_real_mean([0.04#7], [0.2944#8])', eta_ip='[0.0006#9];+-50%', integration_length='[15#10];+-50%', clip_min=None),#mp='n.output_new/2.0+n.output_new_temp/2.0'
            25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='0.5;+-50%', h_dh=0.0),#0.9#0.3
            26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#11], [0.2944#12])', eta_sc='[0.1#13];+-50%', integration_length='1'),
            #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]'),
        })

        MT_SOM = NeuronGroup(net=SORN, tag='Martinotti Cell {},Somatostatin'.format(timescale), size=get_squared_dim(int(0.07 * attrs['N_e'])), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='50%', distribution='lognormal(0,0.87038)', normalize=True),
            #4: SORN_init_afferent_synapses(transmitter='GABA', density='20%', distribution='lognormal(0,[0.82099#15])', normalize=True),  # 40

            10: SORN_slow_syn(transmitter='GLU', strength='0.6', so=so),  # 1.5353
            #11: SORN_slow_syn(transmitter='GABA', strength='-[0.1838#16]', so=so),

            #15: SORN_fast_syn(transmitter='GABA', strength='-[0.08#17]', so=False),#0.08
            18: SORN_generate_output(init_TH='0.1;+-0%'),
            19: SORN_buffer_variables(),
            #20: SORN_Refractory(factor='0.2;0.7'),
        })

        '''
        EXP_NOX_CELL = NeuronGroup(net=SORN, tag='NOX Cell {}'.format(timescale), size=get_squared_dim(int(16)), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution=None, normalize=True),
            #4: SORN_init_afferent_synapses(transmitter='GABA', density='20%', distribution='lognormal(0,[0.82099#15])', normalize=True),  # 40

            10: SORN_slow_syn(transmitter='GLU', strength='9.0', so=so),#1.5353
            #11: SORN_slow_syn(transmitter='GABA', strength='-[0.1838#16]', so=so),

            #15: SORN_fast_syn(transmitter='GABA', strength='-[0.08#17]', so=False),#0.08
            18: SORN_generate_output(init_TH='0.0', digital_output=False),
            19: SORN_buffer_variables(),
            #20: SORN_Refractory(factor='0.2;0.7'),
        })
        '''

        BA_PV = NeuronGroup(net=SORN, tag='Basket Cell {},Parvalbumin'.format(timescale), size=get_squared_dim(int(0.07 * attrs['N_e'])), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='50%', distribution='lognormal(0,0.87038)', normalize=True),
            #4: SORN_init_afferent_synapses(transmitter='GABA', density='20%', distribution='lognormal(0,[0.82099#15])', normalize=True),  # 40

            10: SORN_slow_syn(transmitter='GLU', strength='0.6', so=so),  # 1.5353
            #11: SORN_slow_syn(transmitter='GABA', strength='-[0.1838#16]', so=so),

            #15: SORN_fast_syn(transmitter='GABA', strength='-[0.08#17]', so=False),#0.08
            18: SORN_generate_output(init_TH='0.1;+-0%'),
            19: SORN_buffer_variables(),
            #20: SORN_Refractory(factor='0.2;0.7'),
        })

        CH_PV = NeuronGroup(net=SORN, tag='Chandelier Cell {},Parvalbumin'.format(timescale), size=get_squared_dim(int(0.07 * attrs['N_e'])), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='50%', distribution='lognormal(0,0.87038)', normalize=True),
            #4: SORN_init_afferent_synapses(transmitter='GABA', density='20%', distribution='lognormal(0,[0.82099#15])', normalize=True),  # 40

            #11: SORN_slow_syn(transmitter='GABA', strength='-[0.1838#16]', so=so),
            14: SORN_fast_syn(transmitter='GLU', strength='0.2', so=so),#1.5353
            #15: SORN_fast_syn(transmitter='GABA', strength='-[0.08#17]', so=False),#0.08

            18: SORN_generate_output(init_TH='0.1;+-0%'),
            19: SORN_buffer_variables(),
            #19: SORN_Refractory(factor='0.2;0.7'),
        })

        MT_SOM['structure', 0].stretch_to_equal_size(PC)
        BA_PV['structure', 0].stretch_to_equal_size(PC)
        CH_PV['structure', 0].stretch_to_equal_size(PC)
        #EXP_NOX_CELL['structure', 0].stretch_to_equal_size(PC)

        #plt.scatter(PC.x, PC.y)
        #plt.scatter(MT_SOM.x, MT_SOM.y)
        #plt.scatter(BA_PV.x, BA_PV.y)
        #plt.scatter(CH_PV.x, CH_PV.y)
        #plt.scatter(EXP_NOX_CELL.x, EXP_NOX_CELL.y)
        #plt.show()

        #print(np.min(EXP_NOX_CELL.x), np.max(EXP_NOX_CELL.x))
        #print(np.min(EXP_NOX_CELL.y), np.max(EXP_NOX_CELL.y))

        #print(np.min(PC.x), np.max(PC.x))
        #print(np.min(PC.y), np.max(PC.y))

        SynapseGroup(net=SORN, src=PC, dst=PC, tag='GLU', connectivity='(s_id!=d_id)*in_box(10)', partition=True)
        SynapseGroup(net=SORN, src=PC, dst=MT_SOM, tag='GLU', connectivity='in_box(2)', partition=True)
        SynapseGroup(net=SORN, src=PC, dst=BA_PV, tag='GLU', connectivity='in_box(2)', partition=True)
        SynapseGroup(net=SORN, src=PC, dst=CH_PV, tag='GLU', connectivity='in_box(2)', partition=True)

        #SynapseGroup(net=SORN, src=PC, dst=EXP_NOX_CELL, tag='GLU', connectivity='in_box(3.75)', partition=True)

        SynapseGroup(net=SORN, src=MT_SOM, dst=PC, tag='GABA_Dendrite', connectivity='in_box(10)', partition=True)
        SynapseGroup(net=SORN, src=BA_PV, dst=PC, tag='GABA_Soma', connectivity='in_box(10)', partition=True)
        SynapseGroup(net=SORN, src=CH_PV, dst=PC, tag='GABA_AIS', connectivity='in_box(10)', partition=True)

        #SynapseGroup(net=SORN, src=EXP_NOX_CELL, dst=PC, tag='GABA_NOX', connectivity='in_box(3.75)', partition=True)

        #SynapseGroup(net=SORN, src=SOM, dst=SOM, tag='GABA_D,SOM->SOM', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)
        #SynapseGroup(net=SORN, src=SOM, dst=PV, tag='GABA_D,SOM->PV', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)
        ##SynapseGroup(net=SORN, src=PV, dst=SOM, tag='GABA_P,PV->SOM')#not realistic?
        #SynapseGroup(net=SORN, src=PV, dst=PV, tag='GABA_P,PV->PV', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)

        if layer==0:
            PC.add_behaviour(9, SORN_external_input(strength=1.0, pattern_groups=[source]))
            #MT_SOM.add_behaviour(9, SORN_external_input(strength=1.0, pattern_groups=[source]))
            #BA_PV.add_behaviour(9, SORN_external_input(strength=1.0, pattern_groups=[source]))
            #CH_PV.add_behaviour(9, SORN_external_input(strength=1.0, pattern_groups=[source]))
        #else:
        #    #forward synapses
        #    SynapseGroup(net=SORN, src=last_PC, dst=PC, tag='GLU,PC->PC(+1)', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)#.partition([10, 10], [partition, partition])
        #    SynapseGroup(net=SORN, src=last_PC, dst=SOM, tag='GABA,PC->SOM(+1)', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)#.partition([5, 5], [2, 2])
        #    #backward synapses
        #    SynapseGroup(net=SORN, src=PC, dst=last_PC, tag='GLU,PC(+1)->PC', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)#.partition([10, 10], [partition, partition])
        #    SynapseGroup(net=SORN, src=PC, dst=last_SOM, tag='GABA,PC(+1)->SOM', connectivity='(s_id!=d_id)*(np.abs(sx-dx)<=10)*(np.abs(sy-dy)<=10)', partition=True)#.partition([5, 5], [2, 2])

        last_PC = PC
        #last_MT_SOM = MT_SOM
        #last_BA_PV = BA_PV
        #last_CH_PV = CH_PV
        #last_EXP_NOX_CELL = EXP_NOX_CELL

        if __name__ == '__main__':
            PC.color = get_color(0, timescale)
            MT_SOM.color = get_color(1, timescale)
            BA_PV.color = get_color(2, timescale)
            CH_PV.color = get_color(3, timescale)
            #EXP_NOX_CELL.color = get_color(4, timescale)
            #EXP_NOX_CELL.display_min_max_act = (0, 0.3)

    SORN.set_marked_variables(attrs['ind'], info=print_info, storage_manager=sm)
    SORN.initialize(info=False)

    ###################################################################################################################

    #if __name__ == '__main__':
    #    Network_UI(SORN, label='SORN UI PC PV SOM', storage_manager=sm, group_display_count=4).show()#'GLU', 'GABA_Dendrite', 'GABA_Soma', 'GABA_AIS'

    score = 0
    score += train_and_generate_text(SORN, 20000, 5000, 3000, display=print_info, stdp_off=True, same_timestep_without_feedback_loop=True, steps_recovery=1000, storage_manager=sm)#, steps_recovery=15000
    #score += get_oscillation_score_hierarchical(SORN, 0, 5000)
    return score
def run(tag='hierarchical', ind=[], par={'N_e': 900, 'TS': [1]}):
    sm = StorageManager(tag, random_nr=True, print_msg=display)
    sm.save_param_dict(par)

    #source = FDTGrammarActivator_New(tag='grammar_act', random_blocks=True, input_density=15/par['N_e'])#.plot_char_input_statistics()#output_size=par['N_e']#15
    #source = LongDelayGrammar(tag='grammar_act', output_size=par['N_e'], random_blocks=True, mode=['very simple'], input_density=10/par['N_e'])#.print_test().plot_char_input_statistics()
    #source = Line_Patterns(tag='image_act', group_possibility=1.0, grid_width=20, grid_height=20, center_x=list(range(20)), center_y=30 / 2, degree=90, line_length=60)
    #print(source.get_text_score('. parrot likes trees. wolf wolf wolf..'))
    source = MNIST_Patterns(tag='image_act',
                            group_possibility=1.0,
                            repeat_same_label_time=1)

    SORN = Network()

    for timecale in par['TS']:

        e_ng = NeuronGroup(
            net=SORN,
            tag='PC_Neurons_{},prediction_source'.format(timecale),
            size=get_squared_dim(int(par['N_e'])),
            behaviour={
                2:
                SORN_init_neuron_vars(iteration_lag=timecale,
                                      init_TH='0.05;+-80%',
                                      activation_function='binary'),
                3:
                SORN_init_afferent_synapses(
                    transmitter='GLU',
                    density='13%',
                    distribution='lognormal(0,[0.89#0])',
                    normalize=True,
                    partition_compensation=True),
                4:
                SORN_init_afferent_synapses(
                    transmitter='GABA',
                    density='45%',
                    distribution='lognormal(0,[0.80222#1])',
                    normalize=True),  #electrical synapses
                12:
                SORN_slow_syn(transmitter='GLU', strength='[0.14#2]'),
                13:
                SORN_slow_syn(transmitter='GABA', strength='-[0.14#3]'),
                17:
                SORN_fast_syn(transmitter='GABA', strength='-[0.03#4]'),
                18:
                SORN_random_act(chance=0.001),
                19:
                SORN_input_collect(),

                #20: SORN_Refractory(strength=1, factor='0.5;+-50%'),
                21:
                SORN_STDP(eta_stdp='[0.0001#5]', prune_stdp=False),
                22:
                SORN_SN(syn_type='GLU', clip_max=None, init_norm_factor=1.0),
                23:
                SORN_IP_TI(h_ip='lognormal_real_mean([0.04#6], [0.2944#7])',
                           eta_ip='[0.0006#8];+-50%',
                           integration_length='[30#18];+-50%',
                           clip_min=0.0),
                25:
                SORN_NOX(mp='self.partition_sum(n)',
                         eta_nox='[0.0005#9];+-50%'),  #0.4
                26:
                SORN_SC_TI(h_sc='lognormal_real_mean([0.01#10], [0.2944#11])',
                           eta_sc='[0.1#12];+-50%',
                           integration_length='1'),
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]'),
                30:
                SORN_finish()
            })

        SynapseGroup(net=SORN,
                     src=e_ng,
                     dst=e_ng,
                     tag='GLU,e->e',
                     connectivity='(s_id!=d_id)*in_box(10)',
                     partition=True)  #.partition([10, 10], [6, 6])
        SynapseGroup(net=SORN,
                     src=e_ng,
                     dst=e_ng,
                     tag='GABA,e->e',
                     connectivity='(s_id!=d_id)*in_box(10)',
                     partition=True)  #.partition([10, 10], [6, 6])

        e_ng.add_behaviour(
            9, SORN_external_input(strength=0.3, pattern_groups=[source]))
        if timecale == 1:
            e_ng.add_behaviour(101, Recorder(['n.pattern_index'],
                                             tag='inp_rec'))
        else:
            #forward synapses
            SynapseGroup(net=SORN,
                         src=last_e_ng,
                         dst=e_ng,
                         tag='GLU,e->e(+1)',
                         connectivity='in_box(10)',
                         partition=True)  #.partition([10, 10], [6, 6])
            SynapseGroup(net=SORN,
                         src=last_e_ng,
                         dst=e_ng,
                         tag='GABA,e->i(+1)',
                         connectivity='in_box(10)',
                         partition=True)  #.partition([10, 10], [2, 2])
            #backward synapses
            SynapseGroup(net=SORN,
                         src=e_ng,
                         dst=last_e_ng,
                         tag='GLU,e(+1)->e',
                         connectivity='in_box(10)',
                         partition=True)  #.partition([10, 10], [6, 6])
            SynapseGroup(net=SORN,
                         src=e_ng,
                         dst=last_e_ng,
                         tag='GABA,e(+1)->i',
                         connectivity='in_box(10)',
                         partition=True)  #.partition([10, 10], [2, 2])

        last_e_ng = e_ng
        e_ng.color = (0, 0, 255, 255)

    SORN.set_marked_variables(ind, info=(ind == []))
    SORN.initialize(info=False)

    ############################################################################################################################################################

    score = 0

    #SORN.simulate_iterations(10000, 100, measure_block_time=True, disable_recording=True)

    Network_UI(SORN,
               label='no inh neurons',
               storage_manager=sm,
               group_display_count=1).show()

    score += train_and_generate_text(
        SORN,
        15000,
        5000,
        2000,
        display=False,
        stdp_off=True,
        same_timestep_without_feedback_loop=True,
        steps_recovery=15000)  #, steps_recovery=15000
    #score += get_oscillation_score_hierarchical(SORN, 0, 5000)
    return score
def run(
    attrs={
        'name': 'maze',
        'ind': [],
        'N_e': 900,
        'TS': [1],
        'ff': True,
        'fb': True,
        'plastic': 15000
    }):
    so = True

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    sm = StorageManager(attrs, random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    #source = LongDelayGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, mode=['simple'], input_density=0.01)
    maze = Maze(level='default')

    SORN = Network()  #behaviour={maze.get_network_behaviour()}

    SORN.maze = maze

    for layer, timescale in enumerate(attrs['TS']):

        e_location = NeuronGroup(
            net=SORN,
            tag='E_location_{}'.format(timescale),
            size=maze.get_location_neuron_dimension(),
            behaviour={
                2:
                SORN_init_neuron_vars(timescale=timescale),
                3:
                SORN_init_afferent_synapses(
                    transmitter='GLU',
                    density='full',
                    distribution='uniform(0.1,0.11)',
                    normalize=True,
                    partition_compensation=True
                ),  # 0.89 uniform(0.1,0.11)uniform(0,[0.95#0])
                4:
                SORN_init_afferent_synapses(
                    transmitter='GABA',
                    density='full',
                    distribution='uniform(0.1,0.11)',
                    normalize=True
                ),  # 0.80222 uniform(0.1,0.11) lognormal(0,[0.4#1])
                9:
                maze.get_location_neuron_behaviour(),  ###############
                12:
                SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                13:
                SORN_slow_syn(transmitter='GABA',
                              strength='-[0.1698#3]',
                              so=False),
                17:
                SORN_fast_syn(transmitter='GABA',
                              strength='-[0.1#4]',
                              so=False),  # 0.11045
                18:
                SORN_generate_output(init_TH='0.1;+-100%'),
                19:
                SORN_buffer_variables(),
                20:
                SORN_Refractory_Analog(factor='0.5;+-50%'),
                21:
                SORN_STDP(eta_stdp='[0.0015#5]',
                          weight_attr='W_temp',
                          STDP_F={
                              -5: 0.2,
                              -4: 0.4,
                              -3: 0.6,
                              -2: 0.8,
                              -1: 1
                          }),
                22:
                SORN_temporal_synapses(syn_type='GLU',
                                       behaviour_norm_factor=1.0),
                #22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=1.0),
                23:
                SORN_IP_TI(h_ip='lognormal_real_mean([0.004#6], [0.2944#7])',
                           eta_ip='[0.0006#8];+-50%',
                           integration_length='[15#18];+-50%',
                           clip_min=None),
                25:
                SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                26:
                SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])',
                           eta_sc='[0.1#12];+-50%',
                           integration_length='1'),  # 60;+-50% #0.05
                27:
                SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]'),

                #29: SORN_dopamine(),
            })

        i_location = NeuronGroup(
            net=SORN,
            tag='I_location{}'.format(timescale),
            size=maze.get_inhibitory_location_neuron_dimension(),
            behaviour={
                2:
                SORN_init_neuron_vars(timescale=timescale),
                3:
                SORN_init_afferent_synapses(
                    transmitter='GLU',
                    density='50%',
                    distribution='uniform(0.1,0.11)',
                    normalize=True),  #lognormal(0,[0.87038#14])
                4:
                SORN_init_afferent_synapses(
                    transmitter='GABA',
                    density='20%',
                    distribution='uniform(0.1,0.11)',
                    normalize=True),  #lognormal(0,[0.82099#15])
                11:
                SORN_slow_syn(transmitter='GABA',
                              strength='-[0.1838#16]',
                              so=so),
                14:
                SORN_fast_syn(transmitter='GLU', strength='[1.5#16]',
                              so=so),  # 1.5353
                15:
                SORN_fast_syn(transmitter='GABA',
                              strength='-[0.08#17]',
                              so=False),  # 0.08
                18:
                SORN_generate_output(init_TH='0.1;+-0%'),
                19:
                SORN_buffer_variables(),
                20:
                SORN_Refractory_Analog(factor='0.2;0.7'),
            })
        '''
        e_vision = NeuronGroup(net=SORN, tag='E_vision_{}'.format(timescale), size=maze.get_vision_neuron_dimension(), behaviour={
                2: SORN_init_neuron_vars(timescale=timescale),
                3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution='lognormal(0,[0.95#0])', normalize=True, partition_compensation=True),  # 0.89 uniform(0.1,0.11)
                4: SORN_init_afferent_synapses(transmitter='GABA', density='45%', distribution='lognormal(0,[0.4#1])', normalize=True),  # 0.80222 uniform(0.1,0.11)

                9: maze.get_vision_neuron_behaviour(),

                12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                13: SORN_slow_syn(transmitter='GABA', strength='-[0.1698#3]', so=False),
                17: SORN_fast_syn(transmitter='GABA', strength='-[0.1#4]', so=False),  # 0.11045
                18: SORN_generate_output(init_TH='0.01'),
                19: SORN_buffer_variables(),

                #20: SORN_Refractory(factor='0.5;+-50%'),
                #21: SORN_STDP(eta_stdp='[0.00015#5]'),
                #22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=5.0),

                #23: SORN_IP_TI(h_ip='0.04', eta_ip='0.001', integration_length='[15#18];+-50%', clip_min=None),
                #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')

            })
        '''

        e_action = NeuronGroup(
            net=SORN,
            tag='E_action_{}'.format(timescale),
            size=maze.get_action_neuron_dimension(),
            behaviour={
                2:
                SORN_init_neuron_vars(timescale=timescale),
                3:
                SORN_init_afferent_synapses(
                    transmitter='GLU',
                    density='full',
                    distribution='uniform(0.1,0.11)',
                    normalize=True,
                    partition_compensation=True),  # 0.89 lognormal(0,[0.5#0])
                12:
                SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                13:
                SORN_slow_syn(transmitter='GABA',
                              strength='-[0.1698#3]',
                              so=False),
                17:
                SORN_fast_syn(transmitter='GABA',
                              strength='-[0.1#4]',
                              so=False),  # 0.11045
                18:
                SORN_generate_output(init_TH='0.01'),
                19:
                SORN_buffer_variables(),
                20:
                SORN_Refractory_Analog(factor='0.7;+-50%'),
                21:
                SORN_STDP(eta_stdp='[0.0015#5]',
                          STDP_F={
                              -1: 1,
                              1: 0
                          },
                          weight_attr='W_temp'),  #bigger #todo!!!!
                #22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=5.0),
                22:
                SORN_temporal_synapses(syn_type='GLU',
                                       behaviour_norm_factor=5.0),
                23:
                SORN_IP_TI(h_ip='0.04',
                           eta_ip='0.001',
                           integration_length='[15#18];+-50%',
                           clip_min=None),
                #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')
                29:
                SORN_dopamine(),
                30:
                maze.get_action_neuron_behaviour()
            })
        '''
        e_reward = NeuronGroup(net=SORN, tag='E_reward_{}'.format(timescale), size=maze.get_reward_neuron_dimension(), behaviour={
                2: SORN_init_neuron_vars(timescale=timescale),
                3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution='lognormal(0,[0.95#0])', normalize=True, partition_compensation=True),

                9: maze.get_reward_neuron_behaviour(),

                12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                18: SORN_generate_output(init_TH='0.01'),
                19: SORN_buffer_variables(),

                #20: SORN_Refractory(factor='0.5;+-50%'),
                21: SORN_STDP(eta_stdp='[0.00015#5]'),
                22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=0.1),

                #23: SORN_IP_TI(h_ip='0.04', eta_ip='0.001', integration_length='[15#18];+-50%', clip_min=None),
                #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')
            })
        '''

        e_punishment = NeuronGroup(
            net=SORN,
            tag='E_punishment_{}'.format(timescale),
            size=maze.get_punishment_neuron_dimension(),
            behaviour={
                2:
                SORN_init_neuron_vars(timescale=timescale),
                3:
                SORN_init_afferent_synapses(
                    transmitter='GLU',
                    density='full',
                    distribution='lognormal(0,[0.95#0])',
                    normalize=True,
                    partition_compensation=True),
                9:
                maze.get_punishment_neuron_behaviour(),
                12:
                SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                18:
                SORN_generate_output(init_TH='0.01'),
                19:
                SORN_buffer_variables(),

                #20: SORN_Refractory(factor='0.5;+-50%'),
                21:
                SORN_STDP(eta_stdp='[0.00015#5]'),  #todo!!!!
                22:
                SORN_SN(syn_type='GLU',
                        clip_max=None,
                        behaviour_norm_factor=0.1),

                #23: SORN_IP_TI(h_ip='0.04', eta_ip='0.001', integration_length='[15#18];+-50%', clip_min=None),
                #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')
            })

        i_location['structure', 0].stretch_to_equal_size(e_location)

        SynapseGroup(net=SORN,
                     src=e_location,
                     dst=e_location,
                     tag='GLU,GLU_same',
                     connectivity='(s_id!=d_id)*in_box(10)',
                     partition=True)
        SynapseGroup(net=SORN,
                     src=e_location,
                     dst=i_location,
                     tag='GLU',
                     connectivity='in_box(10)',
                     partition=True)
        SynapseGroup(net=SORN,
                     src=i_location,
                     dst=e_location,
                     tag='GABA',
                     connectivity='in_box(10)',
                     partition=True)
        SynapseGroup(net=SORN,
                     src=i_location,
                     dst=i_location,
                     tag='GABA',
                     connectivity='(s_id!=d_id)*in_box(10)',
                     partition=True)

        #SynapseGroup(net=SORN, src=e_vision, dst=e_location, tag='GLU', partition=True)
        SynapseGroup(net=SORN,
                     src=e_location,
                     dst=e_action,
                     tag='GLU',
                     partition=True)

        #SynapseGroup(net=SORN, src=e_location, dst=e_reward, tag='GLU', partition=True)
        #SynapseGroup(net=SORN, src=e_reward, dst=e_location, tag='DOP+', partition=True)
        #SynapseGroup(net=SORN, src=e_reward, dst=e_action, tag='DOP+', partition=True)

        SynapseGroup(net=SORN,
                     src=e_location,
                     dst=e_punishment,
                     tag='GLU',
                     partition=True)
        SynapseGroup(net=SORN,
                     src=e_punishment,
                     dst=e_location,
                     tag='DOP-',
                     partition=True)
        SynapseGroup(net=SORN,
                     src=e_punishment,
                     dst=e_action,
                     tag='DOP-',
                     partition=True)

        if __name__ == '__main__' and attrs.get('UI', False):
            e_location.color = (0, 0, 255, 255)
            i_location.color = (255, 0, 0, 255)
            e_action.color = (255, 255, 0, 255)
            #e_vision.color = (0, 255, 255, 255)
            #e_reward.color = (100, 255, 100, 255)
            e_punishment.color = (255, 100, 100, 255)

    SORN.set_marked_variables(attrs['ind'],
                              info=print_info,
                              storage_manager=sm)
    SORN.initialize(info=False)

    ###################################################################################################################

    if __name__ == '__main__' and attrs.get('UI', False):
        #default_modules.insert(0, maze_tab())
        #Network_UI(SORN, label='SORN UI default setup', storage_manager=sm, group_display_count=4, reduced_layout=True).show()
        Network_UI(SORN,
                   modules=get_default_UI_modules() + [maze_tab()],
                   label='SORN UI K_WTA',
                   storage_manager=sm,
                   group_display_count=1,
                   reduced_layout=False).show()

    return 0
Exemple #7
0
def run(attrs={'name': 'KWTA', 'ind': [], 'N_e': 900, 'plastic': 15000}):

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    sm = StorageManager(attrs['name'], random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    source = FewSentencesGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015, frequency_adjustment=True)#21



    SORN = Network()

    e_ng = NeuronGroup(net=SORN, tag='PC_{},prediction_source'.format(1), size=get_squared_dim(attrs['N_e']), behaviour={
                2: SORN_init_neuron_varsTF(timescale=1),
                3: SORN_init_afferent_synapsesTF(transmitter='GLU', density='90%', distribution='uniform(0.1,1.0)', normalize=True),#20%#lognormal(0,[0.95#1]) #[13#0]% #, partition_compensation=True , partition_compensation=True #lognormal(0,0.95)
                #4: SORN_init_afferent_synapses(transmitter='GABA', density='[30#1]%', distribution='uniform(0.0,1.0)', normalize=True),
                5: SORN_init_afferent_synapsesTF(transmitter='GLU_cluster', density='90%', distribution='uniform(0.1,1.0)', normalize=True),

                10.0: SORN_slow_syn_simpleTF(transmitter='GLU', strength='1.0'),
                10.1: SORN_IP_WTA_apply_TF(),
                10.15: WTA_refrac_applyTF(strengthfactor='[0.1#0]'),#0.1 #attrs['refrac']
                10.2: SORN_generate_output_K_WTA_partitionedTF_Experimental(partition_size=7, K='[0.02#1]'),

                10.3: SORN_slow_syn_simpleTF(transmitter='GLU_cluster', strength='1.0'),
                10.4: SORN_generate_output_K_WTA_partitionedTF_Experimental(partition_size=7, K='[0.02#1]', filter_temporal_output=False),

                10.5: SORN_slow_syn_simpleTF(transmitter='GLU_cluster', strength='1.0'),
                10.6: SORN_generate_output_K_WTA_partitionedTF_Experimental(partition_size=7, K='[0.02#1]', filter_temporal_output=False),

                10.7: SORN_slow_syn_simpleTF(transmitter='GLU_cluster', strength='1.0'),
                10.8: SORN_generate_output_K_WTA_partitionedTF_Experimental(partition_size=7, K='[0.02#1]', filter_temporal_output=False),


                #15: SORN_buffer_variables(random_temporal_output_shift=False),

                18: WTA_refracTF(decayfactor=0.5),
                20: SORN_IP_WTA_TF(h_ip='lognormal_real_mean([0.02#1], [0.2944#2])', eta_ip='[0.007#3]', target_clip_min=None, target_clip_max=None), #-1.0 #1.0 #0.007
                21.1: STDP_TF(syn_type='GLU', eta_stdp='[0.00015#4]'),#, 0: 1 #[0.00015#7] #0.5, 0: 3.0
                21.2: STDP_TF_simu(syn_type='GLU_cluster', eta_stdp='[0.00015#5]'),  #[0.00015#7]
                22: SORN_SN_TF(syn_type='GLU', norm_factor=1.0),
                23: SORN_SN_TF(syn_type='GLU_cluster', norm_factor='[0.3#6]')#0.1
            })


    # SynapseGroup(net=SORN, src=retina, dst=e_ng, tag='GLU,GLU_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

    receptive_field = 18
    #receptive_field = int(18*math.sqrt(attrs['N_e']/1400))
    print(receptive_field)

    SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU,syn', connectivity='(s_id!=d_id)*in_box({})'.format(receptive_field))#, partition=True)#14
    SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU_cluster,syn', connectivity='(s_id!=d_id)*in_box({})'.format(receptive_field))#, partition=True)
    #SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GABA,GABA_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

    e_ng.add_behaviour(9, SORN_external_inputTF(strength=1.0, pattern_groups=[source]))

    if __name__ == '__main__' and attrs.get('UI', False):
        e_ng.color = get_color(0, 1)

    SORN.set_marked_variables(attrs['ind'], info=print_info, storage_manager=sm)
    SORN.initialize(info=False)

    ###################################################################################################################

    if __name__ == '__main__' and attrs.get('UI', False):
        my_modules = get_default_UI_modules(['output', 'exhaustion_value', 'weight_norm_factor'])+get_my_default_UI_modules()
        my_modules[0] = UI_sidebar_activity_module(1, add_color_dict={'output': (255, 255, 255), 'Input_Mask': (-100, -100, -100)})
        #my_modules[1] = multi_group_plot_tab(['output', 'exhaustion_value', 'weight_norm_factor'])  # , 'nox', 'refractory_counter'
        my_modules[8] = single_group_plot_tab({'activity': (0, 0, 0), 'excitation': (0, 0, 255), 'inhibition': (255, 0, 0), 'input_act': (255, 0, 255),'exhaustion_value': (0, 255, 0)})
        Network_UI(SORN, modules=my_modules, label='SORN UI K_WTA', storage_manager=sm, group_display_count=1, reduced_layout=False).show()

    '''
    if __name__ == '__main__' and attrs.get('UI', False):
        my_modules = [
            UI_sidebar_activity_module(add_color_dict={'output': (255, 255, 255), 'Input_Mask': (-100, -100, -100)}),

            weight_tab(weight_attrs=['W']),

            sidebar_fast_forward_module(),
            sidebar_save_load_module(),
            multi_group_plot_tab(['output'], tensorflow=True),

            # fourier_tab(parameter='voltage'),
            info_tab(),
        ]

        Network_UI(SORN, modules=my_modules, label='Tensorflow Test', storage_manager=None, group_display_count=1).show()

        #my_modules = get_default_UI_modules()
        #my_modules[1] = multi_group_plot_tab(['output', 'exhaustion_value', 'weight_norm_factor'])  # , 'nox', 'refractory_counter'
        #my_modules[18] = single_group_plot_tab({'activity': (0, 0, 0), 'excitation': (0, 0, 255), 'inhibition': (255, 0, 0), 'input_act': (255, 0, 255),'exhaustion_value': (0, 255, 0)})
        #Network_UI(SORN, modules=my_modules, label='SORN UI K_WTA', storage_manager=sm, group_display_count=1, reduced_layout=False).show()
    '''

    score = 0
    plastic_steps = attrs.get('plastic', 20000)

    #for i in range(1):
    #    #sm = StorageManager(attrs['name'] + '[{:03d}]'.format(i + 1), random_nr=True, print_msg=print_info)
    #    #sm.save_param_dict(attrs)
    #    sm=None
    #    score += train_and_generate_text(SORN, plastic_steps, 5000, 1000, display=print_info, stdp_off=True, same_timestep_without_feedback_loop=True, steps_recovery=1000, storage_manager=sm, return_key='right_sentences_square_score')

    score = 0

    #print(train_and_generate_text(SORN, plastic_steps, 5000, 1000, display=print_info, stdp_off=True, same_timestep_without_feedback_loop=True, steps_recovery=5000, storage_manager=sm, return_key='right_sentences_square_score'))
    #print('Output:')
    #print(predict_text_max_source_act(SORN, plastic_steps, 5000, 5000, display=True, stdp_off=True))#plastic_steps, 5000, 1000

    #print('score=', score)

    #print(SORN.simulate_iteration(True))

    return get_max_text_score(SORN, plastic_steps, 5000, 5000, display=True, stdp_off=True, return_key='right_word_square_score')
Exemple #8
0
def run(attrs={'name': 'KWTA', 'ind': [], 'N_e': 900, 'TS': [1], 'ff': True, 'fb': True, 'plastic': 15000}):
    so = True

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    sm = StorageManager(attrs['name'], random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    source = FewSentencesGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)
    #source = SingleWordGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)
    #source = FewLongSentencesGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)
    #source.plot_char_frequency_histogram(20)

    SORN = Network()

    for layer, timescale in enumerate(attrs['TS']):

        e_ng = NeuronGroup(net=SORN, tag='PC_{},prediction_source'.format(timescale), size=get_squared_dim(int(attrs['N_e'] / timescale)), behaviour={
                2: SORN_init_neuron_vars(timescale=timescale),
                3: SORN_init_afferent_synapses(transmitter='GLU', density='[90#0]%', distribution='lognormal(0,0.95)', normalize=True),#20%#lognormal(0,[0.95#1]) #[13#0]% #, partition_compensation=True , partition_compensation=True
                4: SORN_init_afferent_synapses(transmitter='GABA', density='[30#1]%', distribution='uniform(0.0,1.0)', normalize=True),

                12: SORN_slow_syn(transmitter='GLU', strength='1.0', so=so),
                12.1: SORN_WTA_iSTDP(eta_iSTDP='[0.00015#2]'),
                12.2: SORN_SN(syn_type='GABA'),
                13.0: SORN_IP_WTA(h_ip='lognormal_real_mean(0.04, 0.2944)', eta_ip='[0.006#3]', clip_min=None),
                13.4: SORN_generate_output_K_WTA_partitioned(K='[0.12#4]'),

                13.5: SORN_WTA_fast_syn(transmitter='GABA', strength='-[0.5#5]', so=so),#[0.1383#2]SORN_fast_syn
                18: SORN_generate_output_K_WTA_partitioned(K='[0.04#6]', filter_temporal_output=True),

                19: SORN_buffer_variables(random_temporal_output_shift=False),

                21: SORN_STDP(eta_stdp='[0.00015#7]'),
                22: SORN_SN(syn_type='GLU')
            })

        #smaller number of active neuons for more competition?

        # SynapseGroup(net=SORN, src=retina, dst=e_ng, tag='GLU,GLU_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

        SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU,GLU_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)
        SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GABA,GABA_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

        #if layer > 0:
        #    if attrs.get('ff', True):  # forward synapses
        #        SynapseGroup(net=SORN, src=last_e_ng, dst=e_ng, tag='GLU,GLU_ff', connectivity='in_box(10)', partition=False)
        #    if attrs.get('fb', False):  # backward synapses
        #        SynapseGroup(net=SORN, src=e_ng, dst=last_e_ng, tag='GLU,GLU_fb', connectivity='in_box(10)', partition=False)

        e_ng.add_behaviour(9, SORN_external_input(strength=1.0, pattern_groups=[source]))

        #last_e_ng = e_ng

        if __name__ == '__main__' and attrs.get('UI', False):
            e_ng.color = get_color(0, timescale)

    SORN.set_marked_variables(attrs['ind'], info=print_info, storage_manager=sm)
    SORN.initialize(info=False)

    #for s in e_ng.afferent_synapses['GLU']:
    #    s.W = s.W-np.mean(s.W)

    ###################################################################################################################

    if __name__ == '__main__' and attrs.get('UI', False):
        Network_UI(SORN, label='SORN UI K_WTA', storage_manager=sm, group_display_count=1, reduced_layout=False).show()

    score = 0
    plastic_steps = attrs.get('plastic', 20000)

    for i in range(1):
        sm = StorageManager(attrs['name'] + '[{:03d}]'.format(i + 1), random_nr=True, print_msg=print_info)
        sm.save_param_dict(attrs)
        score += train_and_generate_text(SORN, plastic_steps, 5000, 1000, display=print_info, stdp_off=True, same_timestep_without_feedback_loop=True, steps_recovery=1000, storage_manager=sm)

    print('score=', score)

    return score
def run(tag, ind=[], par={'N_e':[1800], 'TS':[1]}):
    name = 'DrumBeats_'+'_'+tag+str(par['N_e'])+'N_e_'+str(par['TS'])

    sm = StorageManager(main_folder_name=tag, folder_name=name, random_nr=False, print_msg=display)
    sm.save_param_dict(par)

    source = DrumBeatActivator(tag='drum_act', which_tracks=[100], filter_silence=False, THR_similar_tracks=0.5, input_density=0.015, offtoken=True, ontoken=True, include_inverse_alphabet=False)#, include_inverse_alphabet= True)#output_size=par['N_e']

    inh_thr = ['0.1;+-0%','0.1;+-0%', '0.1;+-0%']
    exc_thr = ['0.1;+-100%', '0.1;+-100%', '0.1;+-100%']

    SORN = Network()

    for i, lag in enumerate(par['TS']):#
        e_ng = NeuronGroup(net=SORN, tag='exc_cell_{},prediction_source,text_input_group'.format(lag), size=get_squared_dim(int(par['N_e'][i])), behaviour={
            2: SORN_init_neuron_vars(iteration_lag=lag, init_TH=exc_thr[i]),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='13%', distribution='lognormal(0,[0.89#0])', normalize=True, partition_compensation=True),
            4: SORN_init_afferent_synapses(transmitter='GABA', density='50%', distribution='lognormal(0,[0.80222#1])', normalize=True),

            12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
            13: SORN_slow_syn(transmitter='GABA', strength='-[0.1698#3]', so=so),

            17: SORN_fast_syn(transmitter='GABA', strength='-[0.11045#4]', so=so),
            18: SORN_input_collect(),

            19: SORN_Refractory(factor='0.5;+-50%'),

            21: SORN_STDP(eta_stdp='[0.0001#5]', prune_stdp=False),
            22: SORN_SN(syn_type='GLU', clip_max=None, init_norm_factor=1.0),

            23: SORN_IP_TI(h_ip='lognormal_real_mean([0.04#6], [0.2944#7])', eta_ip='[0.0006#8];+-50%', integration_length='[30#18];+-50%', clip_min=None),#, gap_percent=10 #30;+-50% #0.0003
            #24: SORN_NOX(mp='np.mean(n.output_new)', eta_nox='[0.3#9];+-50%', behaviour_enabled=False), #0.4
            25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.3#9];+-50%'),  # 0.4

            26: SORN_SC_TI(h_sc='lognormal_real_mean([0.01#10], [0.2944#11])', eta_sc='[0.1#12];+-50%', integration_length='1'), #60;+-50% #0.05
            27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]'),

            30: SORN_finish(),

            #99: SynapseRecorder(['[np.sum(s.slow_add)]'], tag='ex_glu_syn_rec'),
            #100: Recorder(['n.output'], tag='exc_out_rec')
        })

        i_ng = NeuronGroup(net=SORN, tag='inh_cell_{}'.format(lag), size=get_squared_dim(int(0.2 * (par['N_e'][i]))), behaviour={
            2: SORN_init_neuron_vars(iteration_lag=lag, init_TH=inh_thr[i]),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='45%', distribution='lognormal(0,[0.87038#14])', normalize=True),  # 450
            4: SORN_init_afferent_synapses(transmitter='GABA', density='20%', distribution='lognormal(0,[0.82099#15])', normalize=True),  # 40

            #11: SORN_slow_syn(transmitter='GABA', strength='-[0.1838#16]', so=so),
            14: SORN_fast_syn(transmitter='GLU', strength='[1.5#16]', so=so),#1.5353
            15: SORN_fast_syn(transmitter='GABA', strength='-[0.08#17]', so=so),#0.08
            18: SORN_input_collect(),

            19: SORN_Refractory(factor='0.1;0.4'),

            #23: SORN_IP_TI(h_ip='lognormal_real_mean([0.08#6], [0.2944#7])', eta_ip='[0.0003#8];+-50%', integration_length='30;+-50%', clip_min=None),

            30: SORN_finish(),

            #100: Recorder(['n.output'], tag='inh_out_rec')
        })

        i_ng['structure', 0].stretch_to_equal_size(e_ng)


        SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU,ee', connectivity='(s_id!=d_id)*in_box(10)', partition=True)#.partition([10, 10], [4, 4])
        SynapseGroup(net=SORN, src=e_ng, dst=i_ng, tag='GLU,ie', connectivity='(s_id!=d_id)*in_box(10)', partition=True)#.partition([5, 5], [2, 2])
        SynapseGroup(net=SORN, src=i_ng, dst=e_ng, tag='GABA,ei', connectivity='(s_id!=d_id)*in_box(10)', partition=True)
        SynapseGroup(net=SORN, src=i_ng, dst=i_ng, tag='GABA,ii', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

        if lag == 1:
            i_ng.add_behaviour(10, SORN_external_input(strength=1.0, pattern_groups=[source]))
            e_ng.add_behaviour(10, SORN_external_input(strength=1.0, pattern_groups=[source]))
            #e_ng.add_behaviour(101, Recorder(['n.pattern_index'], tag='inp_rec'))
        else:
            #forward synapses
            SynapseGroup(net=SORN, src=last_e_ng, dst=e_ng, tag='GLU,eeff')#.partition([10, 10], [4, 4])
            SynapseGroup(net=SORN, src=last_e_ng, dst=i_ng, tag='GABA,ieff')#.partition([5, 5], [2, 2])
            #backward synapses
            SynapseGroup(net=SORN, src=e_ng, dst=last_e_ng, tag='GLU,eebw')#.partition([10, 10], [4, 4])
            SynapseGroup(net=SORN, src=e_ng, dst=last_i_ng, tag='GABA,iebw')#.partition([5, 5], [2, 2])

        last_e_ng = e_ng # excitatory neuron group
        last_i_ng = i_ng # inhibitory neuron group
        e_ng.color = (0,0,255,255)
        i_ng.color = (255,0,255,255)
        e_ng.pattern_index=0
        i_ng.pattern_index=0

    SORN.set_marked_variables(ind, info=(ind == []))

    SORN.initialize(info=False)
    ############################################################################################################################################################

    score_spont = score_predict_train = score_predict_test = 0

    import Exploration.Network_UI.Network_UI as SUI
    SUI.Network_UI(SORN, label='SORN UI default setup', storage_manager=sm, group_display_count=2, reduced_layout=False).show()

    #SORN = run_plastic_phase(SORN, steps_plastic=10000, display=True, storage_manager=sm)#1000000

    #print(set(np.nonzero(np.array(SORN['drum_act', 0].seen))[1]))

    readout, X_train, Y_train, X_test, Y_test = train_readout(SORN, steps_train=10000, steps_test=100, source = SORN['drum_act', 0], display=True, stdp_off=True, storage_manager=sm)#100000
    
    score_predict_train = get_score_predict_next_step(SORN, SORN['drum_act', 0],readout, X_train, Y_train, display=True, stdp_off=True, storage_manager=sm)

    score_predict_test = get_score_predict_next_step(SORN, SORN['drum_act', 0],readout, X_test, Y_test, display=True, stdp_off=True, storage_manager=sm)
    
    score_spont = get_score_spontaneous_music(SORN, SORN['drum_act', 0], readout, split_tracks=False, steps_spont=2000, display=True, stdp_off=True, 
    same_timestep_without_feedback_loop=False, steps_recovery=0, create_MIDI=True, storage_manager=sm)#, steps_recovery=15000
    
    sm.save_obj('score_train', score_predict_train)
    sm.save_obj('score_test', score_predict_test)
    sm.save_obj('score_spontaneous', score_spont)

    #plot_frequencies_poly(score_spont, path = sm.absolute_path+'frequencies', title='{} Ne, {} lag'.format(par['N_e'], par['TS']))

    return score_predict_train, score_predict_test, score_spont
Exemple #10
0
def run(attrs={'name':'hierarchical', 'ind':[], 'N_e':900, 'TS':[1], 'ff':True, 'fb':True, 'plastic':15000}):

    so = True

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    #sm = None
    sm = StorageManager(attrs['name'], random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    #grammar mode: what level17_short simple

    #source = FDTGrammarActivator_New(tag='grammar_act', random_blocks=True, input_density=0.015)#15/par['N_e']#.plot_char_input_statistics()#output_size=par['N_e']#15
    #print(len(source.alphabet))
    #source = LongDelayGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, mode=['level17_short'], input_density=0.015)#.print_test()#.plot_char_input_statistics()#10/par['N_e']

    #source = SingleWordGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)

    source = FewSentencesGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, input_density=0.015)

    #print(source.print_test())

    #print([t for t in source.get_all_grammar_transitions() if len(t)==2])

    #source.plot_char_input_statistics()
    #print(len(source.alphabet))
    #img_source = Line_Patterns(tag='image_act', group_possibility=1.0, grid_width=20, grid_height=20, center_x=list(range(20)), center_y=20 / 2, degree=90, line_length=60, select_mode='random')
    #print(source.get_text_score('. parrot likes trees. wolf wolf wolf..'))

    SORN = Network()




    #retina = NeuronGroup(net=SORN, tag='retina', size=get_squared_dim(20*20), behaviour={
    #        2: SORN_init_neuron_vars(timescale=1),

            #9: SORN_external_input(strength=1.0, pattern_groups=[img_source]),

            #18: SORN_generate_output(init_TH='0.1;+-100%'),
            #19: SORN_buffer_variables(random_temporal_output_shift=False),

            #20: SORN_Refractory_Analog(factor='0.5;+-50%'),
        #})


    for layer, timescale in enumerate(attrs['TS']):

        e_ng = NeuronGroup(net=SORN, tag='PC_{},prediction_source'.format(timescale), size=get_squared_dim(int(attrs['N_e']/timescale)), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution='lognormal(0,[0.95#0])', normalize=True, partition_compensation=True),#0.89 uniform(0.1,0.11)#13%
            4: SORN_init_afferent_synapses(transmitter='GABA', density='full', distribution='lognormal(0,[0.4#1])', normalize=True),#0.80222 uniform(0.1,0.11)#45%

            12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
            13: SORN_slow_syn(transmitter='GABA', strength='-[0.1698#3]', so=False),
            17: SORN_fast_syn(transmitter='GABA', strength='-[0.1#4]', so=False),#0.11045
            18: SORN_generate_output(init_TH='0.1;+-100%'),
            19: SORN_buffer_variables(random_temporal_output_shift=False),

            #20: SORN_Refractory_Digital(factor='0.5;+-50%', threshold=0.1),
            20: SORN_Refractory_Analog(factor='0.5;+-50%'),
            21: SORN_STDP(eta_stdp='[0.00015#5]'),#, STDP_F={-4:-0.01,-3:0.01,-2:0.1,-1:0.5,0:0.2,1:-0.3,2:-0.1,3:-0.05}, plot=True),#{-2:0.1,-1:0.5,0:0.2,1:-0.3,2:-0.1}
            22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=1.0),
            #22.1: SORN_SN(syn_type='GLU_same', clip_max=None, behaviour_norm_factor=0.6),
            #22.2: SORN_SN(syn_type='GLU_ff', clip_max=None, behaviour_norm_factor=0.25),
            #22.3: SORN_SN(syn_type='GLU_fb', clip_max=None, behaviour_norm_factor=0.25),7z

            23: SORN_IP_TI(h_ip='lognormal_real_mean([0.04#6], [0.2944#7])', eta_ip='[0.0006#8];+-50%', integration_length='[15#19];+-50%', clip_min=None),#30          #, gap_percent=10 #30;+-50% #0.0003 #np.mean(n.output_new)
            25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
            26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%', integration_length='1'),#60;+-50% #0.05
            27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')
        })

        i_ng = NeuronGroup(net=SORN, tag='Inter_{}'.format(timescale), size=get_squared_dim(int(0.2 * attrs['N_e']/timescale)), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='50%', distribution='lognormal(0,[0.87038#14])', normalize=True),  # 450
            4: SORN_init_afferent_synapses(transmitter='GABA', density='20%', distribution='lognormal(0,[0.82099#15])', normalize=True),  # 40

            11: SORN_slow_syn(transmitter='GABA', strength='-[0.1838#16]', so=so),
            14: SORN_fast_syn(transmitter='GLU', strength='[1.5#17]', so=so),#1.5353
            15: SORN_fast_syn(transmitter='GABA', strength='-[0.08#18]', so=False),#0.08
            18: SORN_generate_output(init_TH='0.1;+-0%'),
            19: SORN_buffer_variables(),

            #20: SORN_Refractory_Digital(factor='0.2;0.7', threshold=0.1),
            20: SORN_Refractory_Analog(factor='0.2;0.7'),

            #23: SORN_IP_TI(h_ip='lognormal_real_mean([0.08#6], [0.2944#7])', eta_ip='[0.0003#8];+-50%', integration_length='30;+-50%', clip_min=None),
        })

        i_ng['structure', 0].stretch_to_equal_size(e_ng)

        #SynapseGroup(net=SORN, src=retina, dst=e_ng, tag='GLU,GLU_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)

        SynapseGroup(net=SORN, src=e_ng, dst=e_ng, tag='GLU,GLU_same', connectivity='(s_id!=d_id)*in_box(10)', partition=True)#.partition([10, 10], [partition, partition])
        SynapseGroup(net=SORN, src=e_ng, dst=i_ng, tag='GLU', connectivity='in_box(10)', partition=True)#.partition([5, 5], [2, 2])
        SynapseGroup(net=SORN, src=i_ng, dst=e_ng, tag='GABA', connectivity='in_box(10)', partition=True)
        SynapseGroup(net=SORN, src=i_ng, dst=i_ng, tag='GABA', connectivity='(s_id!=d_id)*in_box(10)', partition=True)



        if layer > 0:
            if attrs.get('ff', True):#forward synapses
                SynapseGroup(net=SORN, src=last_e_ng, dst=e_ng, tag='GLU,GLU_ff', connectivity='in_box(10)', partition=True)#.partition([10, 10], [partition, partition])
                #SynapseGroup(net=SORN, src=last_e_ng, dst=i_ng, tag='GABA', connectivity='in_box(10)', partition=True)#.partition([5, 5], [2, 2])
            if attrs.get('fb', False):#backward synapses
                SynapseGroup(net=SORN, src=e_ng, dst=last_e_ng, tag='GLU,GLU_fb', connectivity='in_box(10)', partition=True)#.partition([10, 10], [partition, partition])
                #SynapseGroup(net=SORN, src=e_ng, dst=last_i_ng, tag='GABA', connectivity='in_box(10)', partition=True)#.partition([5, 5], [2, 2])
        #else:
            # i_ng.add_behaviour(10, SORN_external_input(strength=1.0, pattern_groups=[source]))
        e_ng.add_behaviour(9, SORN_external_input(strength=1.0, pattern_groups=[source]))

        last_e_ng = e_ng
        last_i_ng = i_ng

        if __name__ == '__main__' and attrs.get('UI', False):
            e_ng.color = get_color(0, timescale)
            i_ng.color = get_color(1, timescale)

    SORN.set_marked_variables(attrs['ind'], info=print_info, storage_manager=sm)
    SORN.initialize(info=False)

    ###################################################################################################################

    if __name__ == '__main__' and attrs.get('UI', False):
        my_modules = get_default_UI_modules() + get_my_default_UI_modules()
        Network_UI(SORN, modules=my_modules, label='SORN UI default setup', storage_manager=sm, group_display_count=2, reduced_layout=False).show()


    score = 0
    plastic_steps = attrs.get('plastic', 20000)

    for i in range(1):
        sm = StorageManager(attrs['name']+'[{:03d}]'.format(i+1), random_nr=True, print_msg=print_info)
        sm.save_param_dict(attrs)
        score += train_and_generate_text(SORN, plastic_steps, 5000, 1000, display=print_info, stdp_off=True, same_timestep_without_feedback_loop=True, steps_recovery=1000, storage_manager=sm)#5000, 3000 stdp_off=True

    #score += get_oscillation_score_hierarchical(SORN, 0, 5000)
    #print(predict_text_max_source_act(SORN, plastic_steps, 1000, 2000))
    #t = max_source_act_text(SORN, 2000)
    #print(t)
    return score
def run(
    attrs={
        'name': 'maze',
        'ind': [],
        'N_e': 900,
        'TS': [1],
        'ff': True,
        'fb': True,
        'plastic': 15000
    }):
    so = True

    print_info = attrs.get('print', True)

    if print_info:
        print(attrs)

    sm = StorageManager(attrs, random_nr=True, print_msg=print_info)
    sm.save_param_dict(attrs)

    #source = LongDelayGrammar(tag='grammar_act', output_size=attrs['N_e'], random_blocks=True, mode=['simple'], input_density=0.01)
    maze = Maze(level='default', same_color=False)

    SORN = Network()  #behaviour={maze.get_network_behaviour()}

    SORN.maze = maze

    e_location = NeuronGroup(
        net=SORN,
        tag='PC_{},prediction_source'.format(1),
        size=maze.get_location_neuron_dimension(),
        behaviour={
            2:
            SORN_init_neuron_vars(timescale=1),
            3:
            SORN_init_afferent_synapses(
                transmitter='GLU',
                density='90%',
                distribution='uniform(0.1,1.0)',
                normalize=True
            ),  #20%#lognormal(0,[0.95#1]) #[13#0]% #, partition_compensation=True , partition_compensation=True #lognormal(0,0.95)
            #4: SORN_init_afferent_synapses(transmitter='GABA', density='[30#1]%', distribution='uniform(0.0,1.0)', normalize=True),
            5:
            SORN_init_afferent_synapses(transmitter='GLU_cluster',
                                        density='90%',
                                        distribution='uniform(0.1,1.0)',
                                        normalize=True),
            8:
            maze.get_vision_neuron_behaviour(),
            9:
            maze.get_location_neuron_behaviour(),

            #10.0: SORN_slow_syn
            10.0:
            SORN_slow_syn_simple(transmitter='GLU', strength='1.0',
                                 so=so),  #todo: SORN_slow_syn_simple??????
            10.1:
            SORN_IP_WTA_apply(),
            #10.15: WTA_refrac_apply(strengthfactor='[0.1#0]'),#0.1 #attrs['refrac']
            10.2:
            SORN_generate_output_K_WTA_partitioned(partition_size=7,
                                                   K='[0.02#1]'),
            10.3:
            SORN_slow_syn_simple(transmitter='GLU_cluster', strength='1.0'),
            10.4:
            SORN_generate_output_K_WTA_partitioned(
                partition_size=7, K='[0.02#1]', filter_temporal_output=False),
            10.5:
            SORN_slow_syn_simple(transmitter='GLU_cluster', strength='1.0'),
            10.6:
            SORN_generate_output_K_WTA_partitioned(
                partition_size=7, K='[0.02#1]', filter_temporal_output=False),
            10.7:
            SORN_slow_syn_simple(transmitter='GLU_cluster', strength='1.0'),
            10.8:
            SORN_generate_output_K_WTA_partitioned(
                partition_size=7, K='[0.02#1]', filter_temporal_output=False),

            # 12.1: SORN_WTA_iSTDP(eta_iSTDP='[0.00015#2]'),
            # 12.2: SORN_SN(syn_type='GABA'),
            #13.4: SORN_generate_output_K_WTA_partitioned(K='[0.12#4]'),
            #13.5: SORN_WTA_fast_syn(transmitter='GABA', strength='-[0.5#5]', so=so),#[0.1383#2]SORN_fast_syn
            #14: WTA_refrac(),
            #, filter_temporal_output=True
            15:
            SORN_buffer_variables(random_temporal_output_shift=False),
            18:
            WTA_refrac(decayfactor=0.5),
            20:
            SORN_IP_WTA(h_ip='lognormal_real_mean([0.02#1], [0.2944#2])',
                        eta_ip='[0.007#3]',
                        target_clip_min=None,
                        target_clip_max=None),  #-1.0 #1.0 #0.007
            21.1:
            SORN_STDP(transmitter='GLU',
                      eta_stdp='[0.00015#4]',
                      STDP_F={
                          -1: 0.2,
                          1: -1
                      }),  #, 0: 1 #[0.00015#7] #0.5, 0: 3.0
            21.2:
            SORN_STDP(transmitter='GLU_cluster',
                      eta_stdp='[0.00015#5]',
                      STDP_F={0: 2.0}),  #[0.00015#7]
            22:
            SORN_SN(syn_type='GLU', behaviour_norm_factor=1.0),
            23:
            SORN_SN(syn_type='GLU_cluster',
                    behaviour_norm_factor='[0.3#6]'),  #0.1
        })
    '''
        e_action = NeuronGroup(net=SORN, tag='E_action_{}'.format(timescale), size=maze.get_action_neuron_dimension(), behaviour={
                2: SORN_init_neuron_vars(timescale=timescale),
                3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution='uniform(0.1,0.11)', normalize=True, partition_compensation=True),  # 0.89 lognormal(0,[0.5#0])

                12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                13: SORN_slow_syn(transmitter='GABA', strength='-[0.1698#3]', so=False),
                17: SORN_fast_syn(transmitter='GABA', strength='-[0.1#4]', so=False),  # 0.11045
                18: SORN_generate_output(init_TH='0.01'),
                19: SORN_buffer_variables(),

                20: SORN_Refractory_Analog(factor='0.7;+-50%'),
                21: SORN_STDP(eta_stdp='[0.0015#5]', STDP_F={-1: 1, 1:0}, weight_attr='W_temp'),#bigger #todo!!!!
                #22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=5.0),
                22: SORN_temporal_synapses(syn_type='GLU', behaviour_norm_factor=5.0),

                23: SORN_IP_TI(h_ip='0.04', eta_ip='0.001', integration_length='[15#18];+-50%', clip_min=None),
                #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')

                29: SORN_dopamine(),

                30: maze.get_action_neuron_behaviour()
            })


29: SORN_dopamine(),

30: maze.get_action_neuron_behaviour()
    '''
    '''
        e_reward = NeuronGroup(net=SORN, tag='E_reward_{}'.format(timescale), size=maze.get_reward_neuron_dimension(), behaviour={
                2: SORN_init_neuron_vars(timescale=timescale),
                3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution='lognormal(0,[0.95#0])', normalize=True, partition_compensation=True),

                9: maze.get_reward_neuron_behaviour(),

                12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
                18: SORN_generate_output(init_TH='0.01'),
                19: SORN_buffer_variables(),

                #20: SORN_Refractory(factor='0.5;+-50%'),
                21: SORN_STDP(eta_stdp='[0.00015#5]'),
                22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=0.1),

                #23: SORN_IP_TI(h_ip='0.04', eta_ip='0.001', integration_length='[15#18];+-50%', clip_min=None),
                #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
                #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
                #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')
            })
    '''
    '''
    e_punishment = NeuronGroup(net=SORN, tag='E_punishment_{}'.format(timescale), size=maze.get_punishment_neuron_dimension(), behaviour={
            2: SORN_init_neuron_vars(timescale=timescale),
            3: SORN_init_afferent_synapses(transmitter='GLU', density='full', distribution='lognormal(0,[0.95#0])', normalize=True, partition_compensation=True),

            9: maze.get_punishment_neuron_behaviour(),

            12: SORN_slow_syn(transmitter='GLU', strength='[0.1383#2]', so=so),
            18: SORN_generate_output(init_TH='0.01'),
            19: SORN_buffer_variables(),

            #20: SORN_Refractory(factor='0.5;+-50%'),
            21: SORN_STDP(eta_stdp='[0.00015#5]'), #todo!!!!
            22: SORN_SN(syn_type='GLU', clip_max=None, behaviour_norm_factor=0.1),

            #23: SORN_IP_TI(h_ip='0.04', eta_ip='0.001', integration_length='[15#18];+-50%', clip_min=None),
            #25: SORN_NOX(mp='self.partition_sum(n)', eta_nox='[0.5#9];+-50%'),
            #26: SORN_SC_TI(h_sc='lognormal_real_mean([0.015#10], [0.2944#11])', eta_sc='[0.1#12];+-50%',integration_length='1'),  # 60;+-50% #0.05
            #27: SORN_iSTDP(h_ip='same(SCTI, th)', eta_istdp='[0.0001#13]')
        })
    '''

    SynapseGroup(net=SORN, src=e_location, dst=e_location, tag='GLU,GLU_same')
    SynapseGroup(net=SORN,
                 src=e_location,
                 dst=e_location,
                 tag='GLU_cluster,syn')

    #SynapseGroup(net=SORN, src=e_vision, dst=e_location, tag='GLU', partition=True)
    #SynapseGroup(net=SORN, src=e_location, dst=e_action, tag='GLU', partition=True)

    #SynapseGroup(net=SORN, src=e_location, dst=e_reward, tag='GLU', partition=True)
    #SynapseGroup(net=SORN, src=e_reward, dst=e_location, tag='DOP+', partition=True)
    #SynapseGroup(net=SORN, src=e_reward, dst=e_action, tag='DOP+', partition=True)

    #SynapseGroup(net=SORN, src=e_location, dst=e_punishment, tag='GLU', partition=True)
    #SynapseGroup(net=SORN, src=e_punishment, dst=e_location, tag='DOP-', partition=True)
    #SynapseGroup(net=SORN, src=e_punishment, dst=e_action, tag='DOP-', partition=True)

    if __name__ == '__main__' and attrs.get('UI', False):
        e_location.color = (0, 0, 255, 255)
        #e_action.color = (255, 255, 0, 255)
        #e_vision.color = (0, 255, 255, 255)
        #e_reward.color = (100, 255, 100, 255)
        #e_punishment.color = (255, 100, 100, 255)

    SORN.set_marked_variables(attrs['ind'],
                              info=print_info,
                              storage_manager=sm)
    SORN.initialize(info=False)

    ###################################################################################################################

    if __name__ == '__main__' and attrs.get('UI', False):
        #default_modules.insert(0, maze_tab())
        #Network_UI(SORN, label='SORN UI default setup', storage_manager=sm, group_display_count=4, reduced_layout=True).show()
        Network_UI(
            SORN,
            modules=get_default_UI_modules(neuron_parameters=['activity']) +
            [maze_tab()],
            label='SORN UI K_WTA',
            storage_manager=sm,
            group_display_count=1,
            reduced_layout=False).show()

    return 0
Exemple #12
0
from PymoNNto.Exploration.StorageManager.StorageManager import *

#sm = StorageManager('X_1', 'X')
#sm = StorageManager('RALN_Test_2_0', 'RALN_Test_2')
#sm = StorageManager('MemoryCapacity_2', 'MemoryCapacity')#('RALN_Test_MT_Small_0', 'RALN_Test_MT_Small')

#sm = StorageManager('SORN_One_layer_all_600_0', 'SORN_One_layer_all_600')

#sm = StorageManager('SORN_Two_layer_all_600_0', 'SORN_Two_layer_all_600')
sm = StorageManager('SORN_New_Grammar_test', 'SORN_New_Grammar_test_35')  #11

#plt.plot([sm.load_param('error_{}'.format(i)) for i in range(20)])
#plt.show()

weight_limit = None  #1/3#None#1/3#0.5
n_biggest = 3  #None

network = sm.load_obj('net')

network.NeuronGroups = [network.NeuronGroups[0]]
network.SynapseGroups = [network.SynapseGroups[0]]

source = sm.load_obj('source')

RALN = Reconstruct_Analyze_Label_Network(network)

#max_lag = np.max([ng.iteration_lag for ng in network.NeuronGroups])
#for ng in network.NeuronGroups:
#    ng.color = (0, 0, 1/max_lag*ng.iteration_lag)

groups = [network.NeuronGroups[0]]  #, network.NeuronGroups[1]