예제 #1
0
def helper(partb):
    dir_path = os.path.dirname(os.path.realpath(__file__))
    data = BankNoteData.BankNoteData(dir_path + '/../Data/bank_note/train.csv',
                                     shift_origin=True)
    test_data = BankNoteData.BankNoteData(dir_path +
                                          '/../Data/bank_note/test.csv',
                                          shift_origin=True)

    layer_widths = [5, 10, 25, 50, 100]
    train_percentages = []
    test_percentages = []
    max_iters = 5
    for layer_width in layer_widths:
        print("Running test width hidden layer width: %i" % layer_width)
        if partb:
            weights = np.zeros((layer_width, layer_width, 4))
            for i in range(0, layer_width):
                for j in range(0, layer_width):
                    weights[i, j, 2] = np.random.normal()

            for i in range(0, layer_width):
                weights[i, 1, 3] = np.random.normal()

            for i in range(0, data.features.shape[1]):
                for j in range(0, layer_width):
                    weights[i, j, 1] = np.random.normal()

        else:
            weights = np.zeros((layer_width, layer_width, 4))

        three_layer_nn = ThreeLayerNN.ThreeLayerNN(
            num_units_per_layer=layer_width, weights=weights)
        grad_descent = GradientDescent.GradientDescent(features=data.features,
                                                       labels=data.output)
        results = grad_descent.fit_stochastic(
            max_iters=max_iters,
            args=None,
            weights=weights,
            step_function=training_schedule,
            obj_func=three_layer_nn.objective_function,
            grad_func=three_layer_nn.gradient)

        train_percentage = get_percentages(data, three_layer_nn.predict)
        test_percentage = get_percentages(test_data, three_layer_nn.predict)
        train_percentages.append(train_percentage)
        test_percentages.append(test_percentage)
        print("Train error percentage was %.16f" % train_percentage)
        print("Test error percentage was %.16f" % test_percentage)

        t = np.linspace(0, max_iters, max_iters)
        plt.plot(t, results[1])
        plt.show()

    plt.semilogx(layer_widths, train_percentages, label='Train Percentages')
    plt.semilogx(layer_widths, test_percentages, label='Test Percentages')
    plt.legend()
    plt.show()
def choice_c():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    data = BankNoteData.BankNoteData(dir_path + '/../../Data/bank_note/train.csv', shift_origin=True)

    test_data = BankNoteData.BankNoteData(dir_path + '/../../Data/bank_note/test.csv', shift_origin=True)

    perceptron = Perceptron.Perceptron(10, data.features, data.output, 0.5, perceptron_type='averaged')
    perceptron.fit()

    train_percentage = get_percentages(perceptron, data)
    test_percentage = get_percentages(perceptron, test_data)

    print("Weight vector was:")
    print(perceptron.weights)
    print("Train error percentage was %.16f" % train_percentage)
    print("Test error percentage was %.16f" % test_percentage)
예제 #3
0
def helper(parta, obj_fun, grad_func):
    dir_path = os.path.dirname(os.path.realpath(__file__))
    data = BankNoteData.BankNoteData(
        dir_path + '/../Data/bank_note/train.csv',
        shift_origin=True,
    )
    test_data = BankNoteData.BankNoteData(dir_path +
                                          '/../Data/bank_note/test.csv',
                                          shift_origin=True)

    if parta:
        variances = np.array([0.01, 0.1, 0.5, 1, 3, 5, 10, 100])
    else:
        variances = [0]
    max_iters = 100
    train_percentages = []
    test_percentages = []
    for var in variances:
        grad_descent = GradientDescent.GradientDescent(features=data.features,
                                                       labels=data.output)
        results = grad_descent.fit_stochastic(max_iters=max_iters,
                                              obj_func=obj_fun,
                                              grad_func=grad_func,
                                              step_function=training_schedule,
                                              weights=np.zeros(
                                                  data.features.shape[1]),
                                              args=var)

        train_percentage_ = lr.get_percentages(data, results[0])
        test_percentage_ = lr.get_percentages(test_data, results[0])
        train_percentages.append(train_percentage_)
        test_percentages.append(test_percentage_)
        print("Train error percentage was %.16f" % train_percentage_)
        print("Test error percentage was %.16f" % test_percentage_)

        t = np.linspace(0, max_iters, max_iters)
        plt.plot(t, results[1], label='Objective Function')
        plt.legend()
        plt.show()

    if parta:
        plt.semilogx(variances, train_percentages, label='Train Percentages')
        plt.semilogx(variances, test_percentages, label='Test Percentages')
        plt.legend()
        plt.show()
def choice_b():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    data = BankNoteData.BankNoteData(dir_path + '/../../Data/bank_note/train.csv', shift_origin=True)

    test_data = BankNoteData.BankNoteData(dir_path + '/../../Data/bank_note/test.csv', shift_origin=True)

    perceptron = Perceptron.Perceptron(10, data.features, data.output, 0.5, perceptron_type='voted')
    perceptron.fit()

    print("The vectors and their respective count are:")
    np.set_printoptions(precision=4)
    for i in range(0, len(perceptron.weights[0])):
        print(perceptron.weights[0][i], "  : %s" % (str(perceptron.weights[1][i])), )

    train_percentage = get_percentages(perceptron, data)
    test_percentage = get_percentages(perceptron, test_data)

    print("Train error percentage was %.16f" % train_percentage)
    print("Test error percentage was %.16f" % test_percentage)
def kernel_perceptron_test():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    data = BankNoteData.BankNoteData(dir_path + '/../Data/bank_note/train.csv',
                                     shift_origin=True)
    test_data = BankNoteData.BankNoteData(dir_path +
                                          '/../Data/bank_note/test.csv',
                                          shift_origin=True)

    gammas = [0.01, 0.1, 0.5, 1., 2., 5., 10., 100.]
    for gamma in gammas:
        print("Running Kernel Perceptron with Gamma = %.16f" % gamma)
        kernel_perceptron = KernelPerceptron.KernelPerceptron(
            features=data.features, labels=data.output, gamma=gamma)
        perceptron = kernel_perceptron.fit(num_epochs=10)

        train_percentage = get_percentages(data, kernel_perceptron)
        test_percentage = get_percentages(test_data, kernel_perceptron)

        print("Weight vector was:")
        print(perceptron)
        print("Train error percentage was %.16f" % train_percentage)
        print("Test error percentage was %.16f" % test_percentage)
def choice_d():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    data = BankNoteData.BankNoteData(dir_path + '/../../Data/bank_note/train.csv', shift_origin=True)
    test_data = BankNoteData.BankNoteData(dir_path + '/../../Data/bank_note/test.csv', shift_origin=True)

    train_errors = [[], [], []]
    test_errors = [[], [], []]

    print("Calculating statistics")
    sys.stdout.write('Progress: [%s]' % (' ' * 100))
    sys.stdout.flush()
    for i in range(1, 101):
        standard_perceptron = Perceptron.Perceptron(10, data.features, data.output, 0.5)
        standard_perceptron.fit()
        voted_perceptron = Perceptron.Perceptron(10, data.features, data.output, 0.5, perceptron_type='voted')
        voted_perceptron.fit()
        averaged_perceptron = Perceptron.Perceptron(10, data.features, data.output, 0.5, perceptron_type='averaged')
        averaged_perceptron.fit()

        train_errors[0].append(get_percentages(standard_perceptron, data))
        train_errors[1].append(get_percentages(voted_perceptron, data))
        train_errors[2].append(get_percentages(averaged_perceptron, data))

        test_errors[0].append(get_percentages(standard_perceptron, test_data))
        test_errors[1].append(get_percentages(voted_perceptron, test_data))
        test_errors[2].append(get_percentages(averaged_perceptron, test_data))

        sys.stdout.write('\rProgress: [%s' % ('#' * i))
        sys.stdout.write('%s]' % (' ' * (100 - i)))
        sys.stdout.flush()

    print("\n%24s   %10s   %10s" % ("Standard", "Voted", "Averaged"))
    print("Train Errors: %.8f   %.8f   %.8f" % (float(np.mean(np.array(train_errors[0]))),
                                                float(np.mean(np.array(train_errors[1]))),
                                                float(np.mean(np.array(train_errors[2])))))
    print("Test Errors:  %.8f   %.8f   %.8f" % (float(np.mean(np.array(test_errors[0]))),
                                                float(np.mean(np.array(test_errors[1]))),
                                                float(np.mean(np.array(test_errors[2])))))