def testXCommitteeImprovement(self):
        P, T = loadsyn3(100)
        p, t = P, T

        epochs = 10

        come = build_feedforward_committee(size = 3, input_number = 2, hidden_number = 6, output_number = 1)

        Y = come.sim(p)
        [num_correct_first, num_correct_second, initial_performance, num_first, num_second, missed] = stat(Y, t) #@UnusedVariable

        train_committee(come, train_evolutionary, p, t, epochs = epochs, error_function = sumsquare_total)

        Y = come.sim(p)
        [num_correct_first, num_correct_second, genetic_performance, num_first, num_second, missed] = stat(Y, t) #@UnusedVariable

        print(initial_performance, genetic_performance)
        assert(initial_performance < genetic_performance)

        comg = build_feedforward_committee(size = 3, input_number = 2, hidden_number = 6, output_number = 1)
        epochs = 100
        Y = comg.sim(p)
        [num_correct_first, num_correct_second, initial_performance, num_first, num_second, missed] = stat(Y, t) #@UnusedVariable

        train_committee(comg, traingd, p, t, epochs = epochs, block_size = 10, error_function = sumsquare_total)

        Y = comg.sim(p)
        [num_correct_first, num_correct_second, gd_performance, num_first, num_second, missed] = stat(Y, t) #@UnusedVariable

        print(initial_performance, genetic_performance, gd_performance)
        assert(initial_performance < gd_performance)
Beispiel #2
0
    def test1Simple(self):
        net = build_feedforward(input_number = 2, hidden_number = 3, output_number = 1)

        results = net.update([1, 2])
        print(results)

        results = net.sim([[1, 2], [2, 3]])
        print(results)

        com = build_feedforward_committee(input_number = 2, hidden_number = 3, output_number = 1)

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

        results = com.sim([[1, 2], [2, 3]])
        print(results)
        print("Simple done")
Beispiel #3
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