Hàm mất mát tùy chỉnh

Machine Learning cho Tài chính bằng Python

Nathan George

Data Science Professor

sai hướng

Machine Learning cho Tài chính bằng Python

MSE với phạt theo hướng

Nếu hướng của dự đoán và mục tiêu trùng nhau:

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

Nếu không:

  • $\sum (y - \hat{y})^2 * \text{penalty} $
Machine Learning cho Tài chính bằng Python

Cài đặt hàm mất mát tùy chỉnh

import tensorflow as tf
Machine Learning cho Tài chính bằng Python

Tạo một hàm

import tensorflow as tf

# create loss function def mean_squared_error(y_true, y_pred):
Machine Learning cho Tài chính bằng Python

Hàm mất mát MSE

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 cho Tài chính bằng Python

Thêm loss tùy chỉnh vào 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 cho Tài chính bằng Python

Kiểm tra hướng đúng

tf.less(y_true * y_pred, 0)

Đúng hướng:

  • âm * âm = dương
  • dương * dương = dương

Sai hướng:

  • âm * dương = âm
  • dương * âm = âm
Machine Learning cho Tài chính bằng Python

Dùng 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 cho Tài chính bằng Python

Ghép nối hoàn chỉnh

# 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 cho Tài chính bằng Python

Dùng loss tùy chỉnh

# 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 cho Tài chính bằng Python

Dáng hình nơ bướm

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

biểu đồ nơ bướm

Machine Learning cho Tài chính bằng Python

Tự tạo hàm mất mát của bạn!

Machine Learning cho Tài chính bằng Python

Preparing Video For Download...