def test_learn_xnor_operations(self): sgd = Sgd(sizes=[2, 3, 1]) print('Training...') for _ in range(500): sgd.update_network(input=numpy.array([[0], [0]]), expected_output=numpy.array([[1]]), mini_batch=2, eta=2.0) sgd.update_network(input=numpy.array([[0], [1]]), expected_output=numpy.array([[0]]), mini_batch=2, eta=2.0) sgd.update_network(input=numpy.array([[1], [0]]), expected_output=numpy.array([[0]]), mini_batch=2, eta=2.0) sgd.update_network(input=numpy.array([[1], [1]]), expected_output=numpy.array([[1]]), mini_batch=2, eta=2.0) print('Testing...') output1: List[float] = sgd.feed_forward(input=numpy.array([[0], [0]])) self.assertEqual(round(output1[0][0]), 1.0) output4: List[float] = sgd.feed_forward(input=numpy.array([[0], [1]])) self.assertEqual(round(output4[0][0]), 0.0) output3: List[float] = sgd.feed_forward(input=numpy.array([[1], [0]])) self.assertEqual(round(output3[0][0]), 0.0) output2: List[float] = sgd.feed_forward(input=numpy.array([[1], [1]])) self.assertEqual(round(output2[0][0]), 1.0)
def test_learn_handwriting(self): sgd = Sgd(sizes=[Mnist.IMAGE_SIZE * Mnist.IMAGE_SIZE, 30, 10]) mnist_datas = Mnist.retrieve_mnist_datas( filename='../../classes/mnist/mnist.raw') print('Training...') for mnist_data in mnist_datas[:50000]: image_bytes = mnist_data['image_bytes'] input = numpy.frombuffer(buffer=image_bytes, dtype='uint8').reshape( (len(image_bytes), 1)) normalized_input = input / 255 number = mnist_data['number'] expected_output = numpy.zeros(shape=(10, 1)) expected_output[number] = 1.0 sgd.update_network(input=normalized_input, expected_output=expected_output, mini_batch=10, eta=4.0) print('Testing...') correct = 0 total = 0 for mnist_data in mnist_datas[50000:]: image_bytes = mnist_data['image_bytes'] input = numpy.frombuffer(buffer=image_bytes, dtype='uint8').reshape( (len(image_bytes), 1)) normalized_input = input / 255 number = mnist_data['number'] output: List[float] = sgd.feed_forward(input=normalized_input) output_prob: List[float] = sgd.softmax(z=output) output_index = numpy.argmax(output_prob) if output_index == number: correct += 1 total += 1 percent_correct = 100 * correct / total print( f'Correct {correct} out of {total} ({percent_correct:.1f}% correct)' ) self.assertGreater(percent_correct, 80)
def test_softmax_02(self): z = numpy.array([2.0, 1.0, 0.0, -1.0, -2.0]) output = Sgd.softmax(z).tolist() self.assertEqual(output, [0.6364086465588308, 0.23412165725273662, 0.0861285444362687, 0.03168492079612427, 0.011656230956039609]) self.assertAlmostEqual(sum(output), 1.0)
def test_softmax_01(self): z = numpy.array([2.0, 1.0, 0.0]) output = Sgd.softmax(z).tolist() self.assertEqual(output, [0.6652409557748219, 0.24472847105479764, 0.09003057317038046]) self.assertAlmostEqual(sum(output), 1.0)
def test_sigmoid_prime(self): z = numpy.array([-1.0, 0.0, 1.0]) output = Sgd.sigmoid_prime(z).tolist() self.assertEqual(output, [0.19661193324148185, 0.25, 0.19661193324148185])
def test_sigmoid_prime_f(self): self.assertAlmostEqual(Sgd.sigmoid_prime_f(-1.0), 0.19661193324148185) self.assertAlmostEqual(Sgd.sigmoid_prime_f(0.0), 0.25) self.assertAlmostEqual(Sgd.sigmoid_prime_f(1.0), 0.19661193324148185)
def test_sigmoid(self): z = numpy.array([-1.0, 0.0, 1.0]) output = Sgd.sigmoid(z).tolist() self.assertEqual(output, [0.2689414213699951, 0.5, 0.7310585786300049])
def test_sigmoid_f(self): self.assertAlmostEqual(Sgd.sigmoid_f(-1.0), 0.2689414213699951) self.assertAlmostEqual(Sgd.sigmoid_f(0.0), 0.5) self.assertAlmostEqual(Sgd.sigmoid_f(1.0), 0.7310585786300049)