Example #1
0
def main():
    filenames = ["Iris", "Wine", "Cancer", "Heart", "CreditApproval", "Baloon", "TicTac", "Ions", "Zoo",
                 "Lenses", "Balance"]
    problemlist = np.array(range(11))
    input = np.array([4, 13, 9, 13, 15, 4, 9, 34, 16, 4, 4])
    hidden = np.array([6, 6, 6, 16, 20, 5, 30, 8, 6, 5, 8])
    output = np.array([2, 3, 1, 1, 1, 1, 1, 1, 7, 3, 3])

    samplelist = [5000, 8000, 10000, 20000, 15000, 5000, 20000, 5000, 3000, 5000, 2000]
    x = 3

    filetrain = open('Results/train.txt', 'r')
    filetest = open('Results/test.txt', 'r')
    filestdtr = open('Results/std_tr.txt','r')
    filestdts = open('Results/std_ts.txt', 'r')
    
    train_accs = np.loadtxt(filetrain)
    test_accs = np.loadtxt(filetest)

    train_stds = np.loadtxt(filestdtr)
    test_stds = np.loadtxt(filestdts)
    
    filetrain.close()
    filetest.close()
    filestdtr.close()
    filestdts.close()
    
    

    if x == 3:
        w_limit =  0.02
        tau_limit = 0.2
    #if x == 4:
        #w_limit =  0.02
        #tau_limit = 0.1

    for problem in []:

        #if os.path.isfile("Results/"+filenames[problem]+"_rmse.txt"):
        #    print filenames[problem]
        #    continur

        [traindata, testdata, baseNet] = setexperimentdata(problem)

        topology = [input[problem], hidden[problem], output[problem]]

        random.seed(time.time())

        numSamples = samplelist[problem]   # need to decide yourself

        mcmc = MCMC(numSamples, traindata, testdata, topology)  # declare class

        [pos_w, pos_tau, fx_train, fx_test, x_train, x_test, rmse_train, rmse_test, accept_ratio] = mcmc.sampler(w_limit, tau_limit, filenames[problem])
        print '\nsucessfully sampled: '+ str(accept_ratio)+ ' samples accepted'

        burnin = 0.1 * numSamples  # use post burn in samples

        pos_w = pos_w[int(burnin):, ]
        pos_tau = pos_tau[int(burnin):, ]

        print("fx shape:"+str(fx_test.shape))
        print("fx_train shape:"+ str(fx_train.shape))

        fx_mu = fx_test.mean(axis=0)
        fx_high = np.percentile(fx_test, 95, axis=0)
        fx_low = np.percentile(fx_test, 5, axis=0)

        fx_mu_tr = fx_train.mean(axis=0)
        fx_high_tr = np.percentile(fx_train, 95, axis=0)
        fx_low_tr = np.percentile(fx_train, 5, axis=0)

        pos_w_mean = pos_w.mean(axis=0)
        # np.savetxt(outpos_w, pos_w_mean, fmt='%1.5f')


        rmse_tr = np.mean(rmse_train[int(burnin):])
        rmsetr_std = np.std(rmse_train[int(burnin):])
        rmse_tes = np.mean(rmse_test[int(burnin):])
        rmsetest_std = np.std(rmse_test[int(burnin):])
        # print rmse_tr, rmsetr_std, rmse_tes, rmsetest_std
        # np.savetxt(outres, (rmse_tr, rmsetr_std, rmse_tes, rmsetest_std, accept_ratio), fmt='%1.5f')

        ytestdata = testdata[:, input[problem]:]
        ytraindata = traindata[:, input[problem]:]
        
        train_acc = []
        test_acc = []

        for fx in fx_train:
            count = 0
            for index in range(fx.shape[0]):
                if np.allclose(fx[index],ytraindata[index],atol = 0.2):
                    count += 1
            train_acc.append(float(count)/fx.shape[0]*100)
        
        for fx in fx_test:
            count = 0
            for index in range(fx.shape[0]):
                if np.allclose(fx[index],ytestdata[index],atol = 0.5):
                    count += 1
            test_acc.append(float(count)/fx.shape[0]*100)
       
        train_acc = np.array(train_acc[int(burnin):])
        train_std = np.std(train_acc[int(burnin):])
 
        test_acc = np.array(test_acc[int(burnin):])
        test_std = np.std(test_acc[int(burnin):])

        train_acc_mu = train_acc.mean() 
        test_acc_mu = test_acc.mean()      
    
        train_accs[problem] = train_acc_mu
        test_accs[problem] = test_acc_mu
        train_stds[problem] = train_std
        test_stds[problem] = test_std
         
        testResults = np.c_[ytestdata, fx_mu, fx_high, fx_low]

        trainResults = np.c_[ytraindata, fx_mu_tr, fx_high_tr, fx_low_tr]

        # Write RMSE to
        with open("Results/" +
                  filenames[problem] + "_rmse" + ".txt", 'w') as fil:
            rmse = [rmse_tr, rmsetr_std, rmse_tes, rmsetest_std]
            rmse = "\t".join(list(map(str, rmse))) + "\n"
            fil.write(rmse)

    n_groups = len(filenames)
    fig, ax = plt.subplots()  
    index = np.arange(n_groups)
    bar_width = 0.2
    opacity = 0.8
    capsize = 3
    
    filetrain = open('Results/train.txt', 'w+')
    filetest = open('Results/test.txt', 'w+')
    filestdtr = open('Results/std_tr.txt','w+')
    filestdts = open('Results/std_ts.txt', 'w+')

    np.savetxt(filetrain, train_accs, fmt='%2.2f')
    np.savetxt(filestdtr, train_stds, fmt='%2.2f')
    np.savetxt(filetest, test_accs, fmt='%2.2f')
    np.savetxt(filestdts, test_stds, fmt='%2.2f')

    filetrain.close()
    filetest.close()
    filestdtr.close()
    filestdts.close()
   
    print(train_accs) 
    plt.bar(index + float(bar_width)/2, train_accs, bar_width,
                    alpha = opacity,
                    error_kw = dict(elinewidth=1, ecolor='r'),
                    yerr = train_stds,
                    color = 'c',
                    label = 'train')

    plt.bar(index + float(bar_width)/2 + bar_width, test_accs, bar_width,
                    alpha = opacity,
                    error_kw = dict(elinewidth=1, ecolor='g'),
                    yerr = test_stds,
                    color = 'b',
                    label = 'test')
    plt.xlabel('Datasets')
    plt.ylabel('Accuracy')
    plt.xticks(index+bar_width, filenames, rotation=70)
    plt.legend()

    plt.tight_layout()
    plt.savefig('barplt.png')
    plt.show()
def main():

    #os.remove('out2_randomdepth.txt')
    np.random.seed()
    maxEpoch = np.array([500, 500])
    learnRate = 0.5
    fileout1 = open('out1_res.txt', 'a')
    fileout2 = open('out2_res.txt', 'a')

    moduledecomp = [
        0.25, 0.5, 0.75, 1
    ]  # decide what will be number of features for each group of taskdata correpond to module

    for problem in range(0, 13):
        [TrainData, TestData, base] = setexperimentdata(problem)

        MaxTime = maxEpoch[problem]

        TrSamples = np.size(TrainData, 0)
        TestSize = np.size(TestData, 0)

        MaxRun = 30  # number of experimental runs

        MinCriteria = 97  #stop when learn 95 percent

        numModules = 4  # first decide number of  modules (or ensembles for comparison)
        inputfeatures = base[0]  # total num inputfeatures for the prob

        mtaskNet = np.array([base, base, base, base])
        for i in xrange(1, numModules):

            mtaskNet[i - 1][0] = moduledecomp[i - 1] * inputfeatures
            mtaskNet[i][1] += (
                i * 2
            )  # in this example, we have fixed numner  output neurons. input for each task is termined by feature group size.
    # we adapt the number of hidden neurons for each task.
        print mtaskNet  # print network topology of all the modules that make the respective tasks. Note in this example, the tasks aredifferent network topologies given by hiddent number of hidden layers.

        trainPerf = np.random.randn(MaxRun, numModules)
        testPerf = np.random.randn(MaxRun, numModules)
        meanTrain = np.zeros(numModules)
        stdTrain = np.zeros(numModules)
        meanTest = np.zeros(numModules)
        stdTest = np.zeros(numModules)
        x = np.zeros(numModules)

        trainMSE = np.random.randn(MaxRun, numModules)
        testMSE = np.random.randn(MaxRun, numModules)
        Epochs = np.zeros(MaxRun)
        Time = np.zeros(MaxRun)

        for transKnow in xrange(
                1, 2
        ):  # transKnow = 0 # 1 is for MT knowledge transfer. 0 is for no transfer (simple ensemble learning)
            for run in xrange(0, MaxRun):
                mt = MTnetwork(mtaskNet, TrainData, TestData, MaxTime,
                               MinCriteria, learnRate, numModules, transKnow)
                (erPlot, trainMSE[run, :], trainPerf[run, :], testMSE[run, :],
                 testPerf[run, :]) = mt.mainAlg()
                x = [problem, transKnow, run]
                print x, testPerf[run, :]
    #np.savetxt(fileout1, (problem, transKnow, run ), fmt='%1.1f')
    #np.savetxt(fileout1, (testPerf[run,:]), fmt='%1.2f',  newline=' ')

            for module in xrange(0, numModules):
                meanTrain[module] = np.mean(trainPerf[:, module])
                stdTrain[module] = np.std(trainPerf[:, module])
                meanTest[module] = np.mean(testPerf[:, module])
                stdTest[module] = np.std(testPerf[:, module])

            print meanTrain
            print stdTrain
            print meanTest
            print stdTest
            np.savetxt(fileout2, (problem, transKnow), fmt='%1.1f')
            np.savetxt(fileout2, (meanTrain, stdTrain, meanTest, stdTest),
                       fmt='%1.2f')
Example #3
0
def main():
    moduledecompratio = [0.25, 0.50, 0.75, 1]
    filenames = [
        "Iris", "Wine", "Cancer", "Heart", "CreditApproval", "Baloon",
        "TicTac", "Ions", "Zoo", "Lenses", "Balance", "Robot-Four",
        "Robot-TwentyFour"
    ]
    problemlist = np.array(range(13))
    input = np.array([4, 13, 9, 13, 15, 4, 9, 34, 16, 4, 4, 4, 24])
    hidden = np.array([6, 6, 6, 16, 20, 5, 30, 8, 6, 5, 8, 14, 14])
    output = np.array([2, 3, 1, 1, 1, 1, 1, 1, 7, 3, 3, 4, 4])

    samplelist = [
        5000, 80, 10000, 20000, 15000, 5000, 20000, 5000, 3000, 5000, 2000,
        20000, 10000
    ]
    x = 3
    subtasks = 4

    # filetrain = open('Results/train.txt', 'r')
    # filetest = open('Results/test.txt', 'r')
    # filestdtr = open('Results/std_tr.txt', 'r')
    # filestdts = open('Results/std_ts.txt', 'r')

    # train_accs = np.loadtxt(filetrain)
    # test_accs = np.loadtxt(filetest)
    #
    # train_stds = np.loadtxt(filestdtr)
    # test_stds = np.loadtxt(filestdts)

    # filetrain.close()
    # filetest.close()
    # filestdtr.close()
    # filestdts.close()

    numproblems = problemlist.size

    train_accs = np.zeros((numproblems, subtasks))
    test_accs = np.zeros((numproblems, subtasks))

    train_stds = np.zeros((numproblems, subtasks))
    test_stds = np.zeros((numproblems, subtasks))

    if x == 3:
        w_limit = 0.02
        tau_limit = 0.2
    # if x == 4:
    # w_limit =  0.02
    # tau_limit = 0.1

    for problem in [0]:

        [traindata, testdata, baseNet] = setexperimentdata(problem)
        # print(baseNet)
        IN = input[problem]
        moduledecomp = [int(r * IN) for r in moduledecompratio]

        mtaskNet = np.array([baseNet, baseNet, baseNet, baseNet])
        # print(mtaskNet)

        for i in xrange(1, subtasks):
            # print(mtaskNet)
            mtaskNet[i - 1][0] = moduledecomp[i - 1]
            mtaskNet[i][1] += (
                i * 2
            )  # in this example, we have fixed numner  output neurons. input for each task is termined by feature group size.
            # we adapt the number of hidden neurons for each task.

        print(problem, mtaskNet)

        random.seed(time.time())

        numSamples = samplelist[problem]  # need to decide yourself

        mcmc = MCMC(mtaskNet, numSamples, traindata, testdata,
                    subtasks)  # declare class

        [
            pos_w, pos_tau, fx_train, fx_test, x_train, x_test, rmse_train,
            rmse_test, accept_ratio
        ] = mcmc.sampler(w_limit, tau_limit, filenames[problem])

        print 'sucessfully sampled: ' + str(accept_ratio) + ' samples accepted'

        burnin = 0.1 * numSamples  # use post burn in samples

        pos_w = pos_w[int(burnin):, ]
        pos_tau = pos_tau[int(burnin):, ]

        print("fx shape:" + str(fx_test.shape))
        print("fx_train shape:" + str(fx_train.shape))

        print(fx_test[int(burnin):], fx_train[int(burnin):])

        fx_tr_1 = fx_train[int(burnin):, 0, :]
        fx_tr_2 = fx_train[int(burnin):, 1, :]
        fx_tr_3 = fx_train[int(burnin):, 2, :]
        fx_tr_4 = fx_train[int(burnin):, 3, :]

        fx_train = np.asarray([fx_tr_1, fx_tr_2, fx_tr_3, fx_tr_4])

        fx_ts_1 = fx_test[int(burnin):, 0, :]
        fx_ts_2 = fx_test[int(burnin):, 1, :]
        fx_ts_3 = fx_test[int(burnin):, 2, :]
        fx_ts_4 = fx_test[int(burnin):, 3, :]

        # print(fx_ts_1.shape)

        fx_test = np.asarray([fx_ts_1, fx_ts_2, fx_ts_3, fx_ts_4])

        pos_w_mean = pos_w.mean(axis=0)
        # np.savetxt(outpos_w, pos_w_mean, fmt='%1.5f')

        rmse_tr = np.mean(rmse_train[int(burnin):])
        rmsetr_std = np.std(rmse_train[int(burnin):])
        rmse_tes = np.mean(rmse_test[int(burnin):])
        rmsetest_std = np.std(rmse_test[int(burnin):])
        # print rmse_tr, rmsetr_std, rmse_tes, rmsetest_std
        # np.savetxt(outres, (rmse_tr, rmsetr_std, rmse_tes, rmsetest_std, accept_ratio), fmt='%1.5f')

        ytestdata = testdata[:, input[problem]:]
        ytraindata = traindata[:, input[problem]:]

        train_acc = np.zeros((subtasks, fx_tr_1.shape[0]))
        test_acc = np.zeros((subtasks, fx_ts_1.shape[0]))

        # print(fx_test,fx_train)

        for fx_sub_in in range(fx_train.shape[0]):
            fx_sub = fx_train[fx_sub_in]
            acc = np.zeros(fx_sub.shape[0])
            for fx_in in range(fx_sub.shape[0]):
                count = 0
                for index in range(fx_sub[fx_in].shape[0]):
                    if np.isclose(fx_sub[fx_in][index],
                                  ytraindata[index],
                                  atol=0.2).all():
                        count += 1
                acc[fx_in] = (float(count) / fx_sub[fx_in].shape[0] * 100)
            train_acc[fx_sub_in] = acc

        for fx_sub_in in range(fx_test.shape[0]):
            fx_sub = fx_test[fx_sub_in]
            acc = np.zeros(fx_sub.shape[0])
            for fx_in in range(fx_sub.shape[0]):
                count = 0
                for index in range(fx_sub[fx_in].shape[0]):
                    if np.isclose(fx_sub[fx_in][index],
                                  ytestdata[index],
                                  atol=0.5).all():
                        count += 1
                acc[fx_in] = (float(count) / fx_sub[fx_in].shape[0] * 100)
            test_acc[fx_sub_in] = acc

        train_accs[problem] = train_acc.mean(axis=1)
        test_accs[problem] = test_acc.mean(axis=1)

        train_stds[problem] = np.std(train_acc, axis=1)
        test_stds[problem] = np.std(test_acc, axis=1)

        print(train_stds[problem], test_stds[problem], train_accs[problem],
              test_accs[problem])