Keras’s model defines metric or loss,
there is no problem when saving to H5, but when using load_ When importing the model, an error will be reported:
unknown metric function: HammingScore. Please ensure this object is passed to the
custom_ objects
argument.
This is because the custom parameters are not passed in. There are two solutions:
-
-
-
-
- if you only need to predict and no longer train, you can add
compile = false directly
-
-
-
model = keras.models.load_model('model.h5', compile = False)
If you need further training or modification, add your own metrics code and compile it again
model.compile(loss='binary_crossentropy',
optimizer=Ada,
metrics=[HammingScore]) # 这里HammingScore是我自定义的metric
-
-
- when importing, the user-defined metric/loss is passed into
-
Custom as a key value_ objects
-
-
- :
-
model = keras.models.load_model('model.h5', custom_objects={'HammingScore': HammingScore} )
Note that the key value should be consistent