def test_teach_acceptable_error(self):
        backpropagator = Backpropagator()
        normalizer = Normalizer(in_min = -15, in_max = 15,
                                out_min = -30, out_max = 30,
                                norm_min = -2, norm_max = 2)
        
        network = FeedForwardNN(normalizer, [1, 3, 1])
        
        network.randomize_connection_weights(seed = 74)

        expectations = [Expectation([i], [2*i])
                        for i in range(-5, 5)]

        result = backpropagator.teach(network,
                                      expectations, 
                                      learning_rate = 1.5,
                                      max_iterations = 2000,
                                      acceptable_error = .5)
        
        self.assertLessEqual(result.error, .5)

        errors = 0
        for exp in expectations:
            errors += backpropagator.calculate_error(
                network.receive_inputs(exp.inputs), exp.outputs)

        self.assertLessEqual(errors, .5)
    def test_learn(self):
        backpropagator = Backpropagator()
        normalizer = Normalizer(in_min = 1, in_max = 15,
                                out_min = -1, out_max = 1,
                                norm_min = -3, norm_max = 3)
        
        network = FeedForwardNN(normalizer, [1, 2, 2, 1])
        network.randomize_connection_weights(seed = 74)
        neurons = network.neurons        
        expectation = Expectation([1], [0.8415])

        error = backpropagator.calculate_error(
            network.receive_inputs(expectation.inputs),
            expectation.outputs)

        for i in range(20):
            last_error = error
            backpropagator.learn(network, expectation, 1.5)
            actual = network.receive_inputs(expectation.inputs)
            print(actual)
            error = backpropagator.calculate_error(actual, expectation.outputs)
            self.assertLess(error, last_error)
 def test_calculate_error(self):
     backpropagator = Backpropagator()
     error = backpropagator.calculate_error([5.5, 1, -8], [5, -4, 2.5])
     self.assertAlmostEqual(error, 135.5)