On Deep Machine Learning & Time Series Models: A Case Study With The Use of Keras
On Deep Machine Learning & Time Series Models: A Case Study With The Use of Keras
On Deep Machine Learning & Time Series Models: A Case Study With The Use of Keras
net/publication/325170683
On deep machine learning & time series models: A case study with the use of
Keras
CITATIONS READS
0 5,599
1 author:
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Carlin Chun-fai Chu on 16 May 2018.
1
Content
• Why deep learning ?
• What is LSTM ?
• What is Keras ? Characteristics of Keras
• Suggested steps for LSTM coding
• Example codes
• Bollerslev's time series model
• Work-in-progress
2
Why deep learning ?
• Deep learning
– Artificial Neural Networks with > 1 hidden layer
– Involves a lot of data for training
– Different level of abstractions
4
Akita et al. (2016)
News (Textual) + Stock Price (Numerical)
• Deep Learning for Stock Prediction Using Numerical and Textual Information (Akita et al. 2016)
5
What is LSTM ?
Traditional Artificial Neural Network (ANN)
• no notion of time ordering
• map the current input feature(s) to the predicted target variable(s)
A short review can be found on ‘A Beginner’s Guide to Recurrent Networks and LSTMs’
• https://deeplearning4j.org/lstm.html
6
Characteristics of Keras
• high-level neural networks API, written in Python
7
Backend: Theano or TensorFlow ?
8
Suggested steps for LSTM coding
1. Normalize the data (Transformation)
2. Data preparation to a 3D dataset
3. Model specification
4. Model training (tackle over-fit issue)
5. Prediction
6. Inverse transformation
9
Suggested steps for LSTM coding (1)
10
Suggested steps for LSTM coding (2)
Time series
data a) Padding original data series with duplicated/repeated values
3D dataset
11
Suggested steps for LSTM coding (2)
12
Suggested steps for LSTM coding (3)
• Model specification
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
13
Suggested steps for LSTM coding (4 & 5)
• Prediction
# Model prediction
testPredict = model.predict(testX)
14
Suggested steps for LSTM coding (6)
• Inverse transformation
# inverse transformation
testPredict = scaler.inverse_transform(testPredict)
15
Observations
16
import numpy # Version 2: correct usage
input_data=numpy.array([[1.0], [2.0], [3.0], [4],[5],[6],[7],[8],[9]]) model_v2 = Sequential()
model_v2.add(LSTM(4, input_dim=1))
#%% Step 1: normalize the dataset model_v2.add(Dense(1))
import matplotlib.pyplot as plt model_v2.compile(loss='mean_squared_error', optimizer=‘sgd')
from sklearn.preprocessing import MinMaxScaler
#%% Step 4: Model training
scaler = MinMaxScaler(feature_range=(-1, 1)) # Model training (without validation dataset)
normalized_data = scaler.fit_transform(input_data) model_v1.fit(trainX_3D_v1, trainY, nb_epoch=200, batch_size=100, verbose=2)
plt.plot(normalized_data) model_v2.fit(trainX_3D_v2, trainY, nb_epoch=200, batch_size=100, verbose=2)
17
Example code of time series modeling using Keras (1)
18
# LSTM for international airline passengers problem with window regression framing # make predictions
import numpy trainPredict = model.predict(trainX)
import matplotlib.pyplot as plt testPredict = model.predict(testX)
import pandas
# invert predictions
import math
from keras.models import Sequential trainPredict = scaler.inverse_transform(trainPredict)
from keras.layers import Dense trainY = scaler.inverse_transform([trainY])
from keras.layers import LSTM testPredict = scaler.inverse_transform(testPredict)
from sklearn.preprocessing import MinMaxScaler testY = scaler.inverse_transform([testY])
from sklearn.metrics import mean_squared_error
# calculate root mean squared error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1): trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
dataX, dataY = [], [] print('Train Score: %.2f RMSE' % (trainScore))
for i in range(len(dataset)-look_back-1): testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
a = dataset[i:(i+look_back), 0] print('Test Score: %.2f RMSE' % (testScore))
dataX.append(a)
# shift train predictions for plotting
dataY.append(dataset[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY) trainPredictPlot = numpy.empty_like(dataset)
# fix random seed for reproducibility trainPredictPlot[:, :] = numpy.nan
numpy.random.seed(7) trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# load the dataset # shift test predictions for plotting
dataframe = pandas.read_csv('international-airline-passengers.csv', usecols=[1],
testPredictPlot = numpy.empty_like(dataset)
engine='python', skipfooter=3)
dataset = dataframe.values testPredictPlot[:, :] = numpy.nan
dataset = dataset.astype('float32') testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# normalize the dataset # plot baseline and predictions
scaler = MinMaxScaler(feature_range=(0, 1)) plt.plot(scaler.inverse_transform(dataset))
dataset = scaler.fit_transform(dataset)
plt.plot(trainPredictPlot)
# split into train and test sets
train_size = int(len(dataset) * 0.67) plt.plot(testPredictPlot)
test_size = len(dataset) - train_size plt.show()
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_dim=look_back))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=100, batch_size=1, verbose=2)
http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/ 19
Ref: http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-
networks-python-keras/ 20
Example code of time series modeling using Keras (2)
21
# define the raw dataset # %% create and fit the model
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" model1 = Sequential()
# create mapping of characters to integers (0-25) and the reverse model1.add(LSTM(32, input_shape=(X1.shape[1], X1.shape[2])))
char_to_int = dict((c, i) for i, c in enumerate(alphabet)) model1.add(Dense(y1.shape[1], activation='softmax'))
int_to_char = dict((i, c) for i, c in enumerate(alphabet)) model1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
http://machinelearningmastery.com/understanding-stateful-lstm-recurrent-neural-networks-python-keras/ 22
23
Example code of time series modeling using Keras (3)
24
https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent 25
26
Suggested setting for LSTM Hyperparameter
Tuning
• For LSTMs, use the softsign activation function over tanh (it’s faster and
less prone to saturation (vanishing gradient) (~0 gradients)).
• https://deeplearning4j.org/lstm.html
27
Bollerslev, Patton and Quaedvlieg (2016)
28
Work in progress
Long Short Term Memory network (LSTM)
• Adaptive forget gate, throw away information
• Keep information with time gaps of unknown/different size(s)
Investigation:
• Possible to extract features from different time horizons ?
– Daily, Weekly, Monthly, Intraday
• Model structure ?
– Number of layers ? Activation functions
• How to prevent over-fitting ?
– Types of loss function
• What types of information can be used ?
– Numerical, News, Comment from Social network
29
Make use of different types of information ?
Stock market
• Price
• Volatility
Personal blogs • Volume Economic factors
• Multimedia • CPI
commentary • GDP
• Retail sales
32
Reference
• A Beginner's Guide to Recurrent Networks and LSTMs - Deeplearning4j: Open-source, Distributed Deep Learning for the JVM
• https://deeplearning4j.org/lstm.html
• Time Series Prediction with LSTM Recurrent Neural Networks in Python with Keras (by Jason Brownlee)
• http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
• Understanding Stateful LSTM Recurrent Neural Networks in Python with Keras (by Jason Brownlee)
• http://machinelearningmastery.com/understanding-stateful-lstm-recurrent-neural-networks-python-keras/
• Keras recurrent tutorial
• https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent
• Deep learning Wikipedia
• https://en.wikipedia.org/wiki/Deep_learning
• What Is The Difference Between Deep Learning, Machine Learning and AI?
• https://www.forbes.com/sites/bernardmarr/2016/12/08/what-is-the-difference-between-deep-learning-machine-learning-and-
ai/#496301bc26cf
• What is Deep Learning? (by Jason Brownlee)
• http://machinelearningmastery.com/what-is-deep-learning/
• Do you recommend using Theano or Tensor Flow as Keras' backend? - Quora
• https://www.quora.com/Do-you-recommend-using-Theano-or-Tensor-Flow-as-Keras-backend
• Backend - Keras Documentation
• https://keras.io/backend/
• Ill-Conditioning in Neural Networks
• ftp://ftp.sas.com/pub/neural/illcond/illcond.html
• Understanding LSTM Networks
• http://colah.github.io/posts/2015-08-Understanding-LSTMs/
• Getting started with the Keras Sequential model
• https://keras.io/getting-started/sequential-model-guide/
• Tim Bollerslev, Andrew J. Patton, Rogier Quaedvlieg (2016) Exploiting the errors: A simple approach for improved volatility forecasting,
Journal of Econometrics, Volume 192, Issue 1, 2016, Pages 1-18
33
View publication stats