Format: application/octet-stream
1 {
2 "nbformat": 4,
3 "nbformat_minor": 0,
4 "metadata": {
5 "colab": {
6 "name": "S+P Week 4 Lesson 1.ipynb",
7 "provenance": [],
8 "collapsed_sections": [],
9 "toc_visible": true
10 },
11 "kernelspec": {
12 "display_name": "Python 3",
13 "name": "python3"
14 }
15 },
16 "cells": [
17 {
18 "cell_type": "code",
19 "metadata": {
20 "id": "zX4Kg8DUTKWO"
21 },
22 "source": [
23 "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
24 "# you may not use this file except in compliance with the License.\n",
25 "# You may obtain a copy of the License at\n",
26 "#\n",
27 "# https://www.apache.org/licenses/LICENSE-2.0\n",
28 "#\n",
29 "# Unless required by applicable law or agreed to in writing, software\n",
30 "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
31 "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
32 "# See the License for the specific language governing permissions and\n",
33 "# limitations under the License."
34 ],
35 "execution_count": null,
36 "outputs": []
37 },
38 {
39 "cell_type": "markdown",
40 "metadata": {
41 "id": "view-in-github"
42 },
43 "source": [
44 "<a href=\"https://colab.research.google.com/github/lmoroney/dlaicourse/blob/master/TensorFlow%20In%20Practice/Course%204%20-%20S%2BP/S%2BP%20Week%204%20Lesson%201.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
45 ]
46 },
47 {
48 "cell_type": "code",
49 "metadata": {
50 "cellView": "both",
51 "id": "D1J15Vh_1Jih"
52 },
53 "source": [
54 "try:\n",
55 " # %tensorflow_version only exists in Colab.\n",
56 " %tensorflow_version 2.x\n",
57 "except Exception:\n",
58 " pass\n"
59 ],
60 "execution_count": null,
61 "outputs": []
62 },
63 {
64 "cell_type": "code",
65 "metadata": {
66 "id": "BOjujz601HcS"
67 },
68 "source": [
69 "import tensorflow as tf\n",
70 "import numpy as np\n",
71 "import matplotlib.pyplot as plt\n",
72 "print(tf.__version__)"
73 ],
74 "execution_count": null,
75 "outputs": []
76 },
77 {
78 "cell_type": "code",
79 "metadata": {
80 "id": "Zswl7jRtGzkk"
81 },
82 "source": [
83 "def plot_series(time, series, format=\"-\", start=0, end=None):\n",
84 " plt.plot(time[start:end], series[start:end], format)\n",
85 " plt.xlabel(\"Time\")\n",
86 " plt.ylabel(\"Value\")\n",
87 " plt.grid(True)\n",
88 "\n",
89 "def trend(time, slope=0):\n",
90 " return slope * time\n",
91 "\n",
92 "def seasonal_pattern(season_time):\n",
93 " \"\"\"Just an arbitrary pattern, you can change it if you wish\"\"\"\n",
94 " return np.where(season_time < 0.4,\n",
95 " np.cos(season_time * 2 * np.pi),\n",
96 " 1 / np.exp(3 * season_time))\n",
97 "\n",
98 "def seasonality(time, period, amplitude=1, phase=0):\n",
99 " \"\"\"Repeats the same pattern at each period\"\"\"\n",
100 " season_time = ((time + phase) % period) / period\n",
101 " return amplitude * seasonal_pattern(season_time)\n",
102 "\n",
103 "def noise(time, noise_level=1, seed=None):\n",
104 " rnd = np.random.RandomState(seed)\n",
105 " return rnd.randn(len(time)) * noise_level\n",
106 "\n",
107 "time = np.arange(4 * 365 + 1, dtype=\"float32\")\n",
108 "baseline = 10\n",
109 "series = trend(time, 0.1) \n",
110 "baseline = 10\n",
111 "amplitude = 40\n",
112 "slope = 0.05\n",
113 "noise_level = 5\n",
114 "\n",
115 "# Create the series\n",
116 "series = baseline + trend(time, slope) + seasonality(time, period=365, amplitude=amplitude)\n",
117 "# Update with noise\n",
118 "series += noise(time, noise_level, seed=42)\n",
119 "\n",
120 "split_time = 1000\n",
121 "time_train = time[:split_time]\n",
122 "x_train = series[:split_time]\n",
123 "time_valid = time[split_time:]\n",
124 "x_valid = series[split_time:]\n",
125 "\n",
126 "window_size = 20\n",
127 "batch_size = 32\n",
128 "shuffle_buffer_size = 1000"
129 ],
130 "execution_count": null,
131 "outputs": []
132 },
133 {
134 "cell_type": "code",
135 "metadata": {
136 "id": "4sTTIOCbyShY"
137 },
138 "source": [
139 "def windowed_dataset(series, window_size, batch_size, shuffle_buffer):\n",
140 " series = tf.expand_dims(series, axis=-1)\n",
141 " ds = tf.data.Dataset.from_tensor_slices(series)\n",
142 " ds = ds.window(window_size + 1, shift=1, drop_remainder=True)\n",
143 " ds = ds.flat_map(lambda w: w.batch(window_size + 1))\n",
144 " ds = ds.shuffle(shuffle_buffer)\n",
145 " ds = ds.map(lambda w: (w[:-1], w[1:]))\n",
146 " return ds.batch(batch_size).prefetch(1)"
147 ],
148 "execution_count": null,
149 "outputs": []
150 },
151 {
152 "cell_type": "code",
153 "metadata": {
154 "id": "_eaAX9g_jS5W"
155 },
156 "source": [
157 "def model_forecast(model, series, window_size):\n",
158 " ds = tf.data.Dataset.from_tensor_slices(series)\n",
159 " ds = ds.window(window_size, shift=1, drop_remainder=True)\n",
160 " ds = ds.flat_map(lambda w: w.batch(window_size))\n",
161 " ds = ds.batch(32).prefetch(1)\n",
162 " forecast = model.predict(ds)\n",
163 " return forecast"
164 ],
165 "execution_count": null,
166 "outputs": []
167 },
168 {
169 "cell_type": "code",
170 "metadata": {
171 "id": "Yqc2GTsps0qf"
172 },
173 "source": [
174 "tf.keras.backend.clear_session()\n",
175 "tf.random.set_seed(51)\n",
176 "np.random.seed(51)\n",
177 "\n",
178 "window_size = 30\n",
179 "train_set = windowed_dataset(x_train, window_size, batch_size=128, shuffle_buffer=shuffle_buffer_size)\n",
180 "\n",
181 "model = tf.keras.models.Sequential([\n",
182 " tf.keras.layers.Conv1D(filters=32, kernel_size=5,\n",
183 " strides=1, padding=\"causal\",\n",
184 " activation=\"relu\",\n",
185 " input_shape=[None, 1]),\n",
186 " tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)),\n",
187 " tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)),\n",
188 " tf.keras.layers.Dense(1),\n",
189 " tf.keras.layers.Lambda(lambda x: x * 200)\n",
190 "])\n",
191 "lr_schedule = tf.keras.callbacks.LearningRateScheduler(\n",
192 " lambda epoch: 1e-8 * 10**(epoch / 20))\n",
193 "optimizer = tf.keras.optimizers.SGD(lr=1e-8, momentum=0.9)\n",
194 "model.compile(loss=tf.keras.losses.Huber(),\n",
195 " optimizer=optimizer,\n",
196 " metrics=[\"mae\"])\n",
197 "history = model.fit(train_set, epochs=100, callbacks=[lr_schedule])"
198 ],
199 "execution_count": null,
200 "outputs": []
201 },
202 {
203 "cell_type": "code",
204 "metadata": {
205 "id": "MjTvASUns0qh"
206 },
207 "source": [
208 "plt.semilogx(history.history[\"lr\"], history.history[\"loss\"])\n",
209 "plt.axis([1e-8, 1e-4, 0, 30])"
210 ],
211 "execution_count": null,
212 "outputs": []
213 },
214 {
215 "cell_type": "code",
216 "metadata": {
217 "id": "4uh-97bpLZCA"
218 },
219 "source": [
220 "tf.keras.backend.clear_session()\n",
221 "tf.random.set_seed(51)\n",
222 "np.random.seed(51)\n",
223 "#batch_size = 16\n",
224 "dataset = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)\n",
225 "\n",
226 "model = tf.keras.models.Sequential([\n",
227 " tf.keras.layers.Conv1D(filters=32, kernel_size=3,\n",
228 " strides=1, padding=\"causal\",\n",
229 " activation=\"relu\",\n",
230 " input_shape=[None, 1]),\n",
231 " tf.keras.layers.LSTM(32, return_sequences=True),\n",
232 " tf.keras.layers.LSTM(32, return_sequences=True),\n",
233 " tf.keras.layers.Dense(1),\n",
234 " tf.keras.layers.Lambda(lambda x: x * 200)\n",
235 "])\n",
236 "\n",
237 "optimizer = tf.keras.optimizers.SGD(lr=1e-5, momentum=0.9)\n",
238 "model.compile(loss=tf.keras.losses.Huber(),\n",
239 " optimizer=optimizer,\n",
240 " metrics=[\"mae\"])\n",
241 "history = model.fit(dataset,epochs=500)"
242 ],
243 "execution_count": null,
244 "outputs": []
245 },
246 {
247 "cell_type": "code",
248 "metadata": {
249 "id": "MKkic-mLdkRZ"
250 },
251 "source": [
252 "rnn_forecast = model_forecast(model, series[..., np.newaxis], window_size)\n",
253 "rnn_forecast = rnn_forecast[split_time - window_size:-1, -1, 0]"
254 ],
255 "execution_count": null,
256 "outputs": []
257 },
258 {
259 "cell_type": "code",
260 "metadata": {
261 "id": "4N1toSetdnQq"
262 },
263 "source": [
264 "plt.figure(figsize=(10, 6))\n",
265 "plot_series(time_valid, x_valid)\n",
266 "plot_series(time_valid, rnn_forecast)"
267 ],
268 "execution_count": null,
269 "outputs": []
270 },
271 {
272 "cell_type": "code",
273 "metadata": {
274 "id": "ULKO3JINdqkp"
275 },
276 "source": [
277 "tf.keras.metrics.mean_absolute_error(x_valid, rnn_forecast).numpy()"
278 ],
279 "execution_count": null,
280 "outputs": []
281 },
282 {
283 "cell_type": "code",
284 "metadata": {
285 "id": "ok8LjNbbkig4"
286 },
287 "source": [
288 "import matplotlib.image as mpimg\n",
289 "import matplotlib.pyplot as plt\n",
290 "\n",
291 "#-----------------------------------------------------------\n",
292 "# Retrieve a list of list results on training and test data\n",
293 "# sets for each training epoch\n",
294 "#-----------------------------------------------------------\n",
295 "mae=history.history['mae']\n",
296 "loss=history.history['loss']\n",
297 "\n",
298 "epochs=range(len(loss)) # Get number of epochs\n",
299 "\n",
300 "#------------------------------------------------\n",
301 "# Plot MAE and Loss\n",
302 "#------------------------------------------------\n",
303 "plt.plot(epochs, mae, 'r')\n",
304 "plt.plot(epochs, loss, 'b')\n",
305 "plt.title('MAE and Loss')\n",
306 "plt.xlabel(\"Epochs\")\n",
307 "plt.ylabel(\"Accuracy\")\n",
308 "plt.legend([\"MAE\", \"Loss\"])\n",
309 "\n",
310 "plt.figure()\n",
311 "\n",
312 "epochs_zoom = epochs[200:]\n",
313 "mae_zoom = mae[200:]\n",
314 "loss_zoom = loss[200:]\n",
315 "\n",
316 "#------------------------------------------------\n",
317 "# Plot Zoomed MAE and Loss\n",
318 "#------------------------------------------------\n",
319 "plt.plot(epochs_zoom, mae_zoom, 'r')\n",
320 "plt.plot(epochs_zoom, loss_zoom, 'b')\n",
321 "plt.title('MAE and Loss')\n",
322 "plt.xlabel(\"Epochs\")\n",
323 "plt.ylabel(\"Accuracy\")\n",
324 "plt.legend([\"MAE\", \"Loss\"])\n",
325 "\n",
326 "plt.figure()"
327 ],
328 "execution_count": null,
329 "outputs": []
330 }
331 ]
332 }