Exemplo n.º 1
0
class HopfieldNetworkOperationTests(unittest.TestCase):
    def setUp(self):
        self.net = HopfieldNetwork(3)

        self.input_patterns = np.array([[1, -1, 1],
                                        [-1, 1, -1]])

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

        self.net.set_weights(weights)

    def test_calculate_neuron_output(self):
        neuron_output = self.net.calculate_neuron_output(0, self.input_patterns[0])

        expected_neuron_output = 1.0

        self.assertAlmostEqual(neuron_output, expected_neuron_output, 3)

        neuron_output = self.net.calculate_neuron_output(1, self.input_patterns[0])

        expected_neuron_output = -1.0

        self.assertAlmostEqual(neuron_output, expected_neuron_output, 3)

        neuron_output = self.net.calculate_neuron_output(2, self.input_patterns[0])

        expected_neuron_output = 1.0

        self.assertAlmostEqual(neuron_output, expected_neuron_output, 3)
Exemplo n.º 2
0
    def setUp(self):
        self.net = HopfieldNetwork(3)

        self.input_patterns = np.array([[1, -1, 1],
                                        [-1, 1, -1]])

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

        self.net.set_weights(weights)
Exemplo n.º 3
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")
Exemplo n.º 4
0
def run_flip_analysis(max_bits):
    input_patterns = [data[cat]['category_vector'] for cat in data.keys()]
    # initialize the network
    network = HopfieldNetwork(1200)
    # train the network
    hebbian_training(network, input_patterns)

    results = []
    for num_flip in range(max_bits):
        correct = train_and_flip(data, network, num_flip)
        results.append((num_flip, correct))
    return results
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.º 6
0
def train_and_evaluate(data, vector_type):
    input_patterns = [data[cat][vector_type] for cat in data.keys()]
    # initialize the network
    network = HopfieldNetwork(1200)
    # train the network
    hebbian_training(network, input_patterns)

    results = []
    for i, cat in enumerate(data.keys()):
        cat_data = data[cat]
        hyp_vecs = cat_data['hyponym_vectors'].values()
        num_vecs = len(hyp_vecs)
        correct = 0
        mistakes = []
        for pattern in hyp_vecs:
            output = network.run(pattern)
            idx = find_closest(output, input_patterns)
            if i == idx: correct += 1
            else: mistakes.append(mapping[idx])
        mistakes = dict(Counter(mistakes))
        results.append({'correct': correct, 'num_vecs': num_vecs,
            'mistakes': mistakes, 'category': cat, 'vector_type': vector_type})
    return results
Exemplo n.º 7
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.º 8
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.º 9
0
    def setUp(self):
        self.input_patterns = np.array([[1, -1, 1], [-1, 1, -1]])

        self.net = HopfieldNetwork(3)
Exemplo n.º 10
0
e_pattern *= 2
e_pattern -= 1

s_pattern *= 2
s_pattern -= 1

input_patterns = np.array([
    j_pattern.flatten(),
    a_pattern.flatten(),
    m_pattern.flatten(),
    e_pattern.flatten(),
    s_pattern.flatten()
])

# first creating the network and then train it with hebbian
network = HopfieldNetwork(35)

hebbian_training(network, input_patterns)

# Create the test patterns by using the training patterns and adding some noise to them
# and use the neural network to denoise them
j_test = j_pattern.flatten()

for i in range(4):
    p = randint(0, 34)
    j_test[p] *= -1

j_result = network.run(j_test)

j_result.shape = (7, 5)
j_test.shape = (7, 5)
Exemplo n.º 11
0
    def setUp(self):
        self.input_patterns = np.array([[1, -1, 1],
                                        [-1, 1, -1]])

        self.net = HopfieldNetwork(3)
Exemplo n.º 12
0
u_pattern *= 2
u_pattern -= 1

t_pattern *= 2
t_pattern -= 1

s_pattern *= 2
s_pattern -= 1

input_patterns = np.array([a_pattern.flatten(),
                           u_pattern.flatten(), 
                           t_pattern.flatten(),
                           s_pattern.flatten()])

# Create the neural network and train it using the training patterns
network = HopfieldNetwork(35)

hebbian_training(network, input_patterns)

# Create the test patterns by using the training patterns and adding some noise to them
# and use the neural network to denoise them
a_test = a_pattern.flatten()

for i in range(2):
    p = randint(0, 34)
    a_test[p] *= -1

a_result = network.run(a_test)

a_result.shape = (7, 5)
a_test.shape = (7, 5)
Exemplo n.º 13
0
 def setUp(self):
     self.net = HopfieldNetwork(10)
Exemplo n.º 14
0
 def setUp(self):
     self.net = HopfieldNetwork(10)
Exemplo n.º 15
0
Arquivo: hopfield.py Projeto: Kaaml/ai
                      [1, 0, 0, 0, 0],
                      [1, 1, 1, 1, 1]])
 
a_pattern *= 2
a_pattern -= 1
 
b_pattern *= 2
b_pattern -= 1
 
c_pattern *= 2
c_pattern -= 1
 
input_patterns = np.array([a_pattern.flatten(), b_pattern.flatten(), c_pattern.flatten()])
 
#Create the neural network and train it using the training patterns
network = HopfieldNetwork(35)
 
hebbian_training(network, input_patterns)
 
#Create the test patterns by using the training patterns and adding some noise to them
#and use the neural network to denoise them 
a_test =  a_pattern.flatten()
 
for i in range(4):
    p = randint(0, 34)
    a_test[p] *= -1
     
a_result = network.run(a_test)
 
a_result.shape = (7, 5)
a_test.shape = (7, 5)