具有自定义对象的Keras load_model无法正常工作

时间:2019-03-18 13:45:16

标签: python machine-learning keras loss-function

设置

正如标题中已经提到的那样,在尝试加载保存的模型时,我的自定义损失函数出现了问题。我的损失看起来如下:

def weighted_cross_entropy(weights):

    weights = K.variable(weights)

    def loss(y_true, y_pred):
        y_pred = K.clip(y_pred, K.epsilon(), 1-K.epsilon())

        loss = y_true * K.log(y_pred) * weights
        loss = -K.sum(loss, -1)
        return loss

    return loss

weightes_loss = weighted_cross_entropy([0.1,0.9])

因此,在训练期间,我使用weighted_loss函数作为损失函数,并且一切正常。训练完成后,我使用keras API中的标准.h5函数将模型另存为model.save文件。

问题

当我尝试通过加载模型时

model = load_model(path,custom_objects={"weighted_loss":weighted_loss})

我收到ValueError,告诉我损失未知。

错误

错误消息如下:

File "...\predict.py", line 29, in my_script
"weighted_loss": weighted_loss})
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 312, in _deserialize_model
sample_weight_mode=sample_weight_mode)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\training.py", line 139, in compile
loss_function = losses.get(loss)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 133, in get
return deserialize(identifier)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 114, in deserialize
printable_module_name='loss function')
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\utils\generic_utils.py", line 165, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:loss

问题

如何解决此问题?可能原因是我包装的损失定义吗?所以keras不知道如何处理weights变量?

1 个答案:

答案 0 :(得分:1)

您的损失函数的名称为loss(即def loss(y_true, y_pred):)。因此,在加载模型时,您需要指定'loss'作为其名称:

model = load_model(path, custom_objects={'loss': weighted_loss})