Ejemplo n.º 1
0
    def display(self, states_out_train, states_out_test, l_meaning_code_train,
                train_meaning, l_final_mean_test, internal_states_test):
        print " *** Plotting to output file ... *** "
        import oct2011.plotting as plotting
        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/states_out_train",
            array_=states_out_train,
            titles_subset=l_meaning_code_train,
            # legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            legend_=None,
            plot_slice=None,
            title="",
            subtitle="")
        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/states_out_train_sent",
            array_=states_out_train,
            titles_subset=train_meaning,
            # legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            legend_=None,
            plot_slice=None,
            title="",
            subtitle="")
        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/states_out_test",
            array_=states_out_test,
            titles_subset=l_final_mean_test,
            # legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            legend_=None,
            plot_slice=None,
            title="",
            subtitle="")

        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/intern_states_test",
            array_=internal_states_test,
            titles_subset=None,
            plot_slice=None,
            title="",
            subtitle="")

        print " *** ... Plotting to output file done *** "
        print "**********************************************"
Ejemplo n.º 2
0
    def display(self, states_out_train, states_out_test, l_meaning_code_train, train_meaning, l_final_mean_test, internal_states_test):
        print " *** Plotting to output file ... *** "
        import oct2011.plotting as plotting
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train",
                                    array_=states_out_train, titles_subset=l_meaning_code_train,
                                    # legend_=l_m_elt, plot_slice=None, title="", subtitle="")
                                    legend_=None, plot_slice=None, title="", subtitle="")
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train_sent",
                                    array_=states_out_train, titles_subset=train_meaning,
                                    # legend_=l_m_elt, plot_slice=None, title="", subtitle="")
                                    legend_=None, plot_slice=None, title="", subtitle="")
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_test",
                                    array_=states_out_test, titles_subset=l_final_mean_test,
                                    # legend_=l_m_elt, plot_slice=None, title="", subtitle="")
                                    legend_=None, plot_slice=None, title="", subtitle="")   
    
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/intern_states_test", array_=internal_states_test, titles_subset=None, plot_slice=None, title="", subtitle="")

        print " *** ... Plotting to output file done *** "
        print "**********************************************"
Ejemplo n.º 3
0
def main(path_file_in, path_file_out, sentence_to_meaning=True, plot=False, fast=False, keep_internal_states=False, verbose=False):
    import sys,os
    #sys.path.append(os.path.dirname(os.path.abspath(__file__))+"/..")
    #print "path   ", os.path.dirname(os.path.abspath(__file__))+"/.."
    current_directory = os.path.dirname(os.path.abspath(__file__))
    parent_directory = os.path.dirname(current_directory)
    sys.path.append(parent_directory)
    print "sys.path.append(parent_directory)", parent_directory
    import Common_Tools.io_language_coding as CtIolangcod
    
    
    # Definning parameters of stimulus (in a dictionary)
    d = {}
    d['act_time'] = 5#2#1#5#10#2
    d['pause'] = True#False
    d['suppl_pause_at_the_end'] = 1*d['act_time']
    d['initial_pause'] = False#True#False#False
    d['offset'] = True#False#True
    
    # Parameters for reservoir
    N = 500#500#500#1000 #100
    sr = 1#3#3#2#1
    iss = 0.25#0.01#1
    leak = 0.25/float(d['act_time'])#0.75/float(d['act_time'])#0.75/2.#0.5#0.05
    
    ## Random parameters
    seed = 5
    if seed is not None:
        mdp.numx.random.seed(seed)
        np.random.seed(seed)
        
    [train_data_txt, test_data_txt, sent_form_info_test] = common.extract_data_io(path_file=path_file_in, sentence_to_meaning=sentence_to_meaning)
#    print "**************************"
#    print "train data_txt", train_data_txt
#    print "test data_txt", test_data_txt
#    print "sent_form_info_test", sent_form_info_test
    train_corpus, train_meaning  = common.txt2corpus_and_meaning(train_txt=train_data_txt)
    
    if sentence_to_meaning:
        test_corpus = test_data_txt
    else:
        test_meaning = test_data_txt
    
    # making the list of constructions (refering to "construction grammar"), a construction is a sentence without its open class words (Nouns and Verbs)
    (l_construction_train, l_ocw_array_train, construction_words) = common.get_and_remove_ocw_in_corpus(corpus=train_corpus, _OCW='X', l_closed_class=get_closed_class_words())
#    print "**************************"
#    print "l_construction_train", l_construction_train
#    print "l_ocw_array_train", l_ocw_array_train
    if sentence_to_meaning:
        (l_construction_test, l_ocw_array_test, construction_words_test) = common.get_and_remove_ocw_in_corpus(corpus=test_corpus, _OCW='X', l_closed_class=get_closed_class_words())
#        print "l_construction_test", l_construction_test
        if construction_words!=construction_words_test:
            raise Exception, "The construction words are not the same for the train constructions and the test constructions. So the coding of sentences will be different and should provoque a future problem."
    else:
        # check if a special form of sentence is requested (canonical or non-canonical form)
        # i.e. check if there is at least one element that is not None
        print ""
        print "*** Managing sentence form ... ***"
        print "sent_form_info_test:", sent_form_info_test
        # if all sentence information is None (not attributed)
        if all(elt is None for elt in sent_form_info_test):
            # generate default form of sentence
            l_ocw_array_test = generate_l_ocw_array_in_canonical_order(l_meaning=test_meaning)
        # if at least one element is not None
        else:
            # call specific method to deal with the specified order of each meanings in the list
            l_ocw_array_test = generate_l_ocw_array_in_specified_order(l_meaning=test_meaning, l_sent_form = sent_form_info_test)
        print "*** ... sentence form managed ***"
#    print "l_ocw_array_test", l_ocw_array_test
    
    #################################################
    ## Generating all the sentence stimulus (in order to have the same length for each sentence)
    #################################################
    if sentence_to_meaning:
        ## Generate the stimulus input for train and test data
        l_full_const = l_construction_train + l_construction_test
#        slice_train = slice(0,len(l_construction_train))
        slice_test = slice(len(l_construction_train),len(l_construction_train)+len(l_construction_test))
#        print "slice_train", slice_train
        print "slice_test", slice_test
    else:
        l_full_const = l_construction_train
        
    slice_train = slice(0,len(l_construction_train))
    (stim_full_data, l_full_offset) = CtIolangcod.generate_stim_input_nodic(l_data=l_full_const,
#                            act_time=d['act_time'], subset=None, l_input=None,
                            act_time=d['act_time'], subset=None, l_input=construction_words,
                            l_nr_word=None, mult=None, full_time=None,
                            with_offset=d['offset'], pause=d['pause'], initial_pause=d['initial_pause'],
                            suppl_pause_at_the_end=d['suppl_pause_at_the_end'], verbose=False)
    stim_sent_train = stim_full_data[slice_train]
    if sentence_to_meaning:
        stim_sent_test = stim_full_data[slice_test]


    #################################################
    ## Generating all the meaning stimulus 
    #################################################
#    l_m_elt = common.get_meaning_coding()
#    elt_pred=['P','F','O','R']
    l_m_elt = common.get_meaning_coding(max_nr_ocw=8, max_nr_actionrelation=2, elt_pred=elt_pred)
#    print ""
#    print "*** Generating meaning for train set ... ***"
#    (stim_mean_train, l_meaning_code_train) = common.generate_meaning_stim(l_data=train_meaning, l_ocw_array=l_ocw_array_train, full_time=stim_sent_train[0].shape[0], l_m_elt=l_m_elt, verbose=False)
    (stim_mean_train, l_meaning_code_train) = common.generate_meaning_stim(l_data=train_meaning,
           l_ocw_array=l_ocw_array_train, full_time=stim_sent_train[0].shape[0],
           l_m_elt=l_m_elt, l_offset=l_full_offset[slice_train], verbose=False,
           initial_pause=d['initial_pause'], pause=d['pause'], act_time=d['act_time'])
#    print "*** ... meaning generated for train set ***"
#    print "l_m_elt", l_m_elt
#    print "stim_mean_train[0].shape", stim_mean_train[0].shape
#    print "l_meaning_code_train", l_meaning_code_train
#    print ""
    if not sentence_to_meaning:
        print "*** Generating meaning for test set ... ***"
        (stim_mean_test, l_meaning_code_test) = common.generate_meaning_stim(l_data=test_meaning, l_ocw_array=l_ocw_array_test, full_time=stim_sent_train[0].shape[0], l_m_elt=l_m_elt, verbose=False)
        print "*** ... meaning generated for test set ***"
        print ""
        
    ## Defining reservoir, readout and flow
    reservoir = Oger.nodes.LeakyReservoirNode(output_dim = N, spectral_radius = sr, input_scaling =iss, nonlin_func = np.tanh, leak_rate = leak)
    read_out = mdp.nodes.LinearRegressionNode(use_pinv=True, with_bias=True)
    flow = mdp.Flow([reservoir, read_out])
    if keep_internal_states:
        Oger.utils.make_inspectable(mdp.Flow)
        
    ## Trainning and testing
    print "Train and test"
    if not fast:
        ## test set = train set
        if sentence_to_meaning:
            (states_out_train, internal_states_train, internal_outputs_train, neuron_states_train) = \
                common._teach_and_test_flow(inputs_train_set=stim_sent_train, teacher_outputs_train_set=stim_mean_train, inputs_test_set=stim_sent_train, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
        else:
            (states_out_train, internal_states_train, internal_outputs_train, neuron_states_train) = \
                common._teach_and_test_flow(inputs_train_set=stim_mean_train, teacher_outputs_train_set=stim_sent_train, inputs_test_set=stim_mean_train, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
    else:
        raise Exception, "have to define what to do for fast mode"
    ## test set not train set
    if sentence_to_meaning:
        (states_out_test, internal_states_test, internal_outputs_test, neuron_states_test) = \
            common._test_flow(inputs_test_set=stim_sent_test, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
    else:
        (states_out_test, internal_states_test, internal_outputs_test, neuron_states_test) = \
            common._test_flow(inputs_test_set=stim_mean_test, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)

    if verbose:
        for i in range(len(stim_mean_train)):
            print "len(stim_mean_train)", len(stim_mean_train)
            print "len(l_meaning_code_train)", len(l_meaning_code_train)
            print l_meaning_code_train[i]
            print (stim_mean_train[0]==stim_mean_train[i])
            print (l_meaning_code_train[0]==l_meaning_code_train[i])
    
    ## Writting output meaning
    if sentence_to_meaning:
        l_recovered_meaning_test = convert_l_output_activity_in_meaning(l_out_act=states_out_test, l_ocw_array=l_ocw_array_test, l_m_elt=l_m_elt)
        if verbose:
            print "l_recovered_meaning_test", l_recovered_meaning_test
        l_final_mean_test = []
        for meanings in l_recovered_meaning_test:
            current_meanings = ""
            if verbose:
                print "meanings", meanings
            for i_m in range(len(meanings)):
#                if verbose:
#                    print " i_m:",i_m
#                    print " meanings[i_m]:",meanings[i_m]
                if i_m>0:
                    current_meanings+=','
                current_meanings+=" ".join(meanings[i_m])
            l_final_mean_test.append(current_meanings)
        print ""
        print "**********************************************"
        print " *** RECOGNIZED MEANINGS *** "
        for elt in l_final_mean_test:
            print str(elt)
        print "**********************************************"
        
    
    ## Writting output sentence
    if not sentence_to_meaning:
        if not fast and (verbose or plot):
            print ""
            print "**********************************************"
            print "*** Processing recovery of train sentences ..."
            l_recovered_construction_train = convert_l_output_activity_in_construction(l_out_act=states_out_train,
                                                                                       construction_words=construction_words,
                                                                                       min_nr_of_val_upper_thres=1)
            l_recovered_sentences_train = attribute_ocw_to_constructions(l_constructions=l_recovered_construction_train,
                                                                         l_ocw_array=l_ocw_array_train, _OCW='X')
            print "*** l_recovered_sentences_train: ***"
            for s in l_recovered_sentences_train:
                print s
            print "**********************************************"
        if verbose:
            print ""
            print "**********************************************"
            print "*** Processing recovery of test sentences ..."
        l_recovered_construction_test = convert_l_output_activity_in_construction(l_out_act=states_out_test,
                                                                                  construction_words=construction_words,
                                                                                  min_nr_of_val_upper_thres=2)
        l_recovered_sentences_test = attribute_ocw_to_constructions(l_constructions=l_recovered_construction_test,
                                                                    l_ocw_array=l_ocw_array_test, _OCW='X')
        if verbose:
            print "*** l_recovered_sentences_test: ***"
            for s in l_recovered_sentences_test:
                print s
            print "**********************************************"
    
    ## Writting sentences to output file
    print " *** Writting to output file ... *** "
    #ecrire une seule ligne simple dans un fichier la phrase attendue en mode test
    common.write_list_in_file(l=l_final_mean_test, file_path=path_file_out)   
    
    print " *** ... Writting done ***"
    print "**********************************************"

    ## Plot
    if plot:
        print " *** Plotting to output file ... *** "
        import oct2011.plotting as plotting
        if sentence_to_meaning:
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train",
                                        array_=states_out_train, titles_subset=l_meaning_code_train,
#                                        legend_=l_m_elt, plot_slice=None, title="", subtitle="")
                                        legend_=None, plot_slice=None, title="", subtitle="")
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train_sent",
                                        array_=states_out_train, titles_subset=train_meaning,
#                                        legend_=l_m_elt, plot_slice=None, title="", subtitle="")
                                        legend_=None, plot_slice=None, title="", subtitle="")
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_test",
                                        array_=states_out_test, titles_subset=l_final_mean_test,
#                                        legend_=l_m_elt, plot_slice=None, title="", subtitle="")
                                        legend_=None, plot_slice=None, title="", subtitle="")
        else:
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train_recov", array_=states_out_train, titles_subset=l_recovered_sentences_train, legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_test", array_=states_out_test, titles_subset=l_recovered_sentences_test, legend_=construction_words, plot_slice=None, title="", subtitle="")
    
    
        ## Plot internal states
        if sentence_to_meaning:
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/intern_states_test", array_=internal_states_test, titles_subset=None, plot_slice=None, title="", subtitle="")
        else:
            plotting.plot_array_in_file(root_file_name="../../RES_TEMP/intern_states_test", array_=internal_states_test, titles_subset=l_ocw_array_test, plot_slice=None, title="", subtitle="")
        print " *** ... Plotting to output file done *** "
        print "**********************************************"
        return l_final_mean_test 
Ejemplo n.º 4
0
def main(path_file_in, path_file_out, plot=False, keep_internal_states=False, verbose=False):
    def write_list_in_file(l, file=None, file_path=None):
        """
        Write a list in a file with with one item per line (like a one column csv).
        
        If file is given, then it assumes the file is already open for writing.
        If file_path is given, then it opens the file for writing, write the list, and then close the file.
        """
        if file_path is not None:
            if file is not None:
                raise Exception, "Too much arguments. You must choose between file and file_path."
            else:
                file = open(file_path, 'wb')
        if file is None:
            raise Exception, "No file given in input."
        
        for item in l:
            file.write("%s\n" % item)
            
        if file_path is not None:
            file.close()
    
#    import Common_Tools.io_language_coding as CtIolangcod
    import sys
    sys.path.append("../Common_Tools")
    print sys.path
    import io_language_coding as CtIolangcod
    
    sys.path.append("../iCub_language")
    
    sentence_to_meaning = False
    
    # Definning parameters of stimulus (in a dictionary)
    d = {}
    d['act_time'] = 5#10#2
    d['pause'] = True#False
    d['suppl_pause_at_the_end'] = 1*d['act_time']
    d['initial_pause'] = True#False#False
    d['offset'] = False#True
    
    # Parameters for reservoir
    N = 500#1000 #100
    sr = 2#3#3#2#1
    iss = 0.01#1
    leak = 0.75#0.5#0.05
    
    ## output dic
    #    d['start_teacher'] = 1#'end'
    
    
    ## Random parameters
    seed = 5#2#4#2
    # seed 2 works with 2 sentences : both with 1 relation, 1 Canonical, 1 Non-canonical
    if seed is not None:
        mdp.numx.random.seed(seed)
        np.random.seed(seed)
#    if verbose:
#        print "Spectra radius of generated matrix before applying another spectral radius: "+str(Oger.utils.get_spectral_radius(w))
#    if spectral_radius is not None:
#        w *= d['spectral_radius'] / Oger.utils.get_spectral_radius(w)
#        if verbose:
#            print "Spectra radius matrix after applying another spectral radius: "+str(Oger.utils.get_spectral_radius(w))
#    if randomize_seed_afterwards:
#        """ redifine randomly the seed in order to not fix the seed also for other methods that are using numpy.random methods.
#        """
#        import time
#        mdp.numx.random.seed(int(time.time()*10**6))
    
#    [train_data_txt, test_data_txt] = extract_data_io(path_file=path_file_in, sentence_to_meaning=sentence_to_meaning)
    [train_data_txt, test_data_txt, sent_form_info_test] = extract_data_io(path_file=path_file_in, sentence_to_meaning=sentence_to_meaning)
    print "**************************"
    print "train data_txt", train_data_txt
    print "test data_txt", test_data_txt
    print "sent_form_info_test", sent_form_info_test
    train_corpus, train_meaning  = txt2corpus_and_meaning(train_txt=train_data_txt)
    if sentence_to_meaning:
        test_corpus = test_data_txt
    else:
        test_meaning = test_data_txt
    # making the list of constructions (refering to "construction grammar"), a construction is a sentence without its open class words (Nouns and Verbs)
    (l_construction_train, l_ocw_array_train, construction_words) = get_and_remove_ocw_in_corpus(corpus=train_corpus, _OCW='X')
    print "**************************"
    print "l_construction_train", l_construction_train
    print "l_ocw_array_train", l_ocw_array_train
    if sentence_to_meaning:
        (l_construction_test, l_ocw_array_test, construction_words_test) = get_and_remove_ocw_in_corpus(corpus=test_corpus, _OCW='X')
        print "l_construction_test", l_construction_test
        if construction_words!=construction_words_test:
            raise Exception, "The construction words are not the same for the train constructions and the test constructions. So the coding of sentences will be different and should provoque a future problem."
    else:
        # check if a special form of sentence is requested (canonical or non-canonical form)
        # i.e. check if there is at least one element that is not None
        print ""
        print "*** Managing sentence form ... ***"
        print "sent_form_info_test:", sent_form_info_test
        # if all sentence information is None (not attributed)
        if all(elt is None for elt in sent_form_info_test):
            # generate default form of sentence
            l_ocw_array_test = generate_l_ocw_array_in_canonical_order(l_meaning=test_meaning)
        # if at least one element is not None
        else:
            # call specific method to deal with the specified order of each meanings in the list
            l_ocw_array_test = generate_l_ocw_array_in_specified_order(l_meaning=test_meaning, l_sent_form = sent_form_info_test)
        print "*** ... sentence form managed ***"
    print "l_ocw_array_test", l_ocw_array_test
    
    ## Generating all the sentence stimulus (in order to have the same length for each sentence)
    if sentence_to_meaning:
        ## Generate the stimulus input for train and test data
        l_full_const = l_construction_train + l_construction_test
#        slice_train = slice(0,len(l_construction_train))
        slice_test = slice(len(l_construction_train),len(l_construction_train)+len(l_construction_test))
#        print "slice_train", slice_train
        print "slice_test", slice_test
    else:
        l_full_const = l_construction_train
    slice_train = slice(0,len(l_construction_train))
    (stim_full_data, l_full_offset) = CtIolangcod.generate_stim_input_nodic(l_data=l_full_const,
#                            act_time=d['act_time'], subset=None, l_input=None,
                            act_time=d['act_time'], subset=None, l_input=construction_words,
                            l_nr_word=None, mult=None, full_time=None,
                            with_offset=d['offset'], pause=d['pause'], initial_pause=d['initial_pause'],
                            suppl_pause_at_the_end=d['suppl_pause_at_the_end'], verbose=False)
    stim_sent_train = stim_full_data[slice_train]
    if sentence_to_meaning:
        stim_sent_test = stim_full_data[slice_test]
    
    print "stim_sent_train[0].shape", stim_sent_train[0].shape
    print "stim_sent_train[0].shape[0]", stim_sent_train[0].shape[0]
    
    l_m_elt = get_meaning_coding()
    print ""
    print "*** Generating meaning for train set ... ***"
    (stim_mean_train, l_meaning_code_train) = generate_meaning_stim(l_data=train_meaning, l_ocw_array=l_ocw_array_train, full_time=stim_sent_train[0].shape[0], l_m_elt=l_m_elt, verbose=False)
    print "*** ... meaning generated for train set ***"
    print "l_m_elt", l_m_elt
    print "stim_mean_train[0].shape", stim_mean_train[0].shape
    print "l_meaning_code_train", l_meaning_code_train
    print ""
    if not sentence_to_meaning:
        print "*** Generating meaning for test set ... ***"
        (stim_mean_test, l_meaning_code_test) = generate_meaning_stim(l_data=test_meaning, l_ocw_array=l_ocw_array_test, full_time=stim_sent_train[0].shape[0], l_m_elt=l_m_elt, verbose=False)
        print "*** ... meaning generated for test set ***"
        print ""
    
    reservoir = Oger.nodes.LeakyReservoirNode(output_dim = N, spectral_radius = sr, input_scaling =iss, nonlin_func = np.tanh, leak_rate = leak)
    read_out = mdp.nodes.LinearRegressionNode(use_pinv=True, with_bias=True)
    flow = mdp.Flow([reservoir, read_out])
    if keep_internal_states:
        Oger.utils.make_inspectable(mdp.Flow)
    
    print "Train and test"
#    (states_out_test, internal_states_test, internal_outputs_test, neuron_states_test) = \
    ## test set = train set
    (states_out_train, internal_states_train, internal_outputs_train, neuron_states_train) = \
        _teach_and_test_flow(inputs_train_set=stim_mean_train, teacher_outputs_train_set=stim_sent_train, inputs_test_set=stim_mean_train, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
    ## test set not train set
    (states_out_test, internal_states_test, internal_outputs_test, neuron_states_test) = \
        _test_flow(inputs_test_set=stim_mean_test, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
#    (states_out_test, internal_states_test, internal_outputs_test, neuron_states_test) = \
#        _teach_and_test_flow(inputs_train_set=stim_mean_train, teacher_outputs_train_set=stim_sent_train, inputs_test_set=stim_mean_test, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
    
    
    for i in range(len(stim_mean_train)):
        print "len(stim_mean_train)", len(stim_mean_train)
        print "len(l_meaning_code_train)", len(l_meaning_code_train)
        print l_meaning_code_train[i]
        print (stim_mean_train[0]==stim_mean_train[i])
        print (l_meaning_code_train[0]==l_meaning_code_train[i])
    
    # Ecriture de la phrase de réponse
    print ""
    print "**********************************************"
    print "*** Processing recovery of train sentences ..."
    l_recovered_construction_train = convert_l_output_activity_in_construction(l_out_act=states_out_train,
                                                                               construction_words=construction_words,
                                                                               min_nr_of_val_upper_thres=1)
    l_recovered_sentences_train = attribute_ocw_to_constructions(l_constructions=l_recovered_construction_train,
                                                                 l_ocw_array=l_ocw_array_train, _OCW='X')
    print "*** l_recovered_sentences_train: ***"
    for s in l_recovered_sentences_train:
        print s
    print "**********************************************"
    print ""
    print "**********************************************"
    print "*** Processing recovery of test sentences ..."
    l_recovered_construction_test = convert_l_output_activity_in_construction(l_out_act=states_out_test,
                                                                              construction_words=construction_words,
                                                                              min_nr_of_val_upper_thres=2)
    l_recovered_sentences_test = attribute_ocw_to_constructions(l_constructions=l_recovered_construction_test,
                                                                l_ocw_array=l_ocw_array_test, _OCW='X')
    print "*** l_recovered_sentences_test: ***"
    for s in l_recovered_sentences_test:
        print s
    print "**********************************************"
    
    
    ## Writting sentences to output file
    print " *** Writting to output file ... *** "
    l_final_sent_test = []
    for list_words in l_recovered_sentences_test:
        l_final_sent_test.append(" ".join(list_words))
    #ecrire une seule ligne simple dans un fichier la phrase attendue en mode test
    write_list_in_file(l=l_final_sent_test, file_path=path_file_out)
    print " *** ... Writting done ***"
    print "**********************************************"
    
    
    ## Plot inputs
    if plot:
        print " *** Plotting to output file ... *** "
        import oct2011.plotting as plotting
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_sent_train", array_=stim_sent_train, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_sent_test", array_=stim_sent_test, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_mean_train0", array_=stim_mean_train[0].T, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_mean_train1", array_=stim_mean_train[1].T, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_mean_train_T", array_=stim_mean_train[0].T, plot_slice=None, title="", subtitle="")
    
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train", array_=states_out_train, titles_subset=l_construction_train, legend_=construction_words, plot_slice=None, title="", subtitle="")
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train_recov", array_=states_out_train, titles_subset=l_recovered_sentences_train, legend_=construction_words, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_train_detail", array_=states_out_train[0].T, titles_subset=l_construction_train[0], plot_slice=None, title="", subtitle="")
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/states_out_test", array_=states_out_test, titles_subset=l_recovered_sentences_test, legend_=construction_words, plot_slice=None, title="", subtitle="")
    
    
        ## Plot internal states
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/intern_states_train", array_=internal_states_train, titles_subset=l_construction_train, plot_slice=None, title="", subtitle="")
        plotting.plot_array_in_file(root_file_name="../../RES_TEMP/intern_states_test", array_=internal_states_test, titles_subset=l_ocw_array_test, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_int_states", array_=out, plot_slice=None, title="", subtitle="")
    #    plotting.plot_array_in_file(root_file_name="../../RES_TEMP/test_int_states_T", array_=out.T, plot_slice=None, title="", subtitle="")
        print " *** ... Plotting to output file done *** "
        print "**********************************************"
Ejemplo n.º 5
0
def main(path_file_in,
         path_file_out,
         plot=False,
         fast=False,
         keep_internal_states=False,
         verbose=False):
    import os
    #sys.path.append(os.path.dirname(os.path.abspath(__file__))+"/..")
    #print "path   ", os.path.dirname(os.path.abspath(__file__))+"/.."
    current_directory = os.path.dirname(os.path.abspath(__file__))
    parent_directory = os.path.dirname(current_directory)
    sys.path.append(parent_directory)
    import io_language_coding as CtIolangcod

    # Definning parameters of stimulus (in a dictionary)
    d = {}
    d['act_time'] = 5  #2#1#5#10#2
    d['pause'] = True  #False
    d['suppl_pause_at_the_end'] = 1 * d['act_time']
    d['initial_pause'] = False  #True#False#False
    d['offset'] = True  #False#True

    # Parameters for reservoir
    N = 500  #500#500#1000 #100
    sr = 1  #3#3#2#1
    iss = 0.25  #0.01#1
    leak = 0.25 / float(
        d['act_time'])  #0.75/float(d['act_time'])#0.75/2.#0.5#0.05

    ## Random parameters
    seed = 5
    if seed is not None:
        mdp.numx.random.seed(seed)
        np.random.seed(seed)

    [train_data_txt, test_data_txt,
     sent_form_info_test] = common.extract_data_io(path_file=path_file_in)
    train_corpus, train_meaning = common.txt2corpus_and_meaning(
        train_txt=train_data_txt)

    test_corpus = test_data_txt

    # making the list of constructions (refering to "construction grammar"), a construction is a sentence without its open class words (Nouns and Verbs)
    (l_construction_train, l_ocw_array_train,
     construction_words) = common.get_and_remove_ocw_in_corpus(
         corpus=train_corpus,
         _OCW='X',
         l_closed_class=get_closed_class_words())

    (l_construction_test, l_ocw_array_test,
     construction_words_test) = common.get_and_remove_ocw_in_corpus(
         corpus=test_corpus, _OCW='X', l_closed_class=get_closed_class_words())
    if construction_words != construction_words_test:
        raise Exception, "The construction words are not the same for the train constructions and the test constructions. So the coding of sentences will be different and should provoque a future problem."

    #################################################
    ## Generating all the sentence stimulus (in order to have the same length for each sentence)

    l_full_const = l_construction_train + l_construction_test
    slice_test = slice(len(l_construction_train),
                       len(l_construction_train) + len(l_construction_test))

    slice_train = slice(0, len(l_construction_train))
    (stim_full_data, l_full_offset) = CtIolangcod.generate_stim_input_nodic(
        l_data=l_full_const,
        #                            act_time=d['act_time'], subset=None, l_input=None,
        act_time=d['act_time'],
        subset=None,
        l_input=construction_words,
        l_nr_word=None,
        mult=None,
        full_time=None,
        with_offset=d['offset'],
        pause=d['pause'],
        initial_pause=d['initial_pause'],
        suppl_pause_at_the_end=d['suppl_pause_at_the_end'],
        verbose=False)
    stim_sent_train = stim_full_data[slice_train]

    stim_sent_test = stim_full_data[slice_test]

    #################################################
    ## Generating all the meaning stimulus
    #################################################

    l_m_elt = common.get_meaning_coding(
        max_nr_ocw=max_nr_ocw,
        max_nr_actionrelation=max_nr_actionrelation,
        elt_pred=elt_pred)

    (stim_mean_train, l_meaning_code_train) = common.generate_meaning_stim(
        l_data=train_meaning,
        l_ocw_array=l_ocw_array_train,
        full_time=stim_sent_train[0].shape[0],
        l_m_elt=l_m_elt,
        l_offset=l_full_offset[slice_train],
        verbose=False,
        initial_pause=d['initial_pause'],
        pause=d['pause'],
        act_time=d['act_time'])

    ## Defining reservoir, readout and flow
    reservoir = Oger.nodes.LeakyReservoirNode(output_dim=N,
                                              spectral_radius=sr,
                                              input_scaling=iss,
                                              nonlin_func=np.tanh,
                                              leak_rate=leak)
    read_out = mdp.nodes.LinearRegressionNode(use_pinv=True, with_bias=True)
    flow = mdp.Flow([reservoir, read_out])
    if keep_internal_states:
        Oger.utils.make_inspectable(mdp.Flow)

    ## Trainning and testing
    print "Train and test"
    if not fast:
        (states_out_train, internal_states_train, internal_outputs_train, neuron_states_train) = \
            common._teach_and_test_flow(inputs_train_set=stim_sent_train, teacher_outputs_train_set=stim_mean_train, inputs_test_set=stim_sent_train, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)
    else:
        raise Exception, "have to define what to do for fast mode"
    ## test set not train set

    (states_out_test, internal_states_test, internal_outputs_test, neuron_states_test) = \
        common._test_flow(inputs_test_set=stim_sent_test, _flow=flow, _reservoir=reservoir, keep_internal_states=keep_internal_states)

    if verbose:
        for i in range(len(stim_mean_train)):
            print "len(stim_mean_train)", len(stim_mean_train)
            print "len(l_meaning_code_train)", len(l_meaning_code_train)
            print l_meaning_code_train[i]
            print(stim_mean_train[0] == stim_mean_train[i])
            print(l_meaning_code_train[0] == l_meaning_code_train[i])

    ## Writting output meaning

    l_recovered_meaning_test = convert_l_output_activity_in_meaning(
        l_out_act=states_out_test,
        l_ocw_array=l_ocw_array_test,
        l_m_elt=l_m_elt)

    if verbose:
        print "l_recovered_meaning_test", l_recovered_meaning_test
    l_final_mean_test = []
    for meanings in l_recovered_meaning_test:
        current_meanings = ""
        if verbose:
            print "meanings", meanings
        for i_m in range(len(meanings)):
            # if verbose:
            # print " i_m:",i_m
            #  print " meanings[i_m]:",meanings[i_m]
            if i_m > 0:
                current_meanings += ','
            current_meanings += " ".join(meanings[i_m])
        l_final_mean_test.append(current_meanings)
    print ""
    print "**********************************************"
    print " *** RECOGNIZED MEANINGS *** "
    for elt in l_final_mean_test:
        print str(elt)
    print "**********************************************"

    ## Writting sentences to output file
    print " *** Writting to output file ... *** "
    #ecrire une seule ligne simple dans un fichier la phrase attendue en mode test
    common.write_list_in_file(l=l_final_mean_test, file_path=path_file_out)

    print " *** ... Writting done ***"
    print "**********************************************"

    ## Plot
    if plot:
        print " *** Plotting to output file ... *** "
        import oct2011.plotting as plotting
        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/states_out_train",
            array_=states_out_train,
            titles_subset=l_meaning_code_train,
            #                                        legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            legend_=None,
            plot_slice=None,
            title="",
            subtitle="")
        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/states_out_train_sent",
            array_=states_out_train,
            titles_subset=train_meaning,
            #                                        legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            legend_=None,
            plot_slice=None,
            title="",
            subtitle="")
        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/states_out_test",
            array_=states_out_test,
            titles_subset=l_final_mean_test,
            #                                        legend_=l_m_elt, plot_slice=None, title="", subtitle="")
            legend_=None,
            plot_slice=None,
            title="",
            subtitle="")

        plotting.plot_array_in_file(
            root_file_name="../../RES_TEMP/intern_states_test",
            array_=internal_states_test,
            titles_subset=None,
            plot_slice=None,
            title="",
            subtitle="")

        print " *** ... Plotting to output file done *** "
        print "**********************************************"
        return l_final_mean_test