Custom loss function
Up to now, we've used the mean squared error as a loss function. This works fine, but with stock price prediction it can be useful to implement a custom loss function. A custom loss function can help improve our model's performance in specific ways we choose. For example, we're going to create a custom loss function with a large penalty for predicting price movements in the wrong direction. This will help our net learn to at least predict price movements in the correct direction.
To do this, we need to write a function that takes arguments of (y_true, y_predicted)
. We'll also use functionality from the backend keras
(using tensorflow
) to find cases where the true value and prediction don't match signs, then penalize those cases.
This exercise is part of the course
Machine Learning for Finance in Python
Exercise instructions
- Set the arguments of the
sign_penalty()
function to bey_true
andy_pred
. - Multiply the squared error (
tf.square(y_true - y_pred)
) bypenalty
when the signs ofy_true
andy_pred
are different. - Return the average of the
loss
variable from the function -- this is the mean squared error (with our penalty for opposite signs of actual vs predictions).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import keras.losses
import tensorflow as tf
# Create loss function
def sign_penalty(____, ____):
penalty = 100.
loss = tf.where(tf.less(y_true * y_pred, 0), \
____ * tf.square(y_true - y_pred), \
tf.square(y_true - y_pred))
return tf.reduce_mean(____, axis=-1)
keras.losses.sign_penalty = sign_penalty # enable use of loss with keras
print(keras.losses.sign_penalty)