def test_four_neurons_for_not(self, mocked_print): x_test = np.array([[1], [0]]) y_test = np.array([[0], [1]]) net = NeuralNet(1, 3, 2) net.train(x_test, y_test) sys.stderr.write(f'\n\nFour neuron net predicting for NOT:\n') for test, expected in zip([[1], [0], [1], [0]], [0, 1, 0, 1]): test_result = net.predict(test) sys.stderr.write( f'Input: {test}, Expected: {expected}, Prediction: {test_result}\n' ) self.assertTrue(np.abs(test_result - expected) < 0.1)
def test_three_neurons_for_equals(self, mocked_print): x_test = np.array([[1], [0]]) y_test = np.array([[1], [0]]) net = NeuralNet(1, 3, 1) net.train(x_test, y_test) sys.stderr.write(f'\n\nThree neuron net predicting for EQUALS:\n') for test, expected in zip([[1], [0], [1], [0]], [1, 0, 1, 0]): test_result = net.predict(test) sys.stderr.write( f'Input: {test}, Expected: {round(expected)}, Prediction: {test_result}\n' ) self.assertTrue(np.abs(test_result - expected) < 0.1)
def test_seven_neurons_for_mod2(self, mocked_print): x_test = np.array([[1, 0], [0, 0], [0, 1], [1, 1]]) y_test = np.array([[1], [0], [1], [0]]) net = NeuralNet(2, 3, 4) net.train(x_test, y_test, 20000) sys.stderr.write(f'\n\nSeven neuron net predicting for MOD 2:\n') for test, expected in zip([[1, 0], [0, 1], [1, 1], [0, 0]], [1, 1, 0, 0]): test_result = net.predict(test) sys.stderr.write( f'Input: {test}, Expected: {expected}, Prediction: {test_result}\n' ) self.assertTrue(np.abs(test_result - expected) < 0.1)
def test_run_net(self, mocked_print): number_of_input_neurons = 54 nodes_per_layer = 50 number_of_layers = 3 number_of_output_neurons = 27 number_of_epochs = 1 learning_rate = .3 word_file = open('../words.txt', 'r') words = [] for w in word_file.readlines(): words.append(w) word_file.close() sys.stderr.write(f'Input of {len(words)} words.') converter = WordConverter(words) x_test, y_test = converter.get_input() net = NeuralNet(number_of_input_neurons=number_of_input_neurons, number_of_layers=number_of_layers, number_of_neurons_per_layer=nodes_per_layer, number_of_output_neurons=number_of_output_neurons, activation_function=ActivationFunction.LOG, learning_rate=learning_rate) start_time = time.time() sys.stderr.write(f'\nTraining {nodes_per_layer} nodes per layer with ' f'{number_of_layers - 2 } hidden layers for ' f'{number_of_epochs} times.') net.train(np.asarray(x_test), np.asarray(y_test), number_of_epochs) sys.stderr.write( f'\nTraining took {time.time() - start_time} seconds.\n') test_input = x_test[:50] test_output = y_test[:50] for test, expected in zip(test_input, test_output): test_result = net.predict(test) letter_1 = converter.letter_dict(list(test[:27]).index(1)) letter_2 = converter.letter_dict(list(test[27:]).index(1)) sys.stderr.write( f'Input: {letter_1+letter_2}, ' f'Expected: {converter.letter_dict(list(expected).index(1))}, ' f'Prediction: {converter.letter_dict(test_result)}\n') self.assertTrue(True)
def test_three_inputs_three_outputs_and_three_hidden_for_mod2( self, mocked_print): x_test = np.array([[1, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0], [1, 0, 0]]) y_test = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0]]) net = NeuralNet(3, 3, 3, 3, activation_function=ActivationFunction.LOG) sys.stderr.write(f'\n\n------------------TRAINING------------------') net.train(x_test, y_test, 100000) sys.stderr.write( f'\n\nNine neuron net (3 input, 3 hidden, 3 output) predicting for MOD 2:\n' ) test_input = [[1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1]] # test_output = [[1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1]] test_output = [0, 1, 1, 2] for test, expected in zip(test_input, test_output): test_result = net.predict(test) sys.stderr.write( f'Input: {test}, Expected: {expected}, Prediction: {test_result}\n' ) self.assertTrue(np.abs(test_result - expected) < 0.1)
def test_run_net(self, mocked_print): for number_of_nodes_per_layer in [6, 9, 15]: for number_of_layers in range(3, 5): for number_of_epochs in [50000, 100000]: x_test = np.array([[1, 0, 0, 1, 0, 0], [1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 1], [0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 1, 0, 0, 1]]) y_test = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]]) net = NeuralNet(6, number_of_layers, number_of_nodes_per_layer, 3, activation_function=ActivationFunction.LOG) start_time = time.time() sys.stderr.write( f'\nTraining {number_of_nodes_per_layer} nodes per layer with ' f'{number_of_layers - 2 } hidden layers for ' f'{number_of_epochs} times.') net.train(x_test, y_test, number_of_epochs) sys.stderr.write( f'\nTraining took {time.time() - start_time} seconds.\n' ) test_input = [[1, 0, 0, 1, 0, 0], [1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 1], [0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 1, 0], [0, 0, 1, 0, 0, 1]] test_output = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]] for test, expected in zip(test_input, test_output): test_result = net.predict(test) sys.stderr.write( f'Input: {test}, Expected: {expected}, Prediction: {test_result}\n' ) self.assertTrue(True)