Ejemplo n.º 1
0
    def testSingleImprovement(self):
        P, T = loadsyn3(100)

        test, validation = get_validation_set(P, T)
        net1 = build_feedforward(2, 6, 1)

        epochs = 100

        P, T = test
        Y = net1.sim(P)
        [num_correct_first, num_correct_second, initial_test_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        P, T = validation
        Y = net1.sim(P)
        [num_correct_first, num_correct_second, initial_val_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable


        best1 = train_evolutionary(net1, test, validation, epochs, random_range = 5)

        P, T = test
        Y = best1.sim(P)
        [num_correct_first, num_correct_second, genetic_test_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        P, T = validation
        Y = best1.sim(P)
        [num_correct_first, num_correct_second, genetic_val_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        #Test sets
        print(initial_test_performance, genetic_test_performance)
        assert(initial_test_performance < genetic_test_performance)
        #Validation sets
        #print(initial_val_performance, genetic_val_performance)
        #assert(initial_val_performance < genetic_val_performance)

        net2 = build_feedforward(2, 6, 1)

        epochs = 100
        P, T = test
        Y = net2.sim(P)
        [num_correct_first, num_correct_second, initial_test_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        P, T = validation
        Y = net2.sim(P)
        [num_correct_first, num_correct_second, initial_val_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        best2 = traingd(net2, test, validation, epochs, block_size = 10)

        P, T = test
        Y = best2.sim(P)
        [num_correct_first, num_correct_second, gd_test_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        P, T = validation
        Y = best2.sim(P)
        [num_correct_first, num_correct_second, gd_val_performance, num_first, num_second, missed] = stat(Y, T) #@UnusedVariable

        #Assert that an improvement has occurred in each step
        #Test sets
        print(initial_test_performance, genetic_test_performance, gd_test_performance)
        assert(initial_test_performance < gd_test_performance)
Ejemplo n.º 2
0
    def testFilehandling(self):
        print ("Testing network saving/loading")
        net = build_feedforward()

        results1 = net.update([1, 2])

        print (results1)

        filename = path.join(path.expanduser("~"), "test.ann")
        print ("saving and reloading")
        save_network(net, filename)

        net = load_network(filename)
        results2 = net.update([1, 2])
        print (results2)

        print (abs(results1[0] - results2[0]))
        assert abs(results1[0] - results2[0]) < 0.0001  # float doesn't handle absolutes so well
        print ("Good, now testing committee...")

        com = build_feedforward_committee()
        results1 = com.update([1, 2])
        print (results1)

        filename = path.join(path.expanduser("~"), "test.anncom")
        print ("saving and reloading")

        save_committee(com, filename)

        com = load_committee(filename)
        results2 = com.update([1, 2])
        print (results2)

        assert abs(results1[0] - results2[0]) < 0.0001  # float doesn't handle absolutes so well

        print ("Results are good. Testing input parsing....")
        filename = path.join(path.expanduser("~"), "ann_input_data_test_file.txt")
        print ("First, split the file into a test set(80%) and validation set(20%)...")
        inputs, targets = parse_file(filename, targetcols=5, ignorecols=[0, 1, 4], ignorerows=[])
        test, validation = get_validation_set(inputs, targets, validation_size=0.5)
        print (len(test[0]))
        print (len(test[1]))
        print (len(validation[0]))
        print (len(validation[1]))
        assert len(test) == 2
        assert len(test[0]) > 0
        assert len(test[1]) > 0
        assert len(validation) == 2
        assert len(validation[0]) > 0
        assert len(validation[1]) > 0
        print ("Went well, now expecting a zero size validation set...")
        test, validation = get_validation_set(inputs, targets, validation_size=0)
        print (len(test[0]))
        print (len(test[1]))
        print (len(validation[0]))
        print (len(validation[1]))
        assert len(test) == 2
        assert len(test[0]) > 0
        assert len(test[1]) > 0
        assert len(validation) == 2
        assert len(validation[0]) == 0
        assert len(validation[1]) == 0
        print ("As expected. Now a 100% validation set...")
        test, validation = get_validation_set(inputs, targets, validation_size=1)
        print (len(test[0]))
        print (len(test[1]))
        print (len(validation[0]))
        print (len(validation[1]))
        assert len(test) == 2
        assert len(test[0]) == 0
        assert len(test[1]) == 0
        assert len(validation) == 2
        assert len(validation[0]) > 0
        assert len(validation[1]) > 0
        print ("Now we test a stratified set...")
        test, validation = get_validation_set(inputs, targets, validation_size=0.5, binary_column=0)
        print (len(test[0]))
        print (len(test[1]))
        print (len(validation[0]))
        print (len(validation[1]))
        assert len(test) == 2
        assert len(test[0]) > 0
        assert len(test[1]) > 0
        assert len(validation) == 2
        assert len(validation[0]) > 0
        assert len(validation[1]) > 0
        print ("Test with no targets, the no inputs")
        inputs, targets = parse_file(filename, ignorecols=[0, 1, 4], ignorerows=[])
        assert (targets.size) == 0
        assert (inputs.size) > 0
        inputs, targets = parse_file(filename, targetcols=3, ignorecols=[0, 1, 2, 4, 5, 6, 7, 8, 9], ignorerows=[])
        assert (targets.size) > 0
        assert (inputs.size) == 0