Exemplo n.º 1
0
class HopfieldNetworkCreationTests(unittest.TestCase):

    def setUp(self):
        self.net = HopfieldNetwork(10)

    def test_change_network_weights(self):
        new_weights = np.ones((10, 10))

        self.net.set_weights(new_weights)

        self.assertTrue(np.array_equal(self.net.get_weights(), new_weights), "The network weights have not been updated")

    def test_fail_to_change_weights_when_shape_not_same_as_input_vector(self):
        new_weights = np.ones((5, 5))

        self.assertRaises(InvalidWeightsException, self.net.set_weights, new_weights)

    def test_network_creation(self):
        self.assertEqual(self.net.get_weights().shape, (10, 10), "The networks weight array has wrong shape")
class HopfieldNetworkCreationTests(unittest.TestCase):
    def setUp(self):
        self.net = HopfieldNetwork(10)

    def test_change_network_weights(self):
        new_weights = np.ones((10, 10))

        self.net.set_weights(new_weights)

        self.assertTrue(np.array_equal(self.net.get_weights(), new_weights),
                        "The network weights have not been updated")

    def test_fail_to_change_weights_when_shape_not_same_as_input_vector(self):
        new_weights = np.ones((5, 5))

        self.assertRaises(InvalidWeightsException, self.net.set_weights,
                          new_weights)

    def test_network_creation(self):
        self.assertEqual(self.net.get_weights().shape, (10, 10),
                         "The networks weight array has wrong shape")
Exemplo n.º 3
0
class HebbianTrainingTest(unittest.TestCase):
    def setUp(self):
        self.input_patterns = np.array([[1, -1, 1],
                                        [-1, 1, -1]])

        self.net = HopfieldNetwork(3)

    def test_calculate_weight(self):
        w = calculate_weight(0, 1, self.input_patterns)

        expected_weight = -1.0

        self.assertAlmostEqual(w, expected_weight)

        w = calculate_weight(1, 2, self.input_patterns)

        expected_weight = -1.0

        self.assertAlmostEqual(w, expected_weight)

        w = calculate_weight(0, 2, self.input_patterns)

        expected_weight = 1.0

        self.assertAlmostEqual(w, expected_weight)


    def test_calculate_neuron_weights(self):
        w = calculate_neuron_weights(0, self.input_patterns)

        expected_weights = np.array([0.0, -1.0, 1.0])

        self.assertTrue(np.array_equal(w, expected_weights))

    def test_hebbian_training(self):
        hebbian_training(self.net, self.input_patterns)

        expected_weights = np.array([[0, -1, 1],
                                     [-1, 0, -1],
                                     [1, -1, 0]])

        weights = self.net.get_weights()

        self.assertTrue(np.array_equal(weights, expected_weights), "Test weights not equal")
Exemplo n.º 4
0
class HebbianTrainingTest(unittest.TestCase):
    def setUp(self):
        self.input_patterns = np.array([[1, -1, 1], [-1, 1, -1]])

        self.net = HopfieldNetwork(3)

    def test_calculate_weight(self):
        w = calculate_weight(0, 1, self.input_patterns)

        expected_weight = -1.0

        self.assertAlmostEqual(w, expected_weight)

        w = calculate_weight(1, 2, self.input_patterns)

        expected_weight = -1.0

        self.assertAlmostEqual(w, expected_weight)

        w = calculate_weight(0, 2, self.input_patterns)

        expected_weight = 1.0

        self.assertAlmostEqual(w, expected_weight)

    def test_calculate_neuron_weights(self):
        w = calculate_neuron_weights(0, self.input_patterns)

        expected_weights = np.array([0.0, -1.0, 1.0])

        self.assertTrue(np.array_equal(w, expected_weights))

    def test_hebbian_training(self):
        hebbian_training(self.net, self.input_patterns)

        expected_weights = np.array([[0, -1, 1], [-1, 0, -1], [1, -1, 0]])

        weights = self.net.get_weights()

        self.assertTrue(np.array_equal(weights, expected_weights),
                        "Test weights not equal")