Exemplo n.º 1
0
    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.º 2
0
    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.º 3
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
Exemplo n.º 4
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.º 5
0
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)

a_test = a_pattern.flatten()
Exemplo n.º 6
0
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)

u_test = u_pattern.flatten()