LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Machine Learning for Finance in Python

Kurs anzeigen

Anleitung zur Übung

  • 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).

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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 bearbeiten und ausführen