Fungsi loss kustom

Machine Learning untuk Keuangan dengan Python

Nathan George

Data Science Professor

arah tidak cocok

Machine Learning untuk Keuangan dengan Python

MSE dengan penalti arah

Jika arah prediksi dan target cocok:

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

Jika tidak:

  • $\sum (y - \hat{y})^2 * \text{penalty} $
Machine Learning untuk Keuangan dengan Python

Menerapkan fungsi loss kustom

import tensorflow as tf
Machine Learning untuk Keuangan dengan Python

Membuat sebuah fungsi

import tensorflow as tf

# create loss function def mean_squared_error(y_true, y_pred):
Machine Learning untuk Keuangan dengan Python

Loss 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 untuk Keuangan dengan Python

Menambahkan loss kustom ke 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 untuk Keuangan dengan Python

Memeriksa arah yang benar

tf.less(y_true * y_pred, 0)

Arah benar:

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

Arah salah:

  • neg * pos = neg
  • pos * neg = neg
Machine Learning untuk Keuangan dengan Python

Menggunakan 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 untuk Keuangan dengan Python

Menggabungkan semuanya

# 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 untuk Keuangan dengan Python

Menggunakan loss kustom

# 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 untuk Keuangan dengan Python

Bentuk dasi kupu-kupu

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()

plot dasi kupu-kupu

Machine Learning untuk Keuangan dengan Python

Buat fungsi loss Anda sendiri!

Machine Learning untuk Keuangan dengan Python

Preparing Video For Download...