tf.keras自定義損失函數


自定義損失函數

In statistics, the Huber loss is a loss function used in robust regression, that is less sensitive to outliers in data than the squared error loss. A variant for classification is also sometimes used.

def huber_fn(y_true, y_pred):
    error = y_true - y_pred
    is_small_error = tf.abs(error) < 1
    squared_loss = tf.square(error) / 2
    linear_loss  = tf.abs(error) - 0.5
    return tf.where(is_small_error, squared_loss, linear_loss)

注意,自定義損失函數的返回值是一個向量而不是損失平均值,每個元素對應一個實例。這樣的好處是Keras可以通過class_weightsample_weight調整權重。

huber_fn(y_valid, y_pred)
<tf.Tensor: id=4894, shape=(3870, 1), dtype=float64, numpy=
array([[0.10571115],
       [0.03953311],
       [0.02417886],
       ...,
       [0.00039475],
       [0.00245003],
       [0.12238744]])>

導入損失函數

model = keras.models.load_model("my_model_with_a_custom_loss.h5",
                                custom_objects={"huber_fn": huber_fn})

帶參數的自定義損失函數

def create_huber(threshold=1.0):
    def huber_fn(y_true, y_pred):
        error = y_true - y_pred
        is_small_error = tf.abs(error) < threshold
        squared_loss = tf.square(error) / 2
        linear_loss  = threshold * tf.abs(error) - threshold**2 / 2
        return tf.where(is_small_error, squared_loss, linear_loss)
    return huber_fn

model.compile(loss=create_huber(2.0), optimizer="nadam", metrics=["mae"])

導入模型的時候注意

model = keras.models.load_model("my_model_with_a_custom_loss_threshold_2.h5",
                                custom_objects={"huber_fn": create_huber(2.0)})

導入的是帶有參數的create_huber(2.0),而不是create_huber。如果想要保留參數設置,必須自定義


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM