Original article was published on Deep Learning on Medium
Stock Price Prediction : Using Deep Neural Networks on Keras
We all have heard that Deep Neural Networks can just do everything from classifying images to translating sentences. All of these problems are having one similarity, data doesn’t change with the time. Will it work when the data changes with time? Let’s find out!
This is our fourth entry in our series of blogs on Time-series Prediction. Link to the previous three:
Deep Neural Networks
Deep Neural Networks are the most fascinating discovery in the domain of Artificial Intelligence. These networks are loosely modeled after the human brain and designed to learn and recognize complex patterns and phew, it does an amazing job at that. Any real-time data which can be converted to numerical form can be fed into the network, ranging from Images, Sentences, Time Series, etc.
Something so good will be complex to code right?
When many people hear of the word Neural Networks they imagine big and complex code, which is wrong. Keras has made it possible to implement such complex models on a wide range of problems with ease. Let’s find out how.
model = Sequential()
model.add(Dense(100, activation='relu', input_shape=))
model.compile(loss="mse", optimizer=SGD(lr=1e-6, momentum=0.9))
Above is the architecture of our Neural Network. It has 100 neurons in the input layer followed by a dense layer of 10 neurons and at last the output layer having a single neuron. We are using ‘relu’ as the activation function for all our layers.
This isn’t a very deep architecture but we chose this because our dataset only contains 1000 entries, so a very deep model would easily overfit the data and we don’t want that!
Now we prepare the training data for our architecture. As covered in our previous post, The current value of the stock depends highly on the previous values of the stock. This is covered in detail in one of our previous posts, Statistical Approach to Stock Price Prediction: Naive Forecast, Moving Average.
To prepare the input array of the previous 7 day’s prices we use the following code,
def lag_features(df , N , col , col_index):
"adds lag features of the given columnb for previous N days"
series = df[col]
for i in range(N):
shifted_rows = np.zeros(len(df))
shifted_rows[:] = np.nan
array_index = i+1
for j in range(len(df)-(i+1)):
shifted_rows[array_index] = df.iloc[j,col_index]
array_index = array_index + 1
df[col+'_lag_'+str(i+1)] = shifted_rows
Neural Networks face a huge risk of over-fitting the datasets it is training on. To avoid that, we have decided to use 20% of our dataset as the validation data. Whenever during the training the validation accuracy keeps going down and training accuracy keeps going up, then that is bad cause the model has over-fitted!
early_stopping_monitor = EarlyStopping(patience=3)model.fit(trainX , trainY, validation_split=0.2, epochs=100, callbacks=[early_stopping_monitor] , verbose=1)
I am sure everything in the above code snippet must look very familiar to most of you except one thing, ‘early_stopping_monitor’. It keeps track of the accuracy values of the model and when the accuracy for the last n epochs isn’t changing it automatically stops the training. This is very helpful in saving resources and time.
Finally it is the time to test! Our test dataset has 100 entries separated from the dataset before it was sent to the model for training. We are using 3 metrics to determine the results,
- Mean Absolute Error (MAE)
- Mean Squared Error (MSE)
- Mean Absolute Percentage Error (MAPE)
Well, this goes without saying, less the error better the model.
We have successfully implemented stock price prediction using deep neural networks. The blog and code were compiled by Naitik Khandelwal.