{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "import numpy as np\n", "from sklearn import datasets, model_selection\n", "from keras.models import Sequential\n", "from keras.layers import Dense\n", "from keras import optimizers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mnist = datasets.fetch_mldata('MNIST original', data_home='data/src/download')\n", "\n", "X = mnist.data / 255\n", "y = mnist.target\n", "Y = np.identity(10)[y.astype(int)]\n", "\n", "train_size = 60000\n", "test_size = 10000\n", "\n", "X_train, X_test, Y_train, Y_test = model_selection.train_test_split(\n", " X, Y, test_size=test_size, train_size=train_size)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "model = Sequential()\n", "model.add(Dense(units=10, input_dim=784, activation='softmax'))\n", "sgd = optimizers.SGD(lr=0.5)\n", "model.compile(\n", " optimizer=sgd,\n", " loss='categorical_crossentropy',\n", " metrics=['accuracy']\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "batch_size = 100\n", "epochs = 20" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/20\n", "60000/60000 [==============================] - 1s - loss: 0.3934 - acc: 0.8884 \n", "Epoch 2/20\n", "60000/60000 [==============================] - 1s - loss: 0.3018 - acc: 0.9145 \n", "Epoch 3/20\n", "60000/60000 [==============================] - 1s - loss: 0.2870 - acc: 0.9192 \n", "Epoch 4/20\n", "60000/60000 [==============================] - 1s - loss: 0.2793 - acc: 0.9212 \n", "Epoch 5/20\n", "60000/60000 [==============================] - 1s - loss: 0.2739 - acc: 0.9235 \n", "Epoch 6/20\n", "60000/60000 [==============================] - 1s - loss: 0.2699 - acc: 0.9242 \n", "Epoch 7/20\n", "60000/60000 [==============================] - 1s - loss: 0.2666 - acc: 0.9252 \n", "Epoch 8/20\n", "60000/60000 [==============================] - 1s - loss: 0.2645 - acc: 0.9251 \n", "Epoch 9/20\n", "60000/60000 [==============================] - 1s - loss: 0.2621 - acc: 0.9269 \n", "Epoch 10/20\n", "60000/60000 [==============================] - 1s - loss: 0.2601 - acc: 0.9280 \n", "Epoch 11/20\n", "60000/60000 [==============================] - 1s - loss: 0.2588 - acc: 0.9274 \n", "Epoch 12/20\n", "60000/60000 [==============================] - 1s - loss: 0.2586 - acc: 0.9277 \n", "Epoch 13/20\n", "60000/60000 [==============================] - 1s - loss: 0.2558 - acc: 0.9285 \n", "Epoch 14/20\n", "60000/60000 [==============================] - 1s - loss: 0.2547 - acc: 0.9287 \n", "Epoch 15/20\n", "60000/60000 [==============================] - 1s - loss: 0.2537 - acc: 0.9297 \n", "Epoch 16/20\n", "60000/60000 [==============================] - 1s - loss: 0.2526 - acc: 0.9288 \n", "Epoch 17/20\n", "60000/60000 [==============================] - 1s - loss: 0.2524 - acc: 0.9287 \n", "Epoch 18/20\n", "60000/60000 [==============================] - 1s - loss: 0.2515 - acc: 0.9298 \n", "Epoch 19/20\n", "60000/60000 [==============================] - 1s - loss: 0.2499 - acc: 0.9303 \n", "Epoch 20/20\n", "60000/60000 [==============================] - 1s - loss: 0.2499 - acc: 0.9302 \n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000/10000 [==============================] - 0s \n", "[0.30260467473864555, 0.91790000000000005]\n" ] } ], "source": [ "score = model.evaluate(X_test, Y_test)\n", "print(score)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }