{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 程序说明\n", "时间:2016年11月16日\n", "\n", "说明:该程序是一个包含LSTM的神经网络。\n", "\n", "数据集:MNIST" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.加载keras模块" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "from keras.models import Sequential\n", "from keras.layers import LSTM, Dense\n", "from keras.datasets import mnist\n", "from keras.utils import np_utils\n", "from keras import initializations\n", "\n", "def init_weights(shape, name=None):\n", " return initializations.normal(shape, scale=0.01, name=name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 如需绘制模型请加载plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from keras.utils.visualize_util import plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.变量初始化" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Hyper parameters\n", "batch_size = 128\n", "nb_epoch = 10\n", "\n", "# Parameters for MNIST dataset\n", "img_rows, img_cols = 28, 28\n", "nb_classes = 10\n", "\n", "# Parameters for LSTM network\n", "nb_lstm_outputs = 30\n", "nb_time_steps = img_rows\n", "dim_input_vector = img_cols" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.准备数据" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('X_train original shape:', (60000, 28, 28))\n", "('X_train shape:', (60000, 28, 28))\n", "(60000, 'train samples')\n", "(10000, 'test samples')\n" ] } ], "source": [ "# Load MNIST dataset\n", "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", "print('X_train original shape:', X_train.shape)\n", "input_shape = (nb_time_steps, dim_input_vector)\n", "\n", "X_train = X_train.astype('float32') / 255.\n", "X_test = X_test.astype('float32') / 255.\n", "Y_train = np_utils.to_categorical(y_train, nb_classes)\n", "Y_test = np_utils.to_categorical(y_test, nb_classes)\n", "\n", "print('X_train shape:', X_train.shape)\n", "print(X_train.shape[0], 'train samples')\n", "print(X_test.shape[0], 'test samples')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.建立模型\n", "### 使用Sequential()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Build LSTM network\n", "model = Sequential()\n", "model.add(LSTM(nb_lstm_outputs, input_shape=input_shape))\n", "model.add(Dense(nb_classes, activation='softmax', init=init_weights))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 打印模型" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "____________________________________________________________________________________________________\n", "Layer (type) Output Shape Param # Connected to \n", "====================================================================================================\n", "lstm_1 (LSTM) (None, 30) 7080 lstm_input_1[0][0] \n", "____________________________________________________________________________________________________\n", "dense_1 (Dense) (None, 10) 310 lstm_1[0][0] \n", "====================================================================================================\n", "Total params: 7390\n", "____________________________________________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 绘制模型结构图,并保存成图片" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "plot(model, to_file='lstm_model.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 显示绘制的图片\n", "![image](http://p1.bqimg.com/4851/08854414148a36b0.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.训练与评估\n", "### 编译模型" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 迭代训练" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/10\n", "60000/60000 [==============================] - 28s - loss: 1.3082 - acc: 0.6124 \n", "Epoch 2/10\n", "60000/60000 [==============================] - 28s - loss: 0.5380 - acc: 0.8467 \n", "Epoch 3/10\n", "60000/60000 [==============================] - 28s - loss: 0.3363 - acc: 0.9060 \n", "Epoch 4/10\n", "60000/60000 [==============================] - 28s - loss: 0.2553 - acc: 0.9292 \n", "Epoch 5/10\n", "60000/60000 [==============================] - 28s - loss: 0.2113 - acc: 0.9408 \n", "Epoch 6/10\n", "60000/60000 [==============================] - 28s - loss: 0.1811 - acc: 0.9488 \n", "Epoch 7/10\n", "60000/60000 [==============================] - 28s - loss: 0.1578 - acc: 0.9548 \n", "Epoch 8/10\n", "60000/60000 [==============================] - 28s - loss: 0.1407 - acc: 0.9597 \n", "Epoch 9/10\n", "60000/60000 [==============================] - 28s - loss: 0.1284 - acc: 0.9633 \n", "Epoch 10/10\n", "60000/60000 [==============================] - 28s - loss: 0.1188 - acc: 0.9655 \n" ] } ], "source": [ "history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, shuffle=True, verbose=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 模型评估" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000/10000 [==============================] - 5s \n", "('Test score:', 0.11906883909329773)\n", "('Test accuracy:', 0.96530000000000005)\n" ] } ], "source": [ "score = model.evaluate(X_test, Y_test, verbose=1)\n", "print('Test score:', score[0])\n", "print('Test accuracy:', score[1])" ] }, { "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.12" }, "ssap_exp_config": { "error_alert": "Error Occurs!", "initial": [], "max_iteration": 1000, "recv_id": "", "running": [], "summary": [], "version": "1.1.1" } }, "nbformat": 4, "nbformat_minor": 0 }