{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TensorFlow による RNN 実習
ハンズオン資料"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
2016/09/10 機械学習 名古屋 第6回勉強会
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## はじめに" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ この資料は、TensorFlow を用いて、簡単な RNN の構築・学習を実施することを目的とするものです。 \n", "+ この資料に掲載のコードは、TensorFlow 公式のチュートリアル [Recurrent Neural Networks](https://www.tensorflow.org/versions/master/tutorials/recurrent/) を元に構築しております。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## 目標" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "+ TensorFlow と PTB データセットを用いて、基本的な RNN のモデル構築・学習・文章推測ができる。\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.7 / 0.8 / 0.9 / 0.10[New!] どれでもOK)\n", "+ IPython(任意)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "※ TensorFlow 0.6(以前)は未対応。" ] }, { "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.10](https://github.com/tensorflow/tensorflow/releases/tag/v0.10.0rc0) がリリース準備中(2016/09/03 現在、v0.10-rc0 公開中)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "インストールの詳細省略。 \n", "インストールが成功していれば、Python のインタラクティブシェル(もしくは ipython, Jupyter 等)で↓以下のようにすれば利用開始。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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": [ "## RNN" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### おさらい" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "+ RNN:\n", " + 系列データ(文章、音声、映像等)のコンテキストを学習・分類するモデルの総称\n", "+ コンテキスト(文脈):\n", " + 系列内の要素の並び・依存関係\n", " + 要素の例:\n", " + 「文章」中の「文字」、「単語」、「形態素」等\n", " + 「音声」中の「(パワースペクトル密度係数の)ベクトル」、「音素」等\n", " + 「映像(動画)」中の「(ある時刻における)画像(静止画)」等" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "+ LSTM:\n", " + RNN の拡張モデル\n", " + メモリセルと3つのゲートからなるLSTMブロックという構造を持つ\n", " + (従来の RNN と比較して)「長期にわたる依存性」を学習可能" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## PTB データセット" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "+ PTB データセット:\n", " + 単語の履歴を元に、文章中の次の単語を予測する「文章予測モデル」のための(サンプル)データセット。\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "※ RNN の最初のサンプル構築・実験に最適。" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### データ準備" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz をダウンロード・展開。 \n", "(その中の `data/ptb.train.txt`, `data/ptb.valid.txt`, `data/ptb.test.txt` のみ使用)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "※ 必要なデータのみ抽出しアーカイブしたファイルを用意 → https://github.com/antimon2/MLN_201609/raw/tf_rnn_dev/simple-examples.tgz \n", "※ あと今回は自動ダウンロード・展開の機能は提供なし。各自手作業でお願いしますm(\\_ \\_)m" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def _read_words(filename):\n", " with tf.gfile.GFile(filename, \"r\") as f:\n", " return f.read().replace(\"\\n\", \"