{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "from keras.layers.core import Dense, Activation\n", "from keras.models import Sequential\n", "from keras.optimizers import Adam\n", "from sklearn import datasets, preprocessing\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# MNIST - Keras (Backend: TensorFlow)\n", "\n", "以下のTensorFlowのTutorialと見比べてみて下さい \n", "http://www.tensorflow.org/tutorials/mnist/beginners/index.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MNIST Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# データセットの用意\n", "mnist = datasets.fetch_mldata('MNIST original')\n", "\n", "X = mnist.data.astype(float)\n", "X /= 255\n", "\n", "y = preprocessing.LabelBinarizer().fit_transform(mnist.target)\n", "\n", "# 訓練用とテスト用にデータを分割\n", "train_size = 60000\n", "\n", "X_train, X_test = np.split(X, [train_size])\n", "y_train, y_test = np.split(y, [train_size])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Implementing the Regression" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model = Sequential()\n", "model.add(Dense(10, input_shape=(784,), init='zero'))\n", "model.add(Activation(\"softmax\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Training" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# モデルをコンパイル\n", "model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.01))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/10\n", "60000/60000 [==============================] - 4s - loss: 0.3547 \n", "Epoch 2/10\n", "60000/60000 [==============================] - 3s - loss: 0.2955 \n", "Epoch 3/10\n", "60000/60000 [==============================] - 3s - loss: 0.2883 \n", "Epoch 4/10\n", "60000/60000 [==============================] - 3s - loss: 0.2847 \n", "Epoch 5/10\n", "60000/60000 [==============================] - 3s - loss: 0.2812 \n", "Epoch 6/10\n", "60000/60000 [==============================] - 3s - loss: 0.2777 \n", "Epoch 7/10\n", "60000/60000 [==============================] - 3s - loss: 0.2739 \n", "Epoch 8/10\n", "60000/60000 [==============================] - 4s - loss: 0.2730 \n", "Epoch 9/10\n", "60000/60000 [==============================] - 4s - loss: 0.2691 \n", "Epoch 10/10\n", "60000/60000 [==============================] - 4s - loss: 0.2715 \n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X_train, y_train, nb_epoch=10, batch_size=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluating" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000/10000 [==============================] - 0s \n" ] }, { "data": { "text/plain": [ "[0.32320401608347893, 0.9204]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.evaluate(X_test, y_test, show_accuracy=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accuracy: 0.9204" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }