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