Example #1
0
def Test(train, test, data_num, input_dim, hidden_dim, output_dim, iteration_range, B, alpha_range, lamb_range, seed):
    result = DataFrame(
        columns=[
            "ANN",
            "data_num",
            "input_dim",
            "hidden_dim",
            "output_dim",
            "iteration",
            "B",
            "alpha",
            "lamb",
            "seed",
            "train_accuracy",
            "accuracy",
            "error_bar",
        ]
    )
    for a in alpha_range:
        for i in iteration_range:
            for l in lamb_range:
                # print a, i, l
                test_ps = []
                for j in range(10):
                    print j
                    if train.__name__ == "image_train":
                        w, train_p, m1, m2 = train(data_num, input_dim, hidden_dim, output_dim, i, B, a, l, seed)
                    else:
                        w, train_p = train(data_num, input_dim, hidden_dim, output_dim, i, B, a, l, seed)
                    s = np.random.randint(0, 1000)
                    test_p = test(w, data_num, hidden_dim, input_dim, s, m1, m2)
                    test_ps.append(test_p)
                r = [
                    train.__name__,
                    data_num,
                    input_dim,
                    hidden_dim,
                    output_dim,
                    i,
                    B,
                    a,
                    l,
                    s,
                    train_p,
                    np.mean(test_ps),
                    np.sqrt(1.0 / (10 - 1)) * np.std(test_ps),
                ]
                print train.__name__, "alpha:", a, "iteration:", i, "lambda:", l, "train_accuracy:", train_p, "accuracy:", np.mean(
                    test_ps
                ), "+=", np.sqrt(
                    1.0 / (10 - 1)
                ) * np.std(
                    test_ps
                )
                result.loc[len(result)] = r
    result.to_csv(train.__name__ + ".csv")
Example #2
0
if __name__ == "__main__":

    # Problem 1
    # 1(a) test on a tiny graph
    dim = 5
    print SA(graphColor, G, np.zeros(dim), T, 1)
    # test on different size check board
    result = DataFrame(columns=["Size", "error", "error_bar"])
    for i in range(2, 11):
        m = genGraph(i)
        err = []
        for j in range(10):
            X, error = SA(graphColor, m, np.zeros(i ** 2), T, N)
            err.append(error)
        print "Size: ", i ** 2, "error:", np.mean(err), "error_bar: ", np.sqrt(1.0 / 9) * np.std(err)
        result.loc[len(result)] = [i ** 2, np.mean(err), np.sqrt(1.0 / 9) * np.std(err)]
    result.to_csv("test_results/check-board-coloring.csv")

    # 1(b) test on multicolor
    print SA(graphColor, G, np.zeros(dim), T, 3)

    # Problem 2
    def f1(x):
        return (x[0] - 1) ** 2 + (x[1] - 1) ** 2

    def f1_2(x, y):
        return (x - 1) ** 2 + (y - 1) ** 2
        # def RB(x):
        # 	return pow(1-x[0],2)+100*pow(x[1]-pow(x[0],2),2)
        # sareal = DataFrame(columns = ['error','error_bar','time','time_bar'])
        # coor = DataFrame(columns = ['error','error_bar','time','time_bar'])