Funzioni di loss personalizzate

Machine Learning per la finanza in Python

Nathan George

Data Science Professor

mancata corrispondenza di direzione

Machine Learning per la finanza in Python

MSE con penalità di direzione

Se predizione e target hanno la stessa direzione:

  • $ \sum(y - \hat{y})^2$

Se no:

  • $\sum (y - \hat{y})^2 * \text{penalty} $
Machine Learning per la finanza in Python

Implementare funzioni di loss personalizzate

import tensorflow as tf
Machine Learning per la finanza in Python

Creare una funzione

import tensorflow as tf

# create loss function def mean_squared_error(y_true, y_pred):
Machine Learning per la finanza in Python

Loss MSE (mean squared error)

import tensorflow as tf

# create loss function
def mean_squared_error(y_true, y_pred):

loss = tf.square(y_true - y_pred) return tf.reduce_mean(loss, axis=-1)
Machine Learning per la finanza in Python

Aggiungi la loss personalizzata a keras

import tensorflow as tf

# create loss function
def mean_squared_error(y_true, y_pred):
    loss = tf.square(y_true - y_pred)
    return tf.reduce_mean(loss, axis=-1)

# enable use of loss with keras import keras.losses keras.losses.mean_squared_error = mean_squared_error
# fit the model with our mse loss function
model.compile(optimizer='adam', loss=mean_squared_error)
history = model.fit(scaled_train_features, train_targets, epochs=50)
Machine Learning per la finanza in Python

Verificare la direzione corretta

tf.less(y_true * y_pred, 0)

Direzione corretta:

  • neg * neg = pos
  • pos * pos = pos

Direzione errata:

  • neg * pos = neg
  • pos * neg = neg
Machine Learning per la finanza in Python

Usare tf.where()

# create loss function
def sign_penalty(y_true, y_pred):
    penalty = 10.
    loss = tf.where(tf.less(y_true * y_pred, 0), 
                    penalty * tf.square(y_true - y_pred), 
                    tf.square(y_true - y_pred))
Machine Learning per la finanza in Python

Mettere tutto insieme

# create loss function
def sign_penalty(y_true, y_pred):
    penalty = 100.
    loss = tf.where(tf.less(y_true * y_pred, 0),
                    penalty * tf.square(y_true - y_pred), 
                    tf.square(y_true - y_pred))

    return tf.reduce_mean(loss, axis=-1)

# enable use of loss with keras keras.losses.sign_penalty = sign_penalty
Machine Learning per la finanza in Python

Usare la loss personalizzata

# create the model
model = Sequential()
model.add(Dense(50,
                input_dim=scaled_train_features.shape[1],
                activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='linear'))
# fit the model with our custom 'sign_penalty' loss function
model.compile(optimizer='adam', loss=sign_penalty)
history = model.fit(scaled_train_features, train_targets, epochs=50)
Machine Learning per la finanza in Python

La forma a farfalla

train_preds = model.predict(scaled_train_features)
# scatter the predictions vs actual
plt.scatter(train_preds, train_targets)
plt.xlabel('predictions')
plt.ylabel('actual')
plt.show()

grafico a farfalla

Machine Learning per la finanza in Python

Crea la tua funzione di loss!

Machine Learning per la finanza in Python

Preparing Video For Download...