|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Ch `10`: Concept `02`" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Recurrent Neural Network" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "Import the relevant libraries:" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 1, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [ |
| 29 | + { |
| 30 | + "name": "stderr", |
| 31 | + "output_type": "stream", |
| 32 | + "text": [ |
| 33 | + "/Users/anastasiia/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", |
| 34 | + " from ._conv import register_converters as _register_converters\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "import numpy as np\n", |
| 40 | + "import tensorflow as tf\n", |
| 41 | + "from tensorflow.contrib import rnn" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "Define the RNN model:" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 2, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "class SeriesPredictor:\n", |
| 58 | + "\n", |
| 59 | + " def __init__(self, input_dim, seq_size, hidden_dim=10):\n", |
| 60 | + " # Hyperparameters\n", |
| 61 | + " self.input_dim = input_dim\n", |
| 62 | + " self.seq_size = seq_size\n", |
| 63 | + " self.hidden_dim = hidden_dim\n", |
| 64 | + "\n", |
| 65 | + " # Weight variables and input placeholders\n", |
| 66 | + " self.W_out = tf.Variable(tf.random_normal([hidden_dim, 1]), name='W_out')\n", |
| 67 | + " self.b_out = tf.Variable(tf.random_normal([1]), name='b_out')\n", |
| 68 | + " self.x = tf.placeholder(tf.float32, [None, seq_size, input_dim])\n", |
| 69 | + " self.y = tf.placeholder(tf.float32, [None, seq_size])\n", |
| 70 | + "\n", |
| 71 | + " # Cost optimizer\n", |
| 72 | + " self.cost = tf.reduce_mean(tf.square(self.model() - self.y))\n", |
| 73 | + " self.train_op = tf.train.AdamOptimizer().minimize(self.cost)\n", |
| 74 | + "\n", |
| 75 | + " # Auxiliary ops\n", |
| 76 | + " self.saver = tf.train.Saver()\n", |
| 77 | + "\n", |
| 78 | + " def model(self):\n", |
| 79 | + " \"\"\"\n", |
| 80 | + " :param x: inputs of size [T, batch_size, input_size]\n", |
| 81 | + " :param W: matrix of fully-connected output layer weights\n", |
| 82 | + " :param b: vector of fully-connected output layer biases\n", |
| 83 | + " \"\"\"\n", |
| 84 | + " cell = rnn.BasicLSTMCell(self.hidden_dim, reuse=tf.get_variable_scope().reuse)\n", |
| 85 | + " outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)\n", |
| 86 | + " num_examples = tf.shape(self.x)[0]\n", |
| 87 | + " W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])\n", |
| 88 | + " out = tf.matmul(outputs, W_repeated) + self.b_out\n", |
| 89 | + " out = tf.squeeze(out)\n", |
| 90 | + " return out\n", |
| 91 | + "\n", |
| 92 | + " def train(self, train_x, train_y):\n", |
| 93 | + " with tf.Session() as sess:\n", |
| 94 | + " tf.get_variable_scope().reuse_variables()\n", |
| 95 | + " sess.run(tf.global_variables_initializer())\n", |
| 96 | + " for i in range(1000):\n", |
| 97 | + " _, mse = sess.run([self.train_op, self.cost], feed_dict={self.x: train_x, self.y: train_y})\n", |
| 98 | + " if i % 100 == 0:\n", |
| 99 | + " print(i, mse)\n", |
| 100 | + " save_path = self.saver.save(sess, 'model.ckpt')\n", |
| 101 | + " print('Model saved to {}'.format(save_path))\n", |
| 102 | + "\n", |
| 103 | + " def test(self, test_x):\n", |
| 104 | + " with tf.Session() as sess:\n", |
| 105 | + " tf.get_variable_scope().reuse_variables()\n", |
| 106 | + " self.saver.restore(sess, './model.ckpt')\n", |
| 107 | + " output = sess.run(self.model(), feed_dict={self.x: test_x})\n", |
| 108 | + " return output\n", |
| 109 | + "\n", |
| 110 | + "\n" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "Now, we'll train a series predictor. Let's say we have a sequence of numbers `[a, b, c, d]` that we want to transform into `[a, a+b, b+c, c+d]`. We'll give the RNN a couple examples in the training data. Let's see how well it learns this intended transformation:" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 3, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [ |
| 125 | + { |
| 126 | + "name": "stdout", |
| 127 | + "output_type": "stream", |
| 128 | + "text": [ |
| 129 | + "0 70.91218\n", |
| 130 | + "100 28.614563\n", |
| 131 | + "200 10.268018\n", |
| 132 | + "300 5.664831\n", |
| 133 | + "400 3.5102208\n", |
| 134 | + "500 1.883831\n", |
| 135 | + "600 1.0723968\n", |
| 136 | + "700 0.6708067\n", |
| 137 | + "800 0.4667667\n", |
| 138 | + "900 0.35269937\n", |
| 139 | + "Model saved to model.ckpt\n", |
| 140 | + "INFO:tensorflow:Restoring parameters from ./model.ckpt\n", |
| 141 | + "\n", |
| 142 | + "Lets run some tests!\n", |
| 143 | + "\n", |
| 144 | + "When the input is [[1], [2], [3], [4]]\n", |
| 145 | + "The ground truth output should be [[1], [3], [5], [7]]\n", |
| 146 | + "And the model thinks it is [1.6758004 2.7610283 4.739178 7.087058 ]\n", |
| 147 | + "\n", |
| 148 | + "When the input is [[4], [5], [6], [7]]\n", |
| 149 | + "The ground truth output should be [[4], [9], [11], [13]]\n", |
| 150 | + "And the model thinks it is [ 4.4391885 9.112013 12.074081 13.157787 ]\n", |
| 151 | + "\n" |
| 152 | + ] |
| 153 | + } |
| 154 | + ], |
| 155 | + "source": [ |
| 156 | + "if __name__ == '__main__':\n", |
| 157 | + " predictor = SeriesPredictor(input_dim=1, seq_size=4, hidden_dim=10)\n", |
| 158 | + " train_x = [[[1], [2], [5], [6]],\n", |
| 159 | + " [[5], [7], [7], [8]],\n", |
| 160 | + " [[3], [4], [5], [7]]]\n", |
| 161 | + " train_y = [[1, 3, 7, 11],\n", |
| 162 | + " [5, 12, 14, 15],\n", |
| 163 | + " [3, 7, 9, 12]]\n", |
| 164 | + " predictor.train(train_x, train_y)\n", |
| 165 | + "\n", |
| 166 | + " test_x = [[[1], [2], [3], [4]], # 1, 3, 5, 7\n", |
| 167 | + " [[4], [5], [6], [7]]] # 4, 9, 11, 13\n", |
| 168 | + " actual_y = [[[1], [3], [5], [7]],\n", |
| 169 | + " [[4], [9], [11], [13]]]\n", |
| 170 | + " pred_y = predictor.test(test_x)\n", |
| 171 | + " \n", |
| 172 | + " print(\"\\nLets run some tests!\\n\")\n", |
| 173 | + " \n", |
| 174 | + " for i, x in enumerate(test_x):\n", |
| 175 | + " print(\"When the input is {}\".format(x))\n", |
| 176 | + " print(\"The ground truth output should be {}\".format(actual_y[i]))\n", |
| 177 | + " print(\"And the model thinks it is {}\\n\".format(pred_y[i]))" |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
| 181 | + "metadata": { |
| 182 | + "kernelspec": { |
| 183 | + "display_name": "Python 3", |
| 184 | + "language": "python", |
| 185 | + "name": "python3" |
| 186 | + }, |
| 187 | + "language_info": { |
| 188 | + "codemirror_mode": { |
| 189 | + "name": "ipython", |
| 190 | + "version": 3 |
| 191 | + }, |
| 192 | + "file_extension": ".py", |
| 193 | + "mimetype": "text/x-python", |
| 194 | + "name": "python", |
| 195 | + "nbconvert_exporter": "python", |
| 196 | + "pygments_lexer": "ipython3", |
| 197 | + "version": "3.6.5" |
| 198 | + } |
| 199 | + }, |
| 200 | + "nbformat": 4, |
| 201 | + "nbformat_minor": 1 |
| 202 | +} |
0 commit comments