在用Keras训练好模型后,加载时出现以下异常:
ValueError: Unknown loss function:sample_loss
没有找到自定义的损失函数。加载方法如下:
# 加载模型
self.model.set_weights(load_model(self.dirPath + str(self.fileName) + ".h5").get_weights())
需要调整加载代码,来让Keras找到自定义的模型
# 加载模型
self.model.set_weights(load_model(self.dirPath + str(self.fileName) + ".h5",custom_objects={"sample_loss":sample_loss}).get_weights())
这样,Keras就可以找到该函数了。