Пример #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)
Пример #2
0
 def test3Bias(self):
     net = build_feedforward(input_number = 2, hidden_number = 2, output_number = 1, hidden_function = "linear", output_function = "linear")
     
     #Bias node should always give an output of 1
     bias = net.bias_node
     
     assert(1.0 == bias.output([1,1]))
     assert(1.0 == bias.output("Crazy string argument"))
     assert(1.0 ==  bias.output(1.0))
     assert(1.0 == bias.output(-1))
     assert(1.0 == bias.output(None))
     
     inputs = [1.0,1.0]
     
     #Set all weights to 1
     for node in net.hidden_nodes:
         for key, val in node.weights.iteritems():
             node.weights[key] = 1.0
     for key, val in net.output_nodes[0].weights.iteritems():
         net.output_nodes[0].weights[key] = 1.0
     
     one_bias = net.update(inputs)
     print("Bias weight is 1: {0}".format(one_bias))
     assert(one_bias == 7.0)
     
     #Set bias weights to 0
     for node in net.hidden_nodes:
         node.weights[bias] = 0.0
     net.output_nodes[0].weights[bias] = 0.0
     
     zero_bias = net.update(inputs)
     print("Bias weight is 0: {0}".format(zero_bias))
     
     assert(one_bias > zero_bias)
     assert(zero_bias == 4.0)
     
     #Set bias weights to -99
     for node in net.hidden_nodes:
         node.weights[bias] = -99.0
     net.output_nodes[0].weights[bias] = -99.0
     
     neg_bias = net.update(inputs)
     print("Bias weight is -99: {0}".format(neg_bias))
     
     assert(zero_bias > neg_bias)
     assert(neg_bias == ((1.0*2 - 99.0)*2 - 99.0))
Пример #3
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")
Пример #4
0
 def test6NetworkGDTrain(self):
     import numpy as np
     #Train on XOR
     indata = np.array([[0, 0],
               [0, 1],
               [1, 0],
               [1, 1]])
     outdata = np.array([[0],
                [1],
                [1],
                [0]])
     
     net = build_feedforward(2, 4, 1)
     
     net.learn(indata, outdata, epochs=500)
     
     print('network training xor results:')
     for result, target in zip(net.sim(indata), outdata):
         print(result[0], target[0])
         assert(round(result[0]) == target[0])
Пример #5
0
    def test2Multiplication(self):
        net = build_feedforward(input_number = 2, hidden_number = 3, output_number = 1)
        first_sum = 0
        for node in net.get_all_nodes():
            for weight in node.weights.values():
                first_sum += weight
        a = -11.0
        net = net * a
        second_sum = 0
        for node in net.get_all_nodes():
            for weight in node.weights.values():
                second_sum += weight

        net / a
        third_sum = 0
        for node in net.get_all_nodes():
            for weight in node.weights.values():
                third_sum += weight

        print(first_sum, second_sum, third_sum)
        assert(round(a * first_sum, 10) == round(second_sum, 10))
        assert(round(first_sum, 10) == round(second_sum / a, 10))
        assert(round(first_sum, 10) == round(third_sum, 10))
        print("mul done")
Пример #6
0
 def test5NormalNodesLinear(self):
     net = build_feedforward(input_number = 2, hidden_number = 2, output_number = 1, hidden_function = "linear", output_function = "linear")
     assert(5 == len(net))
     #Bias node should always give an output of 1
     #bias = net.bias_node
     inputs = [1.0, 1.0]
     
     #Set all weights to 1
     for node in net.hidden_nodes:
         for key, val in node.weights.iteritems():
             node.weights[key] = 1.0
     for key, val in net.output_nodes[0].weights.iteritems():
         net.output_nodes[0].weights[key] = 1.0
     
     one = net.update(inputs)
     print("Weights are 1: {0}".format(one))
     #Assert each individually
     for node in net.hidden_nodes:
         out_val = node.output(inputs)
         print("Node output is: {0}".format(out_val))
         assert(3.0 == out_val)
     #Each hidden should have an output of 3
     #So net output should be 2*3 + 1 = 7
     assert(one == 7.0)
Пример #7
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
Пример #8
0
                #node.weights[keynode] += uniform(-random_range, random_range)
                _node.weights[keynode] += choice([-1, 1]) * numpy.random.exponential(random_mean)

def train_evolutionary(net, (input_array, output_array), (validation_inputs, validation_targets), epochs = 1, population_size = 100, mutation_chance = 0.25, random_range = 1.0, random_mean = 0.2, top_number = 25, cross_over_chance = 0.5, error_function = sumsquare_total, loglevel = None, *args, **kwargs): #@UnusedVariable
    """Creates more networks and evolves the best it can.
    Does NOT use any validation set..."""
    if top_number > population_size:
        raise ValueError('Top_number({0}) can not be larger than population size({1})!'.format(top_number, population_size))

    if loglevel is not None:
        logging.basicConfig(level = loglevel)
    #Create a population of 50 networks
    best = None
    best_error = None
    best_val_error = None
    population = [build_feedforward(net.num_of_inputs, len(net.hidden_nodes), len(net.output_nodes)) for each in xrange(int(population_size))]
    #For each generation
    for generation in xrange(int(epochs)):
        try: # I want to catch keyboard interrupt if the user wants to cancel training
            error = {} #reset errors
            top_networks = [None for each in xrange(int(top_number))] #reset top five
            #For all networks, simulate, measure their error, and save the best network so far
            #sim_results = mp_nets_sim(population, input_array)
            sim_results = [net.sim(input_array) for net in population]
            for member, sim_result in zip(population, sim_results):
                error[member] = error_function(output_array, sim_result) / len(output_array)
                #compare with best
                if not best or error[member] < best_error:
                    best = member
                    best_error = error[member]
                    if validation_inputs is not None and len(validation_inputs) > 0: