|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Activity 4: Creating a TensorFlow Model using Keras\n", |
| 8 | + "In this notebook we design and compile a deep learning model using Keras as an interface to TensorFlow. We will continue to modify this model in our next lessons and activities by experimenting with different optimization techniques. However, the essential components of the model are entirely designed in this notebook." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 1, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [ |
| 16 | + { |
| 17 | + "name": "stderr", |
| 18 | + "output_type": "stream", |
| 19 | + "text": [ |
| 20 | + "Using TensorFlow backend.\n", |
| 21 | + "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n", |
| 22 | + " return f(*args, **kwds)\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "from keras.models import Sequential\n", |
| 28 | + "from keras.layers.recurrent import LSTM\n", |
| 29 | + "from keras.layers.core import Dense, Activation" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "### Building a Model" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "Our dataset contains daily observations and each observation influences a future observation. Also, we are interested in predicting a week--that is, seven days--of Bitcoin prices in the future. For those reasons, we chose the parameters `period_length` and `number_of_observations` as follows:\n", |
| 44 | + "\n", |
| 45 | + "* `period_length`: the size of the period to use as training input. Our periods are organized in distinct weeks. We will be using a 7-day period to predict a week in the future.\n", |
| 46 | + "* `number_of_observations`: how many distinct periods does our dataset has? We hvae 77 weeks available in our dataset, given that we will be using the very last week to test the LSTM network on every epoch, we will use 77 - 1 = 76 periods for training it." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 2, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "period_length = 7\n", |
| 56 | + "number_of_periods = 76" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "We now build our LSTM model. " |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": 3, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "def build_model(period_length, number_of_periods, batch_size=1):\n", |
| 73 | + " \"\"\"\n", |
| 74 | + " Builds an LSTM model using Keras. This function\n", |
| 75 | + " works as a simple wrapper for a manually created\n", |
| 76 | + " model.\n", |
| 77 | + " \n", |
| 78 | + " Parameters\n", |
| 79 | + " ----------\n", |
| 80 | + " period_length: int\n", |
| 81 | + " The size of each observation used as input.\n", |
| 82 | + " \n", |
| 83 | + " number_of_periods: int\n", |
| 84 | + " The number of periods available in the \n", |
| 85 | + " dataset.\n", |
| 86 | + " \n", |
| 87 | + " batch_size: int\n", |
| 88 | + " The size of the batch used in each training\n", |
| 89 | + " period.\n", |
| 90 | + " \n", |
| 91 | + " Returns\n", |
| 92 | + " -------\n", |
| 93 | + " model: Keras model\n", |
| 94 | + " Compiled Keras model that can be trained\n", |
| 95 | + " and stored in disk.\n", |
| 96 | + " \"\"\"\n", |
| 97 | + " model = Sequential()\n", |
| 98 | + " model.add(LSTM(\n", |
| 99 | + " units=period_length,\n", |
| 100 | + " batch_input_shape=(batch_size, number_of_periods, period_length),\n", |
| 101 | + " input_shape=(number_of_periods, period_length),\n", |
| 102 | + " return_sequences=False, stateful=False))\n", |
| 103 | + "\n", |
| 104 | + " model.add(Dense(units=period_length))\n", |
| 105 | + " model.add(Activation(\"linear\"))\n", |
| 106 | + "\n", |
| 107 | + " model.compile(loss=\"mse\", optimizer=\"rmsprop\")\n", |
| 108 | + "\n", |
| 109 | + " return model" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "### Saving Model" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "We can use the function `build_model()` as a starting point for building our model. That function will be refactored when building our Flask application for making it easier to train the network and use it for predictions. For now, let's store the model output on disk." |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 4, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "model = build_model(period_length=period_length, number_of_periods=number_of_periods)" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": 6, |
| 138 | + "metadata": {}, |
| 139 | + "outputs": [], |
| 140 | + "source": [ |
| 141 | + "model.save('bitcoin_lstm_v0.h5')" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "markdown", |
| 146 | + "metadata": {}, |
| 147 | + "source": [ |
| 148 | + "The steps above compile the LSTM model as TensorFlow computation graph. We can now train that model using our train and evaluate its results with our test set." |
| 149 | + ] |
| 150 | + } |
| 151 | + ], |
| 152 | + "metadata": { |
| 153 | + "kernelspec": { |
| 154 | + "display_name": "Python 3", |
| 155 | + "language": "python", |
| 156 | + "name": "python3" |
| 157 | + }, |
| 158 | + "language_info": { |
| 159 | + "codemirror_mode": { |
| 160 | + "name": "ipython", |
| 161 | + "version": 3 |
| 162 | + }, |
| 163 | + "file_extension": ".py", |
| 164 | + "mimetype": "text/x-python", |
| 165 | + "name": "python", |
| 166 | + "nbconvert_exporter": "python", |
| 167 | + "pygments_lexer": "ipython3", |
| 168 | + "version": "3.6.3" |
| 169 | + } |
| 170 | + }, |
| 171 | + "nbformat": 4, |
| 172 | + "nbformat_minor": 2 |
| 173 | +} |
0 commit comments