Format: application/octet-stream
⬇ Download File (6.1 KB)
   1  {
   2    "nbformat": 4,
   3    "nbformat_minor": 0,
   4    "metadata": {
   5      "accelerator": "GPU",
   6      "colab": {
   7        "name": "S+P_Week_2_Exercise_Answer.ipynb",
   8        "provenance": [],
   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": "code",
  40        "metadata": {
  41          "cellView": "both",
  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": "TW-vT7eLYAdb"
 143        },
 144        "source": [
 145          "dataset = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)\n",
 146          "\n",
 147          "\n",
 148          "model = tf.keras.models.Sequential([\n",
 149          "    tf.keras.layers.Dense(100, input_shape=[window_size], activation=\"relu\"), \n",
 150          "    tf.keras.layers.Dense(10, activation=\"relu\"), \n",
 151          "    tf.keras.layers.Dense(1)\n",
 152          "])\n",
 153          "\n",
 154          "model.compile(loss=\"mse\", optimizer=tf.keras.optimizers.SGD(lr=1e-6, momentum=0.9))\n",
 155          "model.fit(dataset,epochs=100,verbose=0)\n"
 156        ],
 157        "execution_count": null,
 158        "outputs": []
 159      },
 160      {
 161        "cell_type": "code",
 162        "metadata": {
 163          "id": "efhco2rYyIFF"
 164        },
 165        "source": [
 166          "forecast = []\n",
 167          "for time in range(len(series) - window_size):\n",
 168          "  forecast.append(model.predict(series[time:time + window_size][np.newaxis]))\n",
 169          "\n",
 170          "forecast = forecast[split_time-window_size:]\n",
 171          "results = np.array(forecast)[:, 0, 0]\n",
 172          "\n",
 173          "\n",
 174          "plt.figure(figsize=(10, 6))\n",
 175          "\n",
 176          "plot_series(time_valid, x_valid)\n",
 177          "plot_series(time_valid, results)"
 178        ],
 179        "execution_count": null,
 180        "outputs": []
 181      },
 182      {
 183        "cell_type": "code",
 184        "metadata": {
 185          "id": "-kT6j186YO6K"
 186        },
 187        "source": [
 188          "tf.keras.metrics.mean_absolute_error(x_valid, results).numpy()"
 189        ],
 190        "execution_count": null,
 191        "outputs": []
 192      }
 193    ]
 194  }