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
Read More:
- How to Solve keras load_model() Error
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged
- [Solved] AttributeError: module ‘tensorflow._api.v2.train‘ has no attribute ‘AdampOptimizer‘
- urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>
- [Solved] AttributeError: Manager isn‘t available; ‘auth.User‘ has been swapped for ‘
- Pytorch Loading model error: RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- [Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘
- [Solved] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter st
- [Solved] AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- [Solved] RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- [Solved] AttributeError: ‘_IncompatibleKeys’ object has no attribute
- [Solved] HTTPError: 404 Client Error: Not Found for url: https://huggingface.co/saved_model
- [Solved] Error(s) in loading state_dict for GeneratorResNet
- Python RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 1, 5, 5]
- pytorch RuntimeError: Error(s) in loading state_ Dict for dataparall… Import model error solution
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- AttributeError: lower not found (NLP extracted tfidf features)
- [Solved] torchsummary Error: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.F
- How to Solve word2vec Module Error: AttributeError & UnicodeDecodeError