Exemple #1
0
 def test_change_weights_for_example0b(self):
     activation_functions = [ sigmoid_function ]
     weight_init_functions = [ self.weight_init_function_test1 ]
     learning_rate_functions = [learning_rate_function]
     network = self.setup1(activation_functions, weight_init_functions, learning_rate_functions)
     single_training_example_inputs = [1.0, 1.0]  # includes Bias!
     network_outputs = network.calc_networks_output( single_training_example_inputs )        
     
     expected_network_output = sigmoid_function(5.0)
     assert network_outputs == [expected_network_output]
     
     errors = network.calc_output_neurons_errors(network_outputs, [5.5])
     expected_output_error = expected_network_output - 5.5
     assert errors == [expected_output_error]
     output_neurons_error = network.layers[-1].neurons[0].error
     assert output_neurons_error == expected_output_error
     
     output_neurons_error_at_input = network.layers[-1].neurons[0].error_at_input
     output_neurons_netinput = network.layers[-1].neurons[0].netinput
     assert output_neurons_error_at_input == output_neurons_error * sigmoid_function(output_neurons_netinput, True)
     
     print network.layers[0]
                   
     network.change_weights_for_example( single_training_example_inputs )
     assert network.layers[0].neurons[0].weights[0] == 2.0 + (output_neurons_error_at_input * 1.0)
     assert network.layers[0].neurons[0].weights[1] == 3.0 + (output_neurons_error_at_input * 1.0)
Exemple #2
0
    def test_change_weights_for_example0b(self):
        activation_functions = [sigmoid_function]
        weight_init_functions = [self.weight_init_function_test1]
        learning_rate_functions = [learning_rate_function]
        network = self.setup1(activation_functions, weight_init_functions,
                              learning_rate_functions)
        single_training_example_inputs = [1.0, 1.0]  # includes Bias!
        network_outputs = network.calc_networks_output(
            single_training_example_inputs)

        expected_network_output = sigmoid_function(5.0)
        assert network_outputs == [expected_network_output]

        errors = network.calc_output_neurons_errors(network_outputs, [5.5])
        expected_output_error = expected_network_output - 5.5
        assert errors == [expected_output_error]
        output_neurons_error = network.layers[-1].neurons[0].error
        assert output_neurons_error == expected_output_error

        output_neurons_error_at_input = network.layers[-1].neurons[
            0].error_at_input
        output_neurons_netinput = network.layers[-1].neurons[0].netinput
        assert output_neurons_error_at_input == output_neurons_error * sigmoid_function(
            output_neurons_netinput, True)

        print network.layers[0]

        network.change_weights_for_example(single_training_example_inputs)
        assert network.layers[0].neurons[0].weights[0] == 2.0 + (
            output_neurons_error_at_input * 1.0)
        assert network.layers[0].neurons[0].weights[1] == 3.0 + (
            output_neurons_error_at_input * 1.0)
Exemple #3
0
    def test_calc_hidden_neurons_errors4(self):
        activation_functions = [sigmoid_function, sigmoid_function]
        weight_init_functions = [
            self.weight_init_function_test2, self.weight_init_function_test2
        ]
        learning_rate_functions = [
            learning_rate_function, learning_rate_function
        ]
        network = self.setup_single_hidden_layer_net2(activation_functions,
                                                      weight_init_functions,
                                                      learning_rate_functions)

        network_outputs = network.calc_networks_output([1.0, 1.0])
        hidden_neuron = network.layers[0].neurons[0]
        assert hidden_neuron.netinput == 3.0
        expected_hiddens_output = sigmoid_function(3.0)
        assert hidden_neuron.output == sigmoid_function(3.0)
        output_neuron = network.layers[1].neurons[0]
        expected_netinput_to_output_neuron = (2.0 *
                                              expected_hiddens_output) + 1.0
        assert output_neuron.netinput == expected_netinput_to_output_neuron
        assert network_outputs[0] == sigmoid_function(
            expected_netinput_to_output_neuron)

        network.calc_output_neurons_errors(network_outputs, [5.5])
        expected_output_error = sigmoid_function(
            expected_netinput_to_output_neuron) - 5.5
        assert output_neuron.error == expected_output_error
        assert output_neuron.error_at_input == (
            expected_output_error *
            sigmoid_function(expected_netinput_to_output_neuron, True))
Exemple #4
0
 def setup2(self):
     random.seed(1)
     aNeuron = Neuron(n_inputs=2, activation_function=sigmoid_function, weight_init_function=weight_init_function_random, learning_rate_function=learning_rate_function)
     aNeuron.weights = [1.0,0.5,0.25]
     inputs = [2.0,1.5,1.0]
     output = aNeuron.calc_neurons_output(inputs)
     expected = sigmoid_function( dot(inputs, aNeuron.weights) )
     return output, expected
Exemple #5
0
 def test_calc_hidden_neurons_errors4(self): 
     activation_functions = [ sigmoid_function, sigmoid_function ]     
     weight_init_functions = [ self.weight_init_function_test2, self.weight_init_function_test2 ]
     learning_rate_functions = [ learning_rate_function, learning_rate_function ]
     network = self.setup_single_hidden_layer_net2(activation_functions, weight_init_functions, learning_rate_functions)
     
     network_outputs = network.calc_networks_output( [1.0, 1.0] )
     hidden_neuron = network.layers[0].neurons[0]    
     assert hidden_neuron.netinput == 3.0
     expected_hiddens_output = sigmoid_function(3.0)
     assert hidden_neuron.output == sigmoid_function(3.0)
     output_neuron = network.layers[1].neurons[0]
     expected_netinput_to_output_neuron = (2.0 * expected_hiddens_output) + 1.0
     assert output_neuron.netinput == expected_netinput_to_output_neuron
     assert network_outputs[0] == sigmoid_function(expected_netinput_to_output_neuron)
     
     network.calc_output_neurons_errors(network_outputs, [5.5])
     expected_output_error = sigmoid_function(expected_netinput_to_output_neuron) - 5.5
     assert output_neuron.error == expected_output_error
     assert output_neuron.error_at_input == (expected_output_error * sigmoid_function( expected_netinput_to_output_neuron, True ))
Exemple #6
0
 def setup2(self):
     random.seed(1)
     aNeuron = Neuron(n_inputs=2,
                      activation_function=sigmoid_function,
                      weight_init_function=weight_init_function_random,
                      learning_rate_function=learning_rate_function)
     aNeuron.weights = [1.0, 0.5, 0.25]
     inputs = [2.0, 1.5, 1.0]
     output = aNeuron.calc_neurons_output(inputs)
     expected = sigmoid_function(dot(inputs, aNeuron.weights))
     return output, expected