如何使用自定义损失加载模型?

时间:2020-01-29 11:23:11

标签: python tensorflow keras deep-learning tensorflow2.0

如何使用子类tf.keras.losses.Los的自定义损失加载模型?

我通过将tf.keras.losses.Loss子类化来定义ContrastiveLoss,如下所示:

import tensorflow as tf
from tensorflow.keras.losses import Loss

class ContrastiveLoss(Loss):
    def __init__(self, alpha, square=True, **kwargs):
        super(ContrastiveLoss, self).__init__(**kwargs)
        self.alpha = alpha
        self.square = square

    def get_dists(self, x, y, square):
        dists = tf.subtract(x, y)
        dists = tf.reduce_sum(tf.square(dists), axis=-1)

        if not square:
            zero_mask = tf.cast(tf.equal(dists, 0.0), tf.float32)
            dists = dists + zero_mask * 1e-16
            dists = tf.sqrt(dists)
            nonzero_mask = 1.0 - zero_mask
            dists = dists * nonzero_mask

        return dists

    def call(self, y_true, y_pred):
        # y_true & y_pred shape == (N, #embed), for N mini-batch
        # y_true[:, 0] == (N)
        if len(y_true.shape) == 2: y_true= y_true[:, 0]

        positive_mask = tf.cast(tf.equal( tf.expand_dims(y_true, 0), tf.expand_dims(y_true, 1) ), tf.float32)
        negative_mask = tf.subtract(1.0, positive_mask)   

        all_dists = self.get_dists(tf.expand_dims(y_pred, 1), tf.expand_dims(y_pred, 0), self.square)
        positive_loss = tf.multiply( positive_mask, all_dists )
        negative_loss = tf.multiply( negative_mask, tf.maximum(tf.subtract(self.alpha, all_dists), 0.) )
        contrastive_loss = tf.add( positive_loss, negative_loss )

        valid_doublet_mask = tf.cast( tf.greater(contrastive_loss, 1e-16), tf.float32)
        num_valid_doublet = tf.reduce_sum(valid_doublet_mask)
        contrastive_loss  = tf.reduce_sum( contrastive_loss ) / (num_valid_doublet + 1e-16)       

        return contrastive_loss 

    def get_config(self):
        config = super(ContrastiveLoss, self).get_config()
        config.update({'alpha' : self.alpha, 
                       'square' : self.square})
        return config  

我可以用它训练和保存模型。

但是,当我按如下方式加载模型时,会收到错误消息。

load_model(model_path, custom_objects={'ContrastiveLoss' : ContrastiveLoss})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-af42cd2404e1> in <module>()
----> 1 load_model(model_path, custom_objects={'ContrastiveLoss' : ContrastiveLoss})

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
    148   if isinstance(filepath, six.string_types):
    149     loader_impl.parse_saved_model(filepath)
--> 150     return saved_model_load.load(filepath, compile)
    151 
    152   raise IOError(

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saved_model/load.py in load(path, compile)
     97     if training_config is not None:
     98       model.compile(**saving_utils.compile_args_from_training_config(
---> 99           training_config))
    100   # pylint: disable=protected-access
    101 

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/saving/saving_utils.py in compile_args_from_training_config(training_config, custom_objects)
    232   loss_config = training_config['loss']  # Deserialize loss class.
    233   if isinstance(loss_config, dict) and 'class_name' in loss_config:
--> 234     loss_config = losses.get(loss_config)
    235   loss = nest.map_structure(
    236       lambda obj: custom_objects.get(obj, obj), loss_config)

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/losses.py in get(identifier)
   1184     return deserialize(identifier)
   1185   if isinstance(identifier, dict):
-> 1186     return deserialize(identifier)
   1187   elif callable(identifier):
   1188     return identifier

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/losses.py in deserialize(name, custom_objects)
   1173       module_objects=globals(),
   1174       custom_objects=custom_objects,
-> 1175       printable_module_name='loss function')
   1176 
   1177 

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    290     config = identifier
    291     (cls, cls_config) = class_and_config_for_serialized_keras_object(
--> 292         config, module_objects, custom_objects, printable_module_name)
    293 
    294     if hasattr(cls, 'from_config'):

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
    248     cls = module_objects.get(class_name)
    249     if cls is None:
--> 250       raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
    251 
    252   cls_config = config['config']

ValueError: Unknown loss function: ContrastiveLoss

奇怪的是,如果我使用自定义损失“函数”,则在load_model(。)期间没有错误

但是在这种情况下,使用Loss的“子类”会发生错误。

Complete code

1 个答案:

答案 0 :(得分:3)

如果javad建议

您是否尝试过使用对象而不是类名,这意味着0 : match '0' (?= : begin a positive lookahead .{0,2} : match 0-2 characters (?:$|1) : match the end of the string or '1' ) : end positive lookahead ,其中var a = [6, 1, 5, 9]; function find() { let x = 5; for (var i = 0; i < a.length; i++) { document.getElementById("out").innerHTML += a[i]; if (x == a[i]) { for (var f = i; f < a.length; f++) { b.push(a[f]) alert[f] var index = a.indexOf(a[i]) a.splice(index) } } } } find(a); 具有损失的所有参数,例如load_model(model_path, custom_objects={'ContrastiveLoss' : ContrastiveLoss(...)}),...?

不起作用,您只想进行推断,然后尝试使用:

...

希望有帮助。