def part_3_4(): """ Test hopfield network's robustness on different settings of noisy data :param p: dictionary of different patterns """ # Train with 3 patterns p_train = [p[0].flatten(), p[1].flatten(), p[2].flatten()] train_data = np.asarray(p_train) h_net = HopfieldNet(train_data) h_net.batch_train() # Choose a pattern and add noise to it test_pattern = 2 # Choose between 0, 1, 2 p_test = [p[test_pattern].flatten()] test_data = np.asarray(p_test) # Set noise percentages to test on [start, end, step] noise_percentages = np.arange(0, 101, 1) n_runs = 1 runs = [] for run in range(n_runs): acc = {} # Test for different percentages of noise for noise_perc in noise_percentages: # add noise to test data noisy_test_data = add_noise(test_data[0], noise_perc) # try to recall test_pred = h_net.recall([noisy_test_data], epochs=batch_epochs) acc[noise_perc] = calc_acc(test_data[0], test_pred[0]) if show_plots: test_pred_1 = test_pred[0].reshape(32, 32) # prepare for plotting show_tested(noisy_test_data, test_pred_1, test_pred_1.shape[0], test_pred_1.shape[1], title="Testing with " + str(noise_perc) + "% noise") # plot_accuracy(acc) runs.append(acc) average_acc = {} for noise_perc in acc.keys(): av_acc_i = 0. for run in range(n_runs): av_acc_i += runs[run][noise_perc] average_acc[noise_perc] = av_acc_i / float(n_runs) plot_accuracy(average_acc)
def main(): """ Test for the hopfield network """ """ This is just for testing the functions """ x1 = np.array([1, 1, 1, 1, -1, -1, 1, 1, 1]) x2 = np.array([1, -1, 1, 1, 1, 1, 1, -1, 1]) x3 = np.array([-1, 1, -1, -1, 1, -1, -1, 1, -1]) train_set = np.vstack((x1, x2)) train_set = np.vstack((train_set, x3)) params = { "epochs": 100, "neurons": len(x1), "learn_method": 'classic' } hop = hop_net.HopfieldNet(train_set, **params) hop.batch_train() show_trained(train_set) x4d = [1,1,1,1,1,1,1,1,1] x5d = [1,1,1,1,-1,-1,1,-1,-1] x45d = np.vstack((x4d, x5d)) test_set = np.vstack((x45d, train_set)) recalled_set = hop.recall(test_set) for i in range(test_set.shape[0]): show_tested(test_set[i], recalled_set[i])
def main(): """ main function""" x1 = np.array([-1, -1, 1, -1, 1, -1, -1, 1]) x2 = np.array([-1, -1, -1, -1, -1, 1, -1, -1]) x3 = np.array([-1, 1, 1, -1, -1, 1, -1, 1]) train_set = np.vstack((x1, x2, x3)) hop = hop_net.HopfieldNet(train_set) hop.batch_train() show_trained(train_set, 4, 2) x1d = np.array([1, -1, 1, -1, 1, -1, -1, 1]) x2d = np.array([1, 1, -1, -1, -1, 1, -1, -1]) x3d = np.array([1, 1, 1, -1, 1, 1, -1, 1]) test_set = np.vstack((train_set, x1d, x2d, x3d)) recalled_set = hop.recall(test_set, epochs=15) for i in range(test_set.shape[0]): show_tested(test_set[i], recalled_set[i], 4, 2)
def test_recovery(train_data, method): """ Test recovery abilities :param train_data: :param method: :return: """ h_net = HopfieldNet(train_data) h_net.batch_train() p_test = [p[9].flatten(), p[10].flatten()] test_data = np.asarray(p_test) if method == 'batch': print("Testing recovery with batch") test_pred = h_net.recall(test_data, epochs=batch_epochs) else: print("Testing recovery with sequential") test_pred, _ = h_net.sequential_recall(test_data, epochs=seq_epochs, plot_at_100=plot_at_100) test_pred_10 = test_pred[0].reshape(32, 32) # prepare for plotting test_pred_11 = test_pred[1].reshape(32, 32) # prepare for plotting show_tested(p_test[0], test_pred_10, test_pred_10.shape[0], test_pred_10.shape[1], title="Testing recovery abilities with " + method) show_tested(p_test[1], test_pred_11, test_pred_11.shape[0], test_pred_11.shape[1], title="Testing recovery abilities with " + method)
def test_stability(train_data, method): """ Test that the patterns are stable :param train_data: :param method: :return: """ h_net = HopfieldNet(train_data) h_net.batch_train() test_data = train_data if method == 'batch': print("Testing stability with batch") test_pred = h_net.recall(test_data, epochs=batch_epochs) else: print("Testing stability with sequential") test_pred, _ = h_net.sequential_recall(test_data, epochs=seq_epochs, plot_at_100=plot_at_100) test_pred_1 = test_pred[0].reshape(32, 32) # prepare for plotting test_pred_2 = test_pred[1].reshape(32, 32) # prepare for plotting test_pred_3 = test_pred[2].reshape(32, 32) # prepare for plotting show_tested(test_data[0], test_pred_1, test_pred_1.shape[0], test_pred_1.shape[1], title="Testing stability with " + method) show_tested(test_data[1], test_pred_2, test_pred_2.shape[0], test_pred_2.shape[1], title="Testing stability with " + method) show_tested(test_data[2], test_pred_3, test_pred_3.shape[0], test_pred_3.shape[1], title="Testing stability with " + method)