无法加载具有未知自定义Lambda损失函数的keras模型

问题描述 投票:0回答:1

我的模特看起来像

[Keras中的单输出多重损失函数:https://stackoverflow.com/a/51705573/9079093

model = Model(inputs=[sketch_inp, color_inp], outputs=disc_outputs)

opt = Adam(lr=learning_rate, beta_1=.5)



model.compile(loss=lambda y_true, y_pred : tf.keras.losses.binary_crossentropy(y_true, y_pred) + \
                                                 pixelLevelLoss_weight * pixelLevelLoss(y_true, y_pred) + \
                                                 totalVariationLoss_weight * totalVariationLoss(y_true, y_pred) + \
                                                 featureLevelLoss_weight * featureLevelLoss(y_true, y_pred),\
                    optimizer=opt)

保存模型后,我想加载它并完成训练,但是我不知道如何使用此自定义损失函数加载它

python tensorflow keras loss
1个回答
0
投票

在加载模型时,只需使用cutom_objects参数传递损失。

如果要加载的模型包括自定义图层或其他自定义类或函数,则可以通过custom_objects参数将它们传递给加载机制:

from keras.models import load_model
# Assuming your model includes instance of an "AttentionLayer" class
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})
© www.soinside.com 2019 - 2024. All rights reserved.