Esempio n. 1
0
def run():
    X_train, Y_train = load_training_data()
    """
    X_train=crop_images (X_train)
    print("Train images cropped")
    print(X_train.shape())
    """

    X_train, Y_train = rotate_dataset(X_train, Y_train, 1)
    print("Training Data Rotated")
##    X_train, Y_train = nudge_dataset(X_train, Y_train, nudge_size = 2)
##    print("Training Data augmented")

    n_features = X_train.shape[1]
    n_classes = 10

    "Unsupervised learning+pretraining"
    test_data = get_test_data_set()
    #test_data = rotate_dataset(test_data, None, 3)
    # test_data=crop_images (test_data)
    XX = np.vstack((X_train,rotate_dataset(test_data, None, 3)))
    print("Stacked, unlabelled XX.shape: ",XX.shape)

##    aeelm = AEELMTransformer(n_components=700, activation='relu')
##    X_transform = aeelm.fit(XX).transform(XX)
##    print("AE-ELM 1/2")
##
##    aeelm_2 = AEELMTransformer(n_components=640, activation='relu')
##    X_train = aeelm_2.fit(X_transform).transform(aeelm.transform(X_train))
##    print("AE-ELM 2/2 - First autoencoder trained")
    p = Pipeline([
        ('aelm1', AEELMTransformer(n_components=710, activation='tanh')),
        # ('aelm2', AEELMTransformer(n_components=670, activation='relu')),
        ('aelm3', AEELMTransformer(n_components=500, activation='tanh')),
        ('aelm4', AEELMTransformer(n_components=400, activation='relu')),
        ('aelm5', AEELMTransformer(n_components=350, activation='relu')),
##        ('aelm6', AEELMTransformer(n_components=480, activation='tanh'))
    ])

    p.fit(XX)
    print("AE-ELM -  autoencoder trained")
    X_train = p.transform(X_train)
    test_data = p.transform(test_data)

    classifier = LogisticRegression(C=100)
    print("AE-ELM Transformed training Perf:")
    eval_model(classifier, X_train, Y_train, cv=2, n_jobs=3)

    classifier = SVC(kernel="rbf", C=2.8, gamma=.0073,cache_size = 6500,verbose=False)

    # classifier = DBN([n_features, 8000, n_classes],
    #     learn_rates=0.4, learn_rate_decays=0.9 ,epochs=25, verbose=1)

    classifier.fit(X_train, Y_train)

    predictions = classifier.predict(test_data)
    write_predictions_to_csv(predictions)
Esempio n. 2
0
def run():
    X_train, Y_train = load_training_data()

    X_train, Y_train = rotate_dataset(X_train, Y_train, 8)
    X_train, Y_train = nudge_dataset(X_train, Y_train)

    n_features = X_train.shape[1]
    n_classes = 10
    classifier = DBN([n_features, 8000, n_classes], 
        learn_rates=0.4, learn_rate_decays=0.9 ,epochs=75, verbose=1)

    classifier.fit(X_train, Y_train)

    test_data = get_test_data_set()
    predictions = classifier.predict(test_data)
    write_predictions_to_csv(predictions)
Esempio n. 3
0
def continue_gen(net, train_len, pop_size):

    w1 = net.get_weights()
    mut_cnt = int(cfg.MUT_RATE * len(w1))
    print("Training the net!")
    # Initializes the first population
    print("Generation: 0")
    pop = []
    for x in xrange(pop_size):
        new_net = nn.init_net()
        weights = gen.mutate(w1, mut_cnt)
        new_net.put_weights1d(weights)
        pop.append(new_net)

    training_data = helpers.load_training_data()

    # Calculate errors
    errors = gen_err(pop, training_data)

    # Sorts the errors array but only stores indices
    idx_err = sorted(range(len(errors)), key=lambda k: errors[k])

    print(" Smallest error: " + str(errors[idx_err[0]]))

    # Goes through the generations
    counter = 1
    while counter < train_len:

        print("Generation: " + str(counter))

        # Decides best 2 parents based on confidence matrix
        parent1 = pop[idx_err[0]]
        parent2 = pop[idx_err[1]]
        w1 = parent1.get_weights()
        w2 = parent2.get_weights()
        for x in idx_err[2:]:
            new_w = gen.recombine(w1, w2)
            new_w = gen.mutate(new_w, mut_cnt)
            pop[x].put_weights1d(new_w)

        errors = gen_err(pop, training_data)
        idx_err = sorted(range(len(errors)), key=lambda k: errors[k])
        print(" Smallest error: " + str(errors[idx_err[0]]))
        counter += 1

    best_net = pop[idx_err[0]]
    return best_net
Esempio n. 4
0
def continue_bp(net, test_len, testing_name=""):

    # load training data
    training_inputs = helpers.load_training_data()

    # writer for testing data
    csv_file = None
    writer = None
    if testing_name != "":
        path = "./results/" + testing_name
        csv_file = open(path, 'wb+')
        writer = csv.writer(csv_file, delimiter=',')

    # train the net
    for i in range(test_len):

        # Print out progress:
        if i % 100 == 0:
            per = float(i) / float(test_len) * 100
            print("Training: " + str(format(per, '.2f')) + "%")

            # write testing results if testing
            if writer is not None:
                data = [i, helpers.get_testing_error(net)]
                writer.writerow(data)

        # randomly pick an emotion to train
        emotion = random.choice(range(len(cfg.emojis)))

        # pick a specific test case
        test_case = random.choice(training_inputs[emotion])

        net.train(test_case, cfg.outputs[emotion])

    # write testing results if testing
    if writer is not None:
        data = [test_len, helpers.get_testing_error(net)]
        writer.writerow(data)

    # close the file
    if csv_file is not None:
        csv_file.close()

    return net
Esempio n. 5
0
def run():
    X_train, Y_train = load_training_data()

    X_train, Y_train = rotate_dataset(X_train, Y_train, 8)
    X_train, Y_train = nudge_dataset(X_train, Y_train)

    n_features = X_train.shape[1]
    n_classes = 10
    classifier = DBN([n_features, 8000, n_classes],
                     learn_rates=0.4,
                     learn_rate_decays=0.9,
                     epochs=75,
                     verbose=1)

    classifier.fit(X_train, Y_train)

    test_data = get_test_data_set()
    predictions = classifier.predict(test_data)
    write_predictions_to_csv(predictions)
Esempio n. 6
0
def genetic_train(pop_size, test_len, testing_name):

    writer = None
    csv_file = None
    if testing_name != "":
        path = "./results/" + testing_name
        csv_file = open(path, 'wb+')
        writer = csv.writer(csv_file, delimiter=',')

    print("Training the net!")
    # Initializes the first population
    print("Generation: 0")
    pop = []
    for x in xrange(pop_size):
        net = nn.init_net()
        pop.append(net)

    training_data = helpers.load_training_data()

    # Calculate errors
    errors = gen_err(pop, training_data)

    # Sorts the errors array but only stores indices
    idx_err = sorted(range(len(errors)), key=lambda k: errors[k])

    print(" Smallest error: " + str(errors[idx_err[0]]))

    if writer is not None:
        data = [0, helpers.get_testing_error(pop[idx_err[0]])]
        writer.writerow(data)

    # Goes through the generations
    counter = 1
    while errors[idx_err[0]] > 0.1 and counter < test_len:

        print("Generation: " + str(counter))

        # Decides best 2 parents based on confidence matrix
        parent1 = pop[idx_err[0]]
        parent2 = pop[idx_err[1]]
        w1 = parent1.get_weights()
        w2 = parent2.get_weights()
        mut_cnt = int(cfg.MUT_RATE * len(w1))
        for x in idx_err[2:]:
            new_w = gen.recombine(w1, w2)
            new_w = gen.mutate(new_w, mut_cnt)
            pop[x].put_weights1d(new_w)

        errors = gen_err(pop, training_data)
        idx_err = sorted(range(len(errors)), key=lambda k: errors[k])
        print(" Smallest error: " + str(errors[idx_err[0]]))

        # write the current test_error
        if writer is not None and counter % 5 == 0:
            data = [counter, helpers.get_testing_error(pop[idx_err[0]])]
            writer.writerow(data)
        counter += 1

    # write the last test error:
    if writer is not None:
        data = [counter, helpers.get_testing_error(pop[idx_err[0]])]
        writer.writerow(data)

    # close the file
    if csv_file is not None:
        csv_file.close()

    best_net = pop[idx_err[0]]
    return best_net
Esempio n. 7
0
import network
import helpers
import numpy as np


# In[2]:


import matplotlib.pyplot as plt


# In[9]:


img, lbl = helpers.load_training_data()
X, Y = helpers.convert_to_xy(img, lbl)
X_train, Y_train, X_test, Y_test = helpers.create_train_set(X,Y)
learning_rate = 0.01
layer_dims = [X.shape[0], 100, 20, 30, Y.shape[0]]
net = network.Network(layer_dims, learning_rate)
costs = net.train(X_train, Y_train, 1000)


# In[10]:


plt.plot(costs)
plt.show()

Esempio n. 8
0
def main(argv):
    net_name = ""
    train_len = 500
    alg = ""
    autosave_name = ""
    testing_name = ""

    # read command line arguments
    try:
        opts, args = getopt.getopt(argv, "hn:l:c:a:as:t:")
    except getopt.GetoptError:
        print ("Usage: main.py -n <net file to use> -l <training length>, -c <convert images again>, -a <algorithm to use (GEN, BP)> -s <name for autosaving net> -t <name for auto testing file>")
        sys.exit(2)
    if not argv:
        print ("Usage: main.py -n <net file to use> -l <training length>, -c <convert images again>, -a <algorithm to use (GEN, BP)> -s <name for autosaving net> -t <name for auto testing file>")
        sys.exit(2)
    for opt, arg in opts:

        # print usage
        if opt == '-h':
            print ("Usage: main.py -n <net file to use> -l <training length>, -c <convert images again>, -a <algorithm to use (GEN, BP)> -s <name for autosaving net> -t <name for auto testing file>")
            sys.exit()

        # store the name of the net to be loaded
        elif opt == "-n":
            net_name = arg

        # store training length
        elif opt == "-l":
            train_len = int(arg)

        # convert images
        elif opt == "-c":
            converter.convert_images()

        # store the name for the net to be saved
        elif opt == "-s":
            if arg != "":
                autosave_name = arg
            else:
                print("Invalid input for -s")

        # store the algorithm to use
        elif opt == "-a":
            print(arg)
            if arg == "GEN" or arg == "BP":
                alg = arg
            else:
                print ("Non-valid algorithm. Choose either GEN for genetic algorithm or BP for Backpropagation")
                sys.exit()
        elif opt == "-t":
            testing_name = arg


    # initialize net
    # if a net name was given, load that net
    if net_name != "":
        net = helpers.load_net(net_name)

    elif alg == "":
        print ("Neither a previous net or training algorithm was chosen. Try again!")
        sys.exit()

    elif alg == "GEN":

        # train the net using gen alg
        net = gen.genetic_train(cfg.POP_SIZE, train_len, testing_name)

        # if autosave name was given, save the net
        if autosave_name != "":
            helpers.save_net(net, autosave_name)
    else:

        # train the net using BP
        net = nn.backprop_train(train_len, testing_name)

         # if autosave name was given, save the net
        if autosave_name != "":
            helpers.save_net(net, autosave_name)

    print("Net ready!")

    # Ask the user for a command
    while True:
        training_data = []
        print("Choose an action:")
        print("Q - Quit")
        print("T - Run training data")
        print("F - Test a file")
        print("S - Save the net")
        print("C - Continue training")
        print("E - Calculate Error")
        choice = raw_input("")

        # Exit
        if choice in ("Q", "q"):
            print("Exiting!")
            sys.exit()

        # run the training results
        elif choice in ("T", "t"):
            helpers.training_results(net)

        # test a specific file
        elif choice in ("F", "f"):

            # get file path
            test_file = raw_input("input path to file: ")
            while test_file == "":
                test_file = raw_input("Try again!")

            # Check if file is accessible
            if not os.access(test_file, os.R_OK):
                print("file is not accessible :( ")
            else:
                helpers.test_image(test_file, net)
        elif choice in ("S", "s"):

            # Save the net
            name = ""
            while name == "":
                name = raw_input("filename: ")
            helpers.save_net(net, name)
        elif choice in ("C", "c"):

            # prompt the user for parameters
            train_len = raw_input("training length? ")
            while not train_len.isdigit():
                train_len = raw_input("try again! ")
            train_len = int(train_len)
            alg = raw_input("Which algorithm to use? GEN/BP: ")
            while not (alg == "GEN" or alg == "BP"):
                alg = raw_input("Try again! GEN/BP: ")

            # train the net
            if alg == "GEN":
                gen.continue_gen(net, train_len, cfg.POP_SIZE)
            else:
                nn.continue_bp(net, train_len)
        elif choice in ("E", "e"):

            # if training data is not already loaded, load it
            if not training_data:
                training_data = helpers.load_training_data()
            print("Calculating...")

            # print out the total error over training data
            print("Total error for net: " + str(gen.net_error(net, training_data)))
from network import Network, predict_from_output
from helpers import load_training_data, convert_to_xy
import numpy as np

img, lbl = load_training_data()
X, Y = convert_to_xy(img, lbl)
X_train, Y_train, X_test, Y_test = create_train_set(X, Y)

learning_rate = 0.003
layer_dims = [X.shape[0], 30, 20, Y.shape[0]]
net = Network(layer_dims, learning_rate)

costs = net.train(X, Y, 500)

print costs