|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "metadata": { |
6 | | - "deletable": true, |
7 | | - "editable": true |
8 | | - }, |
| 5 | + "metadata": {}, |
9 | 6 | "source": [ |
10 | 7 | "# Ch `10`: Concept `02`" |
11 | 8 | ] |
12 | 9 | }, |
13 | 10 | { |
14 | 11 | "cell_type": "markdown", |
15 | | - "metadata": { |
16 | | - "deletable": true, |
17 | | - "editable": true |
18 | | - }, |
| 12 | + "metadata": {}, |
19 | 13 | "source": [ |
20 | 14 | "## Recurrent Neural Network" |
21 | 15 | ] |
22 | 16 | }, |
23 | 17 | { |
24 | 18 | "cell_type": "markdown", |
25 | | - "metadata": { |
26 | | - "deletable": true, |
27 | | - "editable": true |
28 | | - }, |
| 19 | + "metadata": {}, |
29 | 20 | "source": [ |
30 | 21 | "Import the relevant libraries:" |
31 | 22 | ] |
32 | 23 | }, |
33 | 24 | { |
34 | 25 | "cell_type": "code", |
35 | | - "execution_count": 1, |
36 | | - "metadata": { |
37 | | - "collapsed": false, |
38 | | - "deletable": true, |
39 | | - "editable": true |
40 | | - }, |
| 26 | + "execution_count": 2, |
| 27 | + "metadata": {}, |
41 | 28 | "outputs": [], |
42 | 29 | "source": [ |
43 | 30 | "import numpy as np\n", |
|
47 | 34 | }, |
48 | 35 | { |
49 | 36 | "cell_type": "markdown", |
50 | | - "metadata": { |
51 | | - "deletable": true, |
52 | | - "editable": true |
53 | | - }, |
| 37 | + "metadata": {}, |
54 | 38 | "source": [ |
55 | 39 | "Define the RNN model:" |
56 | 40 | ] |
57 | 41 | }, |
58 | 42 | { |
59 | 43 | "cell_type": "code", |
60 | | - "execution_count": 2, |
61 | | - "metadata": { |
62 | | - "collapsed": true, |
63 | | - "deletable": true, |
64 | | - "editable": true |
65 | | - }, |
| 44 | + "execution_count": 3, |
| 45 | + "metadata": {}, |
66 | 46 | "outputs": [], |
67 | 47 | "source": [ |
68 | 48 | "class SeriesPredictor:\n", |
|
92 | 72 | " :param W: matrix of fully-connected output layer weights\n", |
93 | 73 | " :param b: vector of fully-connected output layer biases\n", |
94 | 74 | " \"\"\"\n", |
95 | | - " cell = rnn.BasicLSTMCell(self.hidden_dim)\n", |
| 75 | + " cell = rnn.BasicLSTMCell(self.hidden_dim, reuse=tf.get_variable_scope().reuse)\n", |
96 | 76 | " outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)\n", |
97 | 77 | " num_examples = tf.shape(self.x)[0]\n", |
98 | 78 | " W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])\n", |
|
123 | 103 | }, |
124 | 104 | { |
125 | 105 | "cell_type": "markdown", |
126 | | - "metadata": { |
127 | | - "deletable": true, |
128 | | - "editable": true |
129 | | - }, |
| 106 | + "metadata": {}, |
130 | 107 | "source": [ |
131 | 108 | "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:" |
132 | 109 | ] |
133 | 110 | }, |
134 | 111 | { |
135 | 112 | "cell_type": "code", |
136 | | - "execution_count": 3, |
137 | | - "metadata": { |
138 | | - "collapsed": false, |
139 | | - "deletable": true, |
140 | | - "editable": true |
141 | | - }, |
| 113 | + "execution_count": 4, |
| 114 | + "metadata": {}, |
142 | 115 | "outputs": [ |
143 | 116 | { |
144 | 117 | "name": "stdout", |
145 | 118 | "output_type": "stream", |
146 | 119 | "text": [ |
147 | | - "0 92.1852\n", |
148 | | - "100 61.1175\n", |
149 | | - "200 27.0341\n", |
150 | | - "300 13.9523\n", |
151 | | - "400 9.39037\n", |
152 | | - "500 7.08643\n", |
153 | | - "600 5.50997\n", |
154 | | - "700 4.12571\n", |
155 | | - "800 3.12016\n", |
156 | | - "900 2.42311\n", |
| 120 | + "0 103.46295\n", |
| 121 | + "100 63.418705\n", |
| 122 | + "200 23.072838\n", |
| 123 | + "300 11.47684\n", |
| 124 | + "400 7.195353\n", |
| 125 | + "500 4.4564924\n", |
| 126 | + "600 2.8910196\n", |
| 127 | + "700 1.948163\n", |
| 128 | + "800 1.3193887\n", |
| 129 | + "900 0.88628125\n", |
157 | 130 | "Model saved to model.ckpt\n", |
| 131 | + "INFO:tensorflow:Restoring parameters from ./model.ckpt\n", |
158 | 132 | "\n", |
159 | 133 | "Lets run some tests!\n", |
160 | 134 | "\n", |
161 | 135 | "When the input is [[1], [2], [3], [4]]\n", |
162 | 136 | "The ground truth output should be [[1], [3], [5], [7]]\n", |
163 | | - "And the model thinks it is [ 0.96018004 2.76944828 5.35826826 7.3706851 ]\n", |
| 137 | + "And the model thinks it is [0.86705637 2.7930977 5.307706 7.302184 ]\n", |
164 | 138 | "\n", |
165 | 139 | "When the input is [[4], [5], [6], [7]]\n", |
166 | 140 | "The ground truth output should be [[4], [9], [11], [13]]\n", |
167 | | - "And the model thinks it is [ 4.17302942 9.161376 11.13204765 11.64120388]\n", |
| 141 | + "And the model thinks it is [ 4.0726233 9.083956 11.937489 12.943668 ]\n", |
168 | 142 | "\n" |
169 | 143 | ] |
170 | 144 | } |
|
193 | 167 | " print(\"The ground truth output should be {}\".format(actual_y[i]))\n", |
194 | 168 | " print(\"And the model thinks it is {}\\n\".format(pred_y[i]))" |
195 | 169 | ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": null, |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [], |
| 176 | + "source": [] |
196 | 177 | } |
197 | 178 | ], |
198 | 179 | "metadata": { |
|
211 | 192 | "name": "python", |
212 | 193 | "nbconvert_exporter": "python", |
213 | 194 | "pygments_lexer": "ipython3", |
214 | | - "version": "3.5.2" |
| 195 | + "version": "3.6.5" |
215 | 196 | } |
216 | 197 | }, |
217 | 198 | "nbformat": 4, |
|
0 commit comments