コード例 #1
0
ファイル: test_sgd_mnist.py プロジェクト: mckev/ml
    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)
コード例 #2
0
 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)
コード例 #3
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)