|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "collapsed": true |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "* http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/\n", |
| 10 | + "* http://learningtensorflow.com/index.html" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "metadata": { |
| 17 | + "collapsed": true |
| 18 | + }, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "import tensorflow as tf\n", |
| 22 | + "import numpy as np\n", |
| 23 | + "from tensorflow.contrib import rnn\n", |
| 24 | + "import pprint\n", |
| 25 | + "pp = pprint.PrettyPrinter(indent=4)" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "## Static RNN" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 2, |
| 38 | + "metadata": { |
| 39 | + "collapsed": true |
| 40 | + }, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "tf.reset_default_graph()\n", |
| 44 | + "sess = tf.InteractiveSession()" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 3, |
| 50 | + "metadata": { |
| 51 | + "collapsed": false |
| 52 | + }, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "name": "stdout", |
| 56 | + "output_type": "stream", |
| 57 | + "text": [ |
| 58 | + "array([[[ 0., 1., 2.],\n", |
| 59 | + " [ 3., 4., 5.],\n", |
| 60 | + " [ 6., 7., 8.],\n", |
| 61 | + " [ 9., 10., 11.]],\n", |
| 62 | + "\n", |
| 63 | + " [[ 12., 13., 14.],\n", |
| 64 | + " [ 15., 16., 17.],\n", |
| 65 | + " [ 18., 19., 20.],\n", |
| 66 | + " [ 21., 22., 23.]]], dtype=float32)\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "# Create input data\n", |
| 72 | + "x_data = np.arange(24, dtype=np.float32).reshape(2,4,3)\n", |
| 73 | + "pp.pprint(x_data)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 4, |
| 79 | + "metadata": { |
| 80 | + "collapsed": false |
| 81 | + }, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "name": "stdout", |
| 85 | + "output_type": "stream", |
| 86 | + "text": [ |
| 87 | + "5 LSTMStateTuple(c=5, h=5)\n", |
| 88 | + "5 (LSTMStateTuple(c=5, h=5), LSTMStateTuple(c=5, h=5))\n" |
| 89 | + ] |
| 90 | + } |
| 91 | + ], |
| 92 | + "source": [ |
| 93 | + "cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True)\n", |
| 94 | + "print(cell.output_size, cell.state_size)\n", |
| 95 | + "cell = rnn.MultiRNNCell([cell] * 2, state_is_tuple=True)\n", |
| 96 | + "print(cell.output_size, cell.state_size)" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 5, |
| 102 | + "metadata": { |
| 103 | + "collapsed": false |
| 104 | + }, |
| 105 | + "outputs": [ |
| 106 | + { |
| 107 | + "name": "stdout", |
| 108 | + "output_type": "stream", |
| 109 | + "text": [ |
| 110 | + "[ array([[ 0., 1., 2.],\n", |
| 111 | + " [ 3., 4., 5.],\n", |
| 112 | + " [ 6., 7., 8.],\n", |
| 113 | + " [ 9., 10., 11.]], dtype=float32),\n", |
| 114 | + " array([[ 12., 13., 14.],\n", |
| 115 | + " [ 15., 16., 17.],\n", |
| 116 | + " [ 18., 19., 20.],\n", |
| 117 | + " [ 21., 22., 23.]], dtype=float32)]\n" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "x_data = tf.reshape(x_data, [-1, 3])\n", |
| 123 | + "x_split = tf.split(value=x_data, num_or_size_splits=2)\n", |
| 124 | + "pp.pprint(sess.run(x_split))" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 6, |
| 130 | + "metadata": { |
| 131 | + "collapsed": false |
| 132 | + }, |
| 133 | + "outputs": [ |
| 134 | + { |
| 135 | + "ename": "AttributeError", |
| 136 | + "evalue": "module 'tensorflow.python.ops.nn' has no attribute 'rnn'", |
| 137 | + "output_type": "error", |
| 138 | + "traceback": [ |
| 139 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 140 | + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", |
| 141 | + "\u001b[0;32m<ipython-input-6-9b5a47592263>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrnn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcell\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx_split\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mglobal_variables_initializer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 142 | + "\u001b[0;31mAttributeError\u001b[0m: module 'tensorflow.python.ops.nn' has no attribute 'rnn'" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "outputs,_states = tf.nn.rnn(cell, x_split, dtype=tf.float32)\n", |
| 148 | + "print(outputs)\n", |
| 149 | + "sess.run(tf.global_variables_initializer())\n", |
| 150 | + "pp.pprint(sess.run(outputs))" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "metadata": {}, |
| 156 | + "source": [ |
| 157 | + "## Dynamic Rnn" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "metadata": { |
| 164 | + "collapsed": true |
| 165 | + }, |
| 166 | + "outputs": [], |
| 167 | + "source": [ |
| 168 | + "tf.reset_default_graph()\n", |
| 169 | + "sess = tf.InteractiveSession()" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": null, |
| 175 | + "metadata": { |
| 176 | + "collapsed": false |
| 177 | + }, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "tf.reset_default_graph()\n", |
| 181 | + "\n", |
| 182 | + "# Create input data\n", |
| 183 | + "X = np.random.randn(2, 10, 8)\n", |
| 184 | + "\n", |
| 185 | + "# The second example is of length 6 \n", |
| 186 | + "X[1,6:] = 0\n", |
| 187 | + "X_lengths = [10, 6]\n", |
| 188 | + "\n", |
| 189 | + "cell = rnn.LSTMCell(num_units=64, state_is_tuple=True)\n", |
| 190 | + "\n", |
| 191 | + "outputs, last_states = tf.nn.dynamic_rnn(\n", |
| 192 | + " cell=cell,\n", |
| 193 | + " dtype=tf.float64,\n", |
| 194 | + " sequence_length=X_lengths,\n", |
| 195 | + " inputs=X)" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "code", |
| 200 | + "execution_count": null, |
| 201 | + "metadata": { |
| 202 | + "collapsed": false |
| 203 | + }, |
| 204 | + "outputs": [], |
| 205 | + "source": [ |
| 206 | + "sess.run(tf.global_variables_initializer())\n", |
| 207 | + "pp.pprint(sess.run(outputs))" |
| 208 | + ] |
| 209 | + } |
| 210 | + ], |
| 211 | + "metadata": { |
| 212 | + "kernelspec": { |
| 213 | + "display_name": "Python 3", |
| 214 | + "language": "python", |
| 215 | + "name": "python3" |
| 216 | + }, |
| 217 | + "language_info": { |
| 218 | + "codemirror_mode": { |
| 219 | + "name": "ipython", |
| 220 | + "version": 3 |
| 221 | + }, |
| 222 | + "file_extension": ".py", |
| 223 | + "mimetype": "text/x-python", |
| 224 | + "name": "python", |
| 225 | + "nbconvert_exporter": "python", |
| 226 | + "pygments_lexer": "ipython3", |
| 227 | + "version": "3.6.0" |
| 228 | + } |
| 229 | + }, |
| 230 | + "nbformat": 4, |
| 231 | + "nbformat_minor": 0 |
| 232 | +} |
0 commit comments