Пример #1
0
def general_SVC_exclude_test_set(spec_svm_file=None):
    ''' Train the general SVM according to regimes on all data excluding the test data.
        Compute power at all (s,t)'s.
    '''
    global st2pow
    pfile_text = []
    
    ########## read (s,t)-specific SVMs from file ##########
    if(spec_svm_file != None):
        # read specific SVM dict from pickled file 
        sys.stdout.write("Reading in cPickled SVM-dict from: %s... " % spec_svm_file)
        with open("../" + spec_svm_file, mode='rb') as svm_fh:
            svm_dict = pickle.load(svm_fh)
        print "loaded.\n"
    
    ############## train & test on all (s,t) ###############
    for s,t in [(s,t) for s in p.selection for t in p.times]:        
        
        # training data, EXCLUDING test data
        print "training (excluded) on (%g,%i)" % (s,t)
        train_mix = data_mix_for_SVM_general_exclude(s,t) 
        
        svm_weights, svm_e_terms = {}, {}
        for i in range(len(train_mix)/2): svm_weights[i], svm_e_terms[i] = 1.0, 0.1
        
        meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)
        
        # test
        test_data, test_states = datas[s,t], states[s,t] # test data
        st2pow[s,t], svm2frac, frac_s_str = learn.metaSVM_pow(test_data, test_states, meta_svm, s, t, svm_dict)
        
        # report
        fracs = ""
        for i, frac in sorted(svm2frac.iteritems()): fracs += "%g\t" % frac
        if(frac_s_str == None):
            to_write = "%g\t%i\t%g\t%s" % (s, t, st2pow[s,t], fracs)
        else:
             to_write = "%g\t%i\t%g\t%s\t%s" % (s, t, st2pow[s,t], fracs, frac_s_str) 
        print to_write
        pfile_text.append(to_write)
        
    write_to_file('power_SVM_general.txt', pfile_text)
    
    ########### make final meta-SVM and pickle ###########
    sys.stdout.write('Training final meta-SVM... ')
    train_mix = data_mix_for_SVM_general(p.first_sim, p.last_sim)
    
    # even svm-weights & error terms
    svm_weights, svm_e_terms = {}, {}
    for i in range(len(train_mix)/2):
        svm_weights[i], svm_e_terms[i] = 1.0, 0.1
    
    meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)        
    print 'done.\n'
    
    with open(gen_svm_file_to_write, mode='wb') as gen_svm_fh:
            pickle.dump(meta_svm, gen_svm_fh)
Пример #2
0
def general_SVC_partition_data(gen_svm_file=None, spec_svm_file=None):
    ''' Train general SVM according to regimes on partition 1, test on partition 2.
        Compute power at all (s,t)'s.
    '''
    global st2pow

    partition_at = 300  # 0 test all data, 300 train & test, 500 train all data
    pfile_text = []
    svm_dict = None

    if (gen_svm_file is None):
        ############ train meta-SVM on training-set #############
        sys.stdout.write('Training meta-SVM... ')

        # get training data
        train_mix = data_mix_for_SVM_general(p.first_sim, partition_at)

        # set equal regime-svm weights and error terms
        regimes_ind = [i for i in range(len(train_mix) / 2)]
        svm_weights = dict(zip(regimes_ind, [1.0] * len(regimes_ind)))
        svm_e_terms = dict(zip(regimes_ind, [0.1] * len(regimes_ind)))

        # train
        meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)
        print 'done.\n'

        # if this was only for training, pickle and exit
        if (partition_at == p.last_sim):
            print 'Pickling SVM, then exiting...'
            with open(gen_svm_file_to_write, mode='wb') as gen_svm_fh:
                pickle.dump(meta_svm, gen_svm_fh)
            sys.exit(0)
    else:
        ############# read SVM from pickled file ##############
        sys.stdout.write("\nReading in cPickled SVM from: %s... " %
                         gen_svm_file)
        with open("../" + gen_svm_file, mode='rb') as svm_fh:
            meta_svm = pickle.load(svm_fh)
        print "loaded.\n"

    if (spec_svm_file != None):
        # read (s,t)-specific SVM dict from pickled file
        sys.stdout.write("\nReading in cPickled SVM-dict from: %s... " %
                         spec_svm_file)
        with open("../" + spec_svm_file, mode='rb') as svm_fh:
            svm_dict = pickle.load(svm_fh)
        print "loaded.\n"

    #dotprod_and_decison_func_of(meta_svm, 0.02,700 , regimes.regime_of(0.02,700) )
    #dotprod_and_decison_func_of(meta_svm, 0.02,1400, regimes.regime_of(0.02,1400))

    ################# apply SVM to test data ##################
    for s, t in [(s, t) for s in p.selection for t in p.times]:

        # evaluate power on test set
        test_data, test_states = data_and_states_for(p.case_type, p.cont_type,
                                                     [s], [t], partition_at,
                                                     p.last_sim)  # test data
        print "test-data: %i, test-states: %i" % (len(test_data),
                                                  len(test_states))
        st2pow[s, t], svm2frac, frac_s_str = learn.metaSVM_pow(
            test_data, test_states, meta_svm, s, t, svm_dict)

        # report
        fracs = ""
        for i, frac in sorted(svm2frac.iteritems()):
            fracs += "%g\t" % frac
        if (frac_s_str == None):
            to_write = "%g\t%i\t%g\t%s" % (s, t, st2pow[s, t], fracs)
        else:
            to_write = "%g\t%i\t%g\t%s\t%s" % (s, t, st2pow[s, t], fracs,
                                               frac_s_str)

        print to_write
        pfile_text.append(to_write)

    # write power stats for file
    write_to_file('power_SVM_general.txt', pfile_text)

    ######## if none input, make & pickle the final meta-SVM ########
    if (gen_svm_file is None):
        # train meta-SVM on training-set
        sys.stdout.write('Training final meta-SVM... ')
        train_mix = data_mix_for_SVM_general(p.first_sim, p.last_sim)

        # set equal regime-svm weights and error terms
        regimes_ind = [i for i in range(len(train_mix) / 2)]
        svm_weights = dict(zip(regimes_ind, [1.0] * len(regimes_ind)))
        svm_e_terms = dict(zip(regimes_ind, [0.1] * len(regimes_ind)))

        meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)
        print 'done.\n'

        with open(gen_svm_file_to_write, mode='wb') as gen_svm_fh:
            pickle.dump(meta_svm, gen_svm_fh)
Пример #3
0
def general_SVC_exclude_test_set(spec_svm_file=None):
    ''' Train the general SVM according to regimes on all data excluding the test data.
        Compute power at all (s,t)'s.
    '''
    global st2pow
    pfile_text = []

    ########## read (s,t)-specific SVMs from file ##########
    if (spec_svm_file != None):
        # read specific SVM dict from pickled file
        sys.stdout.write("Reading in cPickled SVM-dict from: %s... " %
                         spec_svm_file)
        with open("../" + spec_svm_file, mode='rb') as svm_fh:
            svm_dict = pickle.load(svm_fh)
        print "loaded.\n"

    ############## train & test on all (s,t) ###############
    for s, t in [(s, t) for s in p.selection for t in p.times]:

        # training data, EXCLUDING test data
        print "training (excluded) on (%g,%i)" % (s, t)
        train_mix = data_mix_for_SVM_general_exclude(s, t)

        svm_weights, svm_e_terms = {}, {}
        for i in range(len(train_mix) / 2):
            svm_weights[i], svm_e_terms[i] = 1.0, 0.1

        meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)

        # test
        test_data, test_states = datas[s, t], states[s, t]  # test data
        st2pow[s, t], svm2frac, frac_s_str = learn.metaSVM_pow(
            test_data, test_states, meta_svm, s, t, svm_dict)

        # report
        fracs = ""
        for i, frac in sorted(svm2frac.iteritems()):
            fracs += "%g\t" % frac
        if (frac_s_str == None):
            to_write = "%g\t%i\t%g\t%s" % (s, t, st2pow[s, t], fracs)
        else:
            to_write = "%g\t%i\t%g\t%s\t%s" % (s, t, st2pow[s, t], fracs,
                                               frac_s_str)
        print to_write
        pfile_text.append(to_write)

    write_to_file('power_SVM_general.txt', pfile_text)

    ########### make final meta-SVM and pickle ###########
    sys.stdout.write('Training final meta-SVM... ')
    train_mix = data_mix_for_SVM_general(p.first_sim, p.last_sim)

    # even svm-weights & error terms
    svm_weights, svm_e_terms = {}, {}
    for i in range(len(train_mix) / 2):
        svm_weights[i], svm_e_terms[i] = 1.0, 0.1

    meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)
    print 'done.\n'

    with open(gen_svm_file_to_write, mode='wb') as gen_svm_fh:
        pickle.dump(meta_svm, gen_svm_fh)
Пример #4
0
def general_SVC_partition_data(gen_svm_file=None, spec_svm_file=None):
    ''' Train general SVM according to regimes on partition 1, test on partition 2.
        Compute power at all (s,t)'s.
    '''
    global st2pow
    
    partition_at = 300 # 0 test all data, 300 train & test, 500 train all data
    pfile_text = []
    svm_dict = None
    
    if(gen_svm_file is None):
        ############ train meta-SVM on training-set #############
        sys.stdout.write('Training meta-SVM... ')
        
        # get training data
        train_mix = data_mix_for_SVM_general(p.first_sim, partition_at)
        
        # set equal regime-svm weights and error terms
        regimes_ind = [i for i in range(len(train_mix)/2)]
        svm_weights = dict( zip(regimes_ind, [1.0]*len(regimes_ind)) )
        svm_e_terms = dict( zip(regimes_ind, [0.1]*len(regimes_ind)) )
        
        # train
        meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)        
        print 'done.\n'
        
        # if this was only for training, pickle and exit
        if(partition_at == p.last_sim):
            print 'Pickling SVM, then exiting...'
            with open(gen_svm_file_to_write, mode='wb') as gen_svm_fh:
                    pickle.dump(meta_svm, gen_svm_fh)
            sys.exit(0)
    else:
        ############# read SVM from pickled file ############## 
        sys.stdout.write("\nReading in cPickled SVM from: %s... " % gen_svm_file)
        with open("../" + gen_svm_file, mode='rb') as svm_fh:
            meta_svm = pickle.load(svm_fh)
        print "loaded.\n"
    
    if(spec_svm_file != None):
        # read (s,t)-specific SVM dict from pickled file 
        sys.stdout.write("\nReading in cPickled SVM-dict from: %s... " % spec_svm_file)
        with open("../" + spec_svm_file, mode='rb') as svm_fh:
            svm_dict = pickle.load(svm_fh)
        print "loaded.\n"
    
    #dotprod_and_decison_func_of(meta_svm, 0.02,700 , regimes.regime_of(0.02,700) )
    #dotprod_and_decison_func_of(meta_svm, 0.02,1400, regimes.regime_of(0.02,1400))
    
    ################# apply SVM to test data ##################
    for s,t in [(s,t) for s in p.selection for t in p.times]:
        
        # evaluate power on test set
        test_data, test_states = data_and_states_for(p.case_type, p.cont_type, [s], [t], partition_at, p.last_sim) # test data
        print "test-data: %i, test-states: %i" % (len(test_data), len(test_states))
        st2pow[s,t], svm2frac, frac_s_str = learn.metaSVM_pow(test_data, test_states, meta_svm, s, t, svm_dict)
        
        # report
        fracs = ""
        for i, frac in sorted(svm2frac.iteritems()): fracs += "%g\t" % frac
        if(frac_s_str == None):
            to_write = "%g\t%i\t%g\t%s" % (s, t, st2pow[s,t], fracs)
        else:
             to_write = "%g\t%i\t%g\t%s\t%s" % (s, t, st2pow[s,t], fracs, frac_s_str)
             
        print to_write
        pfile_text.append(to_write)
    
    # write power stats for file
    write_to_file('power_SVM_general.txt', pfile_text)
    
    ######## if none input, make & pickle the final meta-SVM ########
    if(gen_svm_file is None):
        # train meta-SVM on training-set
        sys.stdout.write('Training final meta-SVM... ')
        train_mix = data_mix_for_SVM_general(p.first_sim, p.last_sim)
        
        # set equal regime-svm weights and error terms
        regimes_ind = [i for i in range(len(train_mix)/2)]
        svm_weights = dict( zip(regimes_ind, [1.0]*len(regimes_ind)) )
        svm_e_terms = dict( zip(regimes_ind, [0.1]*len(regimes_ind)) )
        
        meta_svm = learn.train_metaSVM(train_mix, svm_weights, svm_e_terms)        
        print 'done.\n'
        
        with open(gen_svm_file_to_write, mode='wb') as gen_svm_fh:
                pickle.dump(meta_svm, gen_svm_fh)