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
]) # 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() 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)
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() for i in range(2): p = randint(0, 34) u_test[p] *= -1 u_result = network.run(u_test) u_result.shape = (7, 5) u_test.shape = (7, 5)
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) b_test = b_pattern.flatten() for i in range(4): p = randint(0, 34) b_test[p] *= -1 b_result = network.run(b_test) b_result.shape = (7, 5) b_test.shape = (7, 5)