|
1 | 1 | {
|
2 |
| - "cells": [], |
3 |
| - "metadata": {}, |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Example for predicting using the RNN\n", |
| 8 | + "\n", |
| 9 | + "This is an example for running the RNN and get water demand predictions.\n", |
| 10 | + "\n", |
| 11 | + "Requirements:\n", |
| 12 | + "- Python 3.6+\n", |
| 13 | + "- NumPy 1.14+ (http://www.numpy.org/)\n", |
| 14 | + "\n", |
| 15 | + "To run this Notebook locally:\n", |
| 16 | + "- Jupyter (https://jupyter.org/)\n", |
| 17 | + "\n", |
| 18 | + "___Note:___ This model is an online prediction model. The current version of code does provides a separate option for training the RNN. Refer to [training_example.ipynb](./training_example.ipynb) for training the RNN." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "#### Step 1: Import external libraries and the `rnn` package" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 1, |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "import os\n", |
| 35 | + "from rnn import *" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "#### Step 2: Specify the location on disk of the data directory and the input CSV file (optional)" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 2, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "DATA_LOCATION = os.getcwd()\n", |
| 52 | + "INPUT_FILE = os.path.join(DATA_LOCATION, 'input_files', 'water_6_17_reduced_2_cap_rs.csv')" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "#### Step 3: To initialize the predictor RNN, create a `rnn_predict` object.\n", |
| 60 | + "\n", |
| 61 | + "Required parameters:\n", |
| 62 | + "- `weights`: Location on disk of the weights file. Typically a Python serialized object (Pickle) file.\n", |
| 63 | + "\n", |
| 64 | + "Optional parameters:\n", |
| 65 | + "- `alpha`: Learning rate for the network, typically a value in the range `(0, 1)`. Default value: `0.38`.\n", |
| 66 | + "- `log`: Boolean value specifying if the execution should be logged to the console. Recommended if running from the command line or in an IDE. Default value: `False`.\n", |
| 67 | + "- `h_bias`: Boolean value specifying if hidden layer should have a bias node. Currently trained models include a hidden layer bias. Default value: `True`." |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 3, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "predictor = rnn_predict(os.path.join(DATA_LOCATION, 'input_files', 'daily_rnn_retrained_16.pickle'))" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "At this point, the model is ready to make predictions.\n", |
| 84 | + "\n", |
| 85 | + "#### Step 4a: Give the network a list of inputs to make predictions\n", |
| 86 | + "\n", |
| 87 | + "Required parameters:\n", |
| 88 | + "- `inputs`: A list of `[day_of_the_year, maximum_temperature, precipitation]` lists. These should be real world observed or predicted values.\n", |
| 89 | + "\n", |
| 90 | + "Optional parameters:\n", |
| 91 | + "- `networks`: A list of the same length as `inputs` list with values of the range `[0, 11]`, specifying what network each prediction to run on, typically `num_month - 1`. If not specified, all predictions will be made for the month of January.\n", |
| 92 | + "- `targets`: A list of the same length as `inputs` of the observed values for the day. The target values are required for updating the weights of the model.\n", |
| 93 | + "- `recal`: Boolean value specifying if the network weights should be recalculated. Recommended if using observed demand as target, to improve future predictions. If set to `True`, requires `targets` to be specified. Default value: `False`.\n", |
| 94 | + "\n", |
| 95 | + "Output:\n", |
| 96 | + "- `predictions`: A list of the same length as `inputs` of the predictions made by the network." |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 4, |
| 102 | + "metadata": { |
| 103 | + "scrolled": false |
| 104 | + }, |
| 105 | + "outputs": [ |
| 106 | + { |
| 107 | + "name": "stdout", |
| 108 | + "output_type": "stream", |
| 109 | + "text": [ |
| 110 | + "The prediction for inputs [1, 71, 0] for network 0 is 110.72\n", |
| 111 | + "The prediction for inputs [2, 101, 121] for network 0 is 110.71\n" |
| 112 | + ] |
| 113 | + } |
| 114 | + ], |
| 115 | + "source": [ |
| 116 | + "inputs = [[1, 71, 0], [2, 101, 121]]\n", |
| 117 | + "networks = [0, 0]\n", |
| 118 | + "\n", |
| 119 | + "predictions = predictor.predict(inputs, networks=networks)" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "metadata": {}, |
| 125 | + "source": [ |
| 126 | + "#### Step 4b: Or give the network a list of inputs to make predictions for and targets to continue learning from\n", |
| 127 | + "\n", |
| 128 | + "___Note:___ Once retrained, the weights are updated in the object immediately and cannot be undone, unless restored from a previously stored weights file." |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 5, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "name": "stdout", |
| 138 | + "output_type": "stream", |
| 139 | + "text": [ |
| 140 | + "The target for inputs [1, 71, 0] for network 0 was 106.53 and the prediction was 110.72, error observed was 3.93%. \n", |
| 141 | + "The target for inputs [2, 101, 121] for network 0 was 110.72 and the prediction was 115.86, error observed was 4.64%. \n" |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "inputs = [[1, 71, 0], [2, 101, 121]]\n", |
| 147 | + "networks = [0, 0]\n", |
| 148 | + "targets = [106.53, 110.72]\n", |
| 149 | + "\n", |
| 150 | + "predictions = predictor.predict(inputs, networks=networks, targets=targets, recal=True)" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "metadata": {}, |
| 156 | + "source": [ |
| 157 | + "The network is now retrained and has updated the weights.\n", |
| 158 | + "\n", |
| 159 | + "(Observe the difference in the second prediction because the weights were updated after predicting the first value.)" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "metadata": {}, |
| 165 | + "source": [ |
| 166 | + "#### Step 4c: Or give the network a CSV file of inputs, targets and networks to make predictions for and to continue learning from\n", |
| 167 | + "\n", |
| 168 | + "___Note:___ Once retrained, the weights are updated in the object immediately and cannot be undone, unless restored from a previously stored weights file." |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": 6, |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [ |
| 176 | + { |
| 177 | + "name": "stdout", |
| 178 | + "output_type": "stream", |
| 179 | + "text": [ |
| 180 | + "The target for inputs [1, 71, 0] for network 0 was 106.53 and the prediction was 115.19, error observed was 8.13%. \n", |
| 181 | + "The target for inputs [2, 101, 121] for network 0 was 114.81 and the prediction was 114.44, error observed was 0.32%. \n", |
| 182 | + "The target for inputs [3, 107, 116] for network 0 was 111.63 and the prediction was 113.52, error observed was 1.70%. \n", |
| 183 | + "The target for inputs [4, 24, 81] for network 0 was 111.85 and the prediction was 113.78, error observed was 1.72%. \n", |
| 184 | + "The target for inputs [5, -69, 39] for network 0 was 111.87 and the prediction was 113.83, error observed was 1.75%. \n" |
| 185 | + ] |
| 186 | + } |
| 187 | + ], |
| 188 | + "source": [ |
| 189 | + "inputs, networks, targets = read_input_file(INPUT_FILE)\n", |
| 190 | + "\n", |
| 191 | + "predictions = predictor.predict(inputs[:5], networks=networks[:5], targets=targets[:5], recal=True)" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "#### Step 5: Save the predictions to a CSV file\n", |
| 199 | + "\n", |
| 200 | + "Required parameters:\n", |
| 201 | + "- `targets`: As above.\n", |
| 202 | + "- `predictions`: As above.\n", |
| 203 | + "- `output_file`: Location on disk of the output file, with the name." |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": 7, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [], |
| 211 | + "source": [ |
| 212 | + "write_output_to_file(targets, predictions, os.path.join(DATA_LOCATION, 'predictions_2017.csv'))" |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "markdown", |
| 217 | + "metadata": {}, |
| 218 | + "source": [ |
| 219 | + "#### Step 6: Store the weights for future use\n", |
| 220 | + "\n", |
| 221 | + "Store the network state for future re-use." |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": 8, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [ |
| 230 | + "predictor.save(os.path.join(DATA_LOCATION, 'daily_rnn_retrained_17.pickle'))" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "markdown", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "Further documentation about other functions implemented is available within the source files. The shared variables and parameters are documented in `__init__.py`." |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "metadata": {}, |
| 244 | + "outputs": [], |
| 245 | + "source": [] |
| 246 | + } |
| 247 | + ], |
| 248 | + "metadata": { |
| 249 | + "kernelspec": { |
| 250 | + "display_name": "Python 3", |
| 251 | + "language": "python", |
| 252 | + "name": "python3" |
| 253 | + }, |
| 254 | + "language_info": { |
| 255 | + "codemirror_mode": { |
| 256 | + "name": "ipython", |
| 257 | + "version": 3 |
| 258 | + }, |
| 259 | + "file_extension": ".py", |
| 260 | + "mimetype": "text/x-python", |
| 261 | + "name": "python", |
| 262 | + "nbconvert_exporter": "python", |
| 263 | + "pygments_lexer": "ipython3", |
| 264 | + "version": "3.6.5" |
| 265 | + } |
| 266 | + }, |
4 | 267 | "nbformat": 4,
|
5 | 268 | "nbformat_minor": 2
|
6 | 269 | }
|
0 commit comments