コード例 #1
0
    def test_adjust_weight(self):
        backpropagator = Backpropagator()
        output = ReceiveAllNeuron()
        hidden_1 = ReceiveAllNeuron()
        hidden_2 = ReceiveAllNeuron()
        
        hidden_1.connect_to(output)
        hidden_1.out_connections_list[0].weight = -0.75
        
        hidden_2.connect_to(output)
        hidden_2.out_connections_list[0].weight = -0.25

        hidden_1.receive_signal(0.434719)
        hidden_2.receive_signal(0.434719)

        output.error = 0.09754925
        hidden_1.error = -0.01745285
        hidden_2.error = -0.00581762

        connection_1 = hidden_1.out_connections_list[0]
        backpropagator.adjust_weight(connection_1, 1.1)
        # -0.75 + (1.1 * 0.09754925 * -0.45525)
        self.assertAlmostEqual(connection_1.weight, -0.68486637)

        connection_2 = hidden_2.out_connections_list[0]
        backpropagator.adjust_weight(connection_2, 1.1)
        # -0.25 + (1.1 * 0.09754925 * -0.15175)
        self.assertAlmostEqual(connection_2.weight, -0.18486637)
コード例 #2
0
    def test_hiddenError(self):
        backpropagator = Backpropagator()
        output = ReceiveAllNeuron()
        hidden_1 = ReceiveAllNeuron()
        hidden_2 = ReceiveAllNeuron()
        
        hidden_1.connect_to(output)
        hidden_1.out_connections_list[0].weight = -0.75
        
        hidden_2.connect_to(output)
        hidden_2.out_connections_list[0].weight = -0.25

        # sigmoid(0.434719) == 0.6070000
        hidden_1.receive_signal(0.434719)
        hidden_2.receive_signal(0.434719)

        # sigmoid(0.434719) * -0.75 + sigmoid(0.434719) * -0.25 == -0.607
        # sigmoid(-0.607) == 0.352744
        # 0.78 - 0.352744 == 0.3527438
        output.error = backpropagator.output_error(output, 0.78)
        self.assertAlmostEqual(output.error, 0.09754925)

        hidden_1.error = backpropagator.hidden_error(hidden_1)
        hidden_2.error = backpropagator.hidden_error(hidden_2)

        # 0.6070000 * (1 - 0.6070000)*(-0.75 * 0.09754925)
        self.assertAlmostEqual(hidden_1.error, -0.01745285)

        # 0.6070000 * (1 - 0.6070000)*(-0.25 * 0.09754925)
        self.assertAlmostEqual(hidden_2.error, -0.00581762)
コード例 #3
0
    def test_connection(self):
        sender = ReceiveAllNeuron(self.simple_activation())
        receiver = ReceiveAllNeuron(self.simple_activation())
        connection = sender.connect_to(receiver)

        self.assertEqual(connection.sender, sender)
        self.assertEqual(connection.receiver, receiver)
        self.assertEqual(connection.weight, 1.0)
        self.assertEqual(connection.signal_sent, 0.0)
        self.assertEqual(connection.signal_received, 0.0)

        connection.weight = 0.5
        sender.receive_signal(7.4)

        self.assertEqual(connection.weight, 0.5)
        self.assertEqual(connection.signal_sent, 3.7)
        self.assertEqual(connection.signal_received, 7.4)
コード例 #4
0
    def test_wait_for_all_signals(self):
        sender_1 = ReceiveAllNeuron(self.simple_activation())
        sender_2 = ReceiveAllNeuron(self.simple_activation())
        receiver = ReceiveAllNeuron(self.simple_activation())

        sender_1.connect_to(receiver)
        sender_2.connect_to(receiver)

        sender_1.receive_signal(2.0)
        self.assertEqual(receiver.output, 0.0)
        self.assertFalse(receiver.allSignalsReceived())
        
        sender_2.receive_signal(3.0)
        self.assertEqual(receiver.output, 5.0)
        self.assertTrue(receiver.allSignalsReceived())

        sender_1.reset()
        sender_2.reset()
        receiver.reset()

        self.assertEqual(sender_1.output, 0.0)
        self.assertEqual(sender_2.output, 0.0)
        self.assertEqual(receiver.output, 0.0)
        self.assertFalse(receiver.allSignalsReceived())
コード例 #5
0
 def test_sigmoid_activation(self):
     neuron = ReceiveAllNeuron()        
     neuron.receive_signal(-0.607)
     self.assertAlmostEqual(neuron.output, 0.3527438)
コード例 #6
0
 def test_outputError(self):
     backpropagator = Backpropagator()
     neuron = ReceiveAllNeuron()
     neuron.receive_signal(-0.607)        
     error = backpropagator.output_error(neuron, 0.78)
     self.assertAlmostEqual(error, 0.09754925)