Recently, I have attempted to create a stock market prediction program upon the basis of previously conducted work within the field, whereby a neural network, created via the Keras module in Python, is fed adjusted stock price information from Quandl, utilising the aforementioned information to train itself. I have completed this program via the utilisation of assistance from the following tutorial; however, I have modified the provided program, replacing the utilisation of the 'sklearn' linear module with a Keras Sequential model. The tutorial is listed below:
https://www.youtube.com/watch?v=EYnC4ACIt2g&t=1551s
I additionally derived the Keras Sequential model information from the official documentation for the Keras module:
I have completed the aforementioned within the Google Colaboratory program, an interpreter and online IDE for Python within the form of a Jupyter Notebook. I utilised the following code:
import tensorflow as tf
import keras
import numpy as np
import quandl
from sklearn.model_selection import train_test_split
df = quandl.get("WIKI/FB")
df = df[['Adj. Close']]
forecast_out = 1
df['Prediction'] = df[['Adj. Close']].shift(-(forecast_out))
X = np.array(df.drop(['Prediction'], 1))
X = X[:-forecast_out]
y = np.array(df['Prediction'])
y = y[:-forecast_out]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
model = keras.models.Sequential()
model.add(keras.layers.Dense(units = 64, activation = 'relu'))
model.add(keras.layers.Dense(units = 10, activation = 'softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32)
However, the Colaboratory compiler provided the following error message:
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3576: The name tf.log is deprecated. Please use tf.math.log instead.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-70cb958ae676> in <module>()
7 metrics=['accuracy'])
8
----> 9 model.fit(x_train, y_train, epochs=5, batch_size=32)
2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
139 ': expected ' + names[i] + ' to have shape ' +
140 str(shape) + ' but got array with shape ' +
--> 141 str(data_shape))
142 return data
143
ValueError: Error when checking target: expected dense_16 to have shape (10,) but got array with shape (1,)
Is there a valid explanation for this error and can it be resolved? If so, what is entailed? Is it necessary to alter the training data or the neural network? Thank you for your assistance.
In a neural network, your last layer (the output layer) should match the shape of the target (the y
). As I see it, you are trying to forecast stock prices (continuous target), so the shape should be (1,). Your final dense layer should be:
model.add(keras.layers.Dense(units = 1, activation = 'linear')
On top of that, you're not classifying, so your loss shouldn't be categorical_crossentropy
. It should be mean_absolute_error
, or the likes.
Lastly, it is good practice to explicitly declare the input_shape
in your first layer. That makes things easier (for us to help too).