Skip to content

Commit 923e025

Browse files
committed
Code files added
1 parent aa4800c commit 923e025

File tree

7 files changed

+3728
-0
lines changed

7 files changed

+3728
-0
lines changed

Lesson02/activity_3/Activity_3_Exploring_Bitcoin_Dataset.ipynb

Lines changed: 598 additions & 0 deletions
Large diffs are not rendered by default.

Lesson02/activity_3/data/bitcoin_historical_prices.csv

Lines changed: 1656 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
Series of normalization functions useful
3+
for normalizing time-series data.
4+
5+
Author: Luis Capelo
6+
"""
7+
def z_score(series):
8+
"""
9+
Computes the normalized value using the Z-score
10+
technique. The Z-score is a technique used for
11+
normalizing Gaussian distributions representing
12+
each observation in relation to the distribution's
13+
mean and standard deviation. For precise definitions,
14+
see the Wikipedia article:
15+
16+
https://en.wikipedia.org/wiki/Standard_score
17+
18+
Parameters
19+
----------
20+
serie: list
21+
List with sequential values to use.
22+
23+
Returns
24+
-------
25+
result: list
26+
List with the normalized results.
27+
"""
28+
result = (series - series.mean()) / series.std(ddof=0)
29+
return result
30+
31+
def point_relative_normalization(series):
32+
"""
33+
Computes the normalized value for the values of a
34+
given series by using the first element of the serie as p_0
35+
as a reference for each p_i.
36+
37+
This technique comes from Siraj Raval's YouTube video
38+
"How to Predict Stock Prices Easily - Intro to Deep Learning #7",
39+
available at:
40+
41+
https://www.youtube.com/watch?v=ftMq5ps503w
42+
43+
Parameters
44+
----------
45+
serie: list
46+
List with sequential values to use.
47+
48+
Returns
49+
-------
50+
result: list
51+
List with the normalized results.
52+
"""
53+
result = (series / series.values[0]) - 1
54+
return result
55+
56+
def maximum_and_minimum_normalization(series, boundary=(0, 1)):
57+
"""
58+
Computes the normalized value for the values of a
59+
given serie by using that series maximum and minimum
60+
values.
61+
62+
This technique is a direct implementation from
63+
scikit-learn, available at:
64+
65+
http://scikit-learn.org/stable/modules/generated/\
66+
sklearn.preprocessing.MinMaxScaler.html
67+
68+
Parameters
69+
----------
70+
serie: list
71+
List with sequential values to use.
72+
73+
boundary: set
74+
Maximum and minimum values used to
75+
scale the series.
76+
77+
Returns
78+
-------
79+
result: list
80+
List with the normalized results.
81+
"""
82+
range_min, range_max = boundary
83+
standard_deviation = (series - series.min(axis=0)) / (series.max(axis=0) - series.min(axis=0))
84+
result = standard_deviation * (range_max - range_min) + range_min
85+
86+
return result
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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

Comments
 (0)