{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# TensorFlow による 3LP MNIST 実習
ハンズオン資料" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

2016/04/16 機械学習 名古屋 第3回勉強会

" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## はじめに" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ この資料は、TensorFlow を用いて、3LP(3層パーセプトロン)により MNIST の学習を実施することを目的とするものです。 \n", "+ この資料に掲載のコードは、本日の読書会の資料([第2章](http://antimon2.github.io/MLNGY_201604/slides/Chapter2_FeedforwardNeuralNetwork.slides.html)・[第3章](http://antimon2.github.io/MLNGY_201604/slides/Chapter3_StochasticGradientDescent.slides.html))、および TensorFlow 公式のチュートリアル [MNIST For ML Beginners](https://www.tensorflow.org/versions/master/tutorials/mnist/beginners/) を元に構築しております。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 目標" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ TensorFlow を用いた基本的な DeepLearning を自分で書ける。\n", "+ 正則化や、学習係数・モメンタム等の工夫を加えて、学習の精度を上げる(オプション、もしくは宿題)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 環境等" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以下の環境を前提とします。\n", "\n", "+ Python(必須)(2.7.x / 3.x どちらでもOK)\n", "+ TensorFlow(必須)(0.6 / 0.7 / 0.8[New!] どれでもOK)\n", "+ matplotlib(任意、結果を確認する際に利用)\n", "+ IPython(任意)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## TensorFlow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ [公式サイト](https://www.tensorflow.org/)\n", "+ Google 製の「データフローグラフを用いた数値計算ライブラリ」(公式の説明を私訳)\n", " + DeepLearning 用の機能も豊富。\n", "+ つい先日 [v0.8がリリース(2016/04/13)](http://googleresearch.blogspot.com/2016/04/announcing-tensorflow-08-now-with.html)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "インストールの詳細省略。 \n", "インストールが成功していれば、Python のインタラクティブシェル(もしくは ipython, Jupyter 等)で↓以下のようにすれば利用開始。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "※ 今回は TensorBoard は不使用。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## MNIST" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ 機械学習定番の、多クラス分類(教師あり学習)問題「手書き数字認識」のサンプルデータ。\n", "+ 多くの機械学習ライブラリで、手軽に体験できるよう「MNIST をロードする関数(またはサンプル)」が用意されている。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "TensorFlow では、以下のようにすると MNIST データセット(訓練データおよびテストデータ)を扱えるようになる:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from tensorflow.examples.tutorials.mnist import input_data\n", "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "※↑データが存在しなければネットワーク経由でダウンロードから開始。 \n", " ダウンロード後(or ダウンロード済のデータが存在する場合)そのデータを読み込んでくれる。 \n", "※⇒既にダウンロード済のデータ(4つの.gzファイル)を持っていたらそれを所定の場所に置いておくだけでもOK。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 3LP 構築" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 訓練データ(及び正解データ)用の変数(プレースホルダ)用意" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 訓練データ:長さ784のベクトルデータ(=28x28 の画像データ)\n", "x = tf.placeholder(tf.float32, [None, 784])\n", "# 正解データ:長さ10のベクトルデータ(0,1,…,9 の対応する要素のみ 1、他は 0)\n", "d = tf.placeholder(tf.float32, [None, 10])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 第1層" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ 入力サイズ:784(訓練データのサイズ)\n", "+ 出力サイズ:128\n", "+ 重み初期化:ガウス分布による乱数で初期化(平均 0.0, 分散 0.05)\n", "+ バイアス初期化:ゼロ値\n", "+ 活性化関数:正規化線形関数(ReLU)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "W1 = tf.Variable(tf.random_normal([784, 128], mean=0.0, stddev=0.05))\n", "b1 = tf.Variable(tf.zeros([128]))\n", "z1 = tf.nn.relu(tf.matmul(x, W1) + b1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 第2層" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ 入力サイズ:128(前の層の出力サイズ)\n", "+ 出力サイズ:64\n", "+ 重み初期化:ガウス分布による乱数で初期化(平均 0.0, 分散 0.05)\n", "+ バイアス初期化:ゼロ値\n", "+ 活性化関数:正規化線形関数(ReLU)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "W2 = tf.Variable(tf.random_normal([128, 64], mean=0.0, stddev=0.05))\n", "b2 = tf.Variable(tf.zeros([64]))\n", "z2 = tf.nn.relu(tf.matmul(z1, W2) + b2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 第3層(出力層)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ 入力サイズ:64(前の層の出力サイズ)\n", "+ 出力サイズ:10(正解データの要素数)\n", "+ 重み初期化:ガウス分布による乱数で初期化(平均 0.0, 分散 0.05)\n", "+ バイアス初期化:ゼロ値\n", "+ 活性化関数:ソフトマックス関数" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "W3 = tf.Variable(tf.random_normal([64, 10], mean=0.0, stddev=0.05))\n", "b3 = tf.Variable(tf.zeros([10]))\n", "y = tf.nn.softmax(tf.matmul(z2, W3) + b3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 誤差関数(=交差エントロピー)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x_entropy = -tf.reduce_sum(d * tf.log(y))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 最適化(=確率的勾配降下法)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "+ 以下では `tf.train.GradientDescentOptimizer` を利用した例を示す。\n", " + 引数(=学習係数)は 0.01\n", "+ 他に、`tf.train.AdagradOptimizer`、`tf.train.MomentumOptimizer` 等を利用しても良い。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "optimizer = tf.train.GradientDescentOptimizer(0.01)\n", "train_step = optimizer.minimize(x_entropy)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## 学習" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### TensorFlow による学習の流れ" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "0. 変数の初期化\n", "1. セッションの開始\n", "2. ミニバッチを利用した学習ステップのループ\n", "3. (オプション)精度の確認\n", "4. (オプション)推測結果確認" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "source": [ "### 1. 変数の初期化" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "init = tf.initialize_all_variables()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "※↑これも「変数を初期化する」という命令を宣言しているだけ。 \n", " この後 `sess.run(init)` することで(そのセッション内で実際に)変数の初期化が実行される。" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "source": [ "### 2. セッションの開始" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sess = tf.Session()\n", "sess.run(init)\n", "# ↑実際にはここで始めて各変数が初期化される" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "source": [ "### 2.5. (先に)精度の確認(の準備)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "今回は分かりやすくするため、ループ中にも随時精度を確認してみる。 \n", "そのため先に精度を確認する各種準備。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# y(=学習結果の出力)と d(正解データ)で一致しているかどうかを確認\n", "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(d, 1))\n", "# 平均(=一致している個数÷全データ数)を計算\n", "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "source": [ "### 3. ミニバッチを利用した学習ステップのループ" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# ループ回数は 2000回\n", "# バッチサイズは 100\n", "# 200イテレーションごとに現在の精度を算出し出力\n", "for i in range(1, 2001):\n", " batch_xs, batch_ys = mnist.train.next_batch(100)\n", " sess.run(train_step, feed_dict={x: batch_xs, d: batch_ys})\n", " if i % 200 == 0:\n", " train_accuracy = sess.run(accuracy, feed_dict={x: batch_xs, d: batch_ys})\n", " print(' step, accurary = %6d: %6.3f' % (i, train_accuracy))\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 4. 精度の確認" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "テストデータを利用して精度の確認。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(sess.run(accuracy, feed_dict={x: mnist.test.images, d: mnist.test.labels}))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "↑だいたい0.96前後になる(と思われます)。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 5. 推測結果の確認" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "matplotlib が入っている場合、以下を実行して実際のデータと推測結果を確認してみる。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%matplotlib inline\n", "# ↑Jupyter 上で実行するときに必要\n", "\n", "import matplotlib\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.cm as cm\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "n = len(mnist.test.images)\n", "i = random.randrange(n)\n", "\n", "imageData = mnist.test.images[i]\n", "label = mnist.test.labels[i]\n", "\n", "result = sess.run(y, feed_dict={x: [imageData], d: [label]})\n", "print(\"Classified as: %d\" % np.argmax(result))\n", "\n", "image = np.reshape(imageData, [28, 28])\n", "\n", "plt.imshow(image, cmap = cm.Greys)\n", "# plt.show()\n", "# ↑Jupyter 以外では必要" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 発展" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### さらなる精度向上のためにできること" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ イテレーション回数を調整する\n", " + TensorBoard を利用するか、自分で matplotlib を用いるなどして学習曲線を引く\n", "+ 層を増やしてみる(4層、5層、…)\n", "+ 各層のユニット数(サイズ)を変えてみる\n", "+ 学習係数を変更してみる\n", "+ 正則化項を追加してみる\n", "+ `tf.train.GradientDescentOptimizer` の代わりに、`tf.train.AdagradOptimizer` や `tf.train.MomentumOptimizer` を試してみる。\n", " + `optimizer = tf.train.MomentumOptimizer(0.1, 0.9)` とするだけでもかなり変わる(はず)\n", "+ **CNN に手を出してみる**\n", " + 次回は↑をやる予定。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "TensorFlow (Python 2)", "language": "python", "name": "tensorflow" }, "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.11" } }, "nbformat": 4, "nbformat_minor": 0 }