『開發技巧』Keras自定義對象(層、評價函數與損失)


1.自定義層

對於簡單、無狀態的自定義操作,你也許可以通過 layers.core.Lambda 層來實現。但是對於那些包含了可訓練權重的自定義層,你應該自己實現這種層。

這是一個 Keras2.0 中,Keras 層的骨架(如果你用的是舊的版本,請更新到新版)。你只需要實現三個方法即可:

  • build(input_shape): 這是你定義權重的地方。這個方法必須設 self.built = True,可以通過調用 super([Layer], self).build() 完成。
  • call(x): 這里是編寫層的功能邏輯的地方。你只需要關注傳入 call 的第一個參數:輸入張量,除非你希望你的層支持masking。
  • compute_output_shape(input_shape): 如果你的層更改了輸入張量的形狀,你應該在這里定義形狀變化的邏輯,這讓Keras能夠自動推斷各層的形狀。
from keras import backend as K
from keras.engine.topology import Layer

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # 為該層創建一個可訓練的權重
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(input_shape[1], self.output_dim),
                                      initializer='uniform',
                                      trainable=True)
        super(MyLayer, self).build(input_shape)  # 一定要在最后調用它

    def call(self, x):
        return K.dot(x, self.kernel)

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim)

還可以定義具有多個輸入張量和多個輸出張量的 Keras 層。 為此,你應該假設方法 build(input_shape)call(x) 和 compute_output_shape(input_shape) 的輸入輸出都是列表。 這里是一個例子,與上面那個相似:

from keras import backend as K
from keras.engine.topology import Layer

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        assert isinstance(input_shape, list)
        # 為該層創建一個可訓練的權重
        self.kernel = self.add_weight(name='kernel',
                                      shape=(input_shape[0][1], self.output_dim),
                                      initializer='uniform',
                                      trainable=True)
        super(MyLayer, self).build(input_shape)  # 一定要在最后調用它

    def call(self, x):
        assert isinstance(x, list)
        a, b = x
        return [K.dot(a, self.kernel) + b, K.mean(b, axis=-1)]

    def compute_output_shape(self, input_shape):
        assert isinstance(input_shape, list)
        shape_a, shape_b = input_shape
        return [(shape_a[0], self.output_dim), shape_b[:-1]]

已有的 Keras 層就是實現任何層的很好例子。不要猶豫閱讀源碼!

2.自定義評價函數

自定義評價函數應該在編譯的時候(compile)傳遞進去。該函數需要以 (y_true, y_pred) 作為輸入參數,並返回一個張量作為輸出結果。

import keras.backend as K

def mean_pred(y_true, y_pred):
    return K.mean(y_pred)

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy', mean_pred])

3.自定義損失函數

自定義損失函數也應該在編譯的時候(compile)傳遞進去。該函數需要以 (y_true, y_pred) 作為輸入參數,並返回一個張量作為輸出結果。

import keras.backend as K

def my_loss(y_true, y_pred):
    return K.mean(K.squre(y_pred-y_true))#以平方差舉例

model.compile(optimizer='rmsprop',
              loss=my_loss,
              metrics=['accuracy'])

4.處理已保存模型中的自定義層(或其他自定義對象)

如果要加載的模型包含自定義層或其他自定義類或函數,則可以通過 custom_objects 參數將它們傳遞給加載機制:

from keras.models import load_model
# 假設你的模型包含一個 AttentionLayer 類的實例
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})

或者,你可以使用 自定義對象作用域

from keras.utils import CustomObjectScope

with CustomObjectScope({'AttentionLayer': AttentionLayer}):
    model = load_model('my_model.h5')

 


免責聲明!

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



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