Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Machine Learning for Finance in Python

Cursus bekijken

Oefeninstructies

  • Set the arguments of the sign_penalty() function to be y_true and y_pred.
  • Multiply the squared error (tf.square(y_true - y_pred)) by penalty when the signs of y_true and y_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).

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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)
Code bewerken en uitvoeren