कस्टम loss functions

Python में Finance के लिए Machine Learning

Nathan George

Data Science Professor

दिशा का मेल नहीं

Python में Finance के लिए Machine Learning

दिशात्मक दंड के साथ MSE

यदि prediction और target की दिशा मिलती है:

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

यदि नहीं मिलती:

  • $\sum (y - \hat{y})^2 * \text{penalty} $
Python में Finance के लिए Machine Learning

कस्टम loss functions लागू करना

import tensorflow as tf
Python में Finance के लिए Machine Learning

फंक्शन बनाना

import tensorflow as tf

# create loss function def mean_squared_error(y_true, y_pred):
Python में Finance के लिए Machine Learning

Mean squared error loss

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)
Python में Finance के लिए Machine Learning

Keras में कस्टम loss जोड़ें

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)
Python में Finance के लिए Machine Learning

सही दिशा की जाँच

tf.less(y_true * y_pred, 0)

सही दिशा:

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

गलत दिशा:

  • neg * pos = neg
  • pos * neg = neg
Python में Finance के लिए Machine Learning

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))
Python में Finance के लिए Machine Learning

सबको जोड़ना

# 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
Python में Finance के लिए Machine Learning

कस्टम loss का उपयोग

# 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)
Python में Finance के लिए Machine Learning

बो-टाई आकार

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

बो-टाई प्लॉट

Python में Finance के लिए Machine Learning

अपना loss function बनाएँ!

Python में Finance के लिए Machine Learning

Preparing Video For Download...