MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Machine Learning for Finance in Python

Lihat Kursus

Petunjuk latihan

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

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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)
Edit dan Jalankan Kode