In running and adapting this code I came across an issue.
https://people.xiph.org/~jm/demo/rnnoise/
Loading the saved model with
model = load_model("model.hdf5", custom_objects={'WeightClip': WeightClip,'mycost':mycost,'msse':msse})
Keras gives the error
__init__() got an unexpected keyword argument 'name'
This can be tracked down to /utils/generic_utils.py
# Then `cls` may be a function returning a class.
# in this case by convention `config` holds
# the kwargs of the function.
custom_objects = custom_objects or {}
with CustomObjectScope(custom_objects):
return cls(**config['config'])
Particularly config['config']['name'] exists, while the class (the constraint WeightClip class) does not expect such an argument. This was both rather un-intuitive behaviour of the serializer and confusing for an error message.
Simply adding *kwargs to my class's __init__ function fixed the issue for me.
Below is my best attempt at patching the issue such that Keras accepts the class without *kwargs and without breaking backward compatibility for anyone who does want to use the name field for some reason, but given my limited python skills I expect there is a better way.
# Then `cls` may be a function returning a class.
# in this case by convention `config` holds
# the kwargs of the function.
custom_objects = custom_objects or {}
with CustomObjectScope(custom_objects):
args, varargs, varkw, defaults = inspect.getargspec(cls) # stop the existence of config['name'] and such from causing errors
if varkw is not None or set(config['config']) <= set(args):
return cls(**config['config'])
else:
return cls(**{k: config[k] for k in set(args)&set(config['config'])})
When I make a class inherited keras.model.Model, I met the same problem.
I add **kwargs to my init function, and it works. thanks.
Adding *kwargs to the __init__ method doesn't solve for me. My custom layer's __init__ method has other arguments (seq and char_len), after adding *kwargs solves the 'name' error but now I get error "TypeError: __init__() missing 2 required positional arguments: 'seq' and 'char_len'"
I'm using keras v2.2.2
If you use custom args for your model, then implement get_config() method. It would help you save all necessary arguments with your model.
@liyuan1234 @eIGato @sander314
Hello, how do you solve this problem, I am also a custom layer, the following problems arise:
TypeError: init() missing 2 required positional arguments: 'pool_size' and 'num_rois'
my keras=2.2.0,,tensorflow=1.8.0
@zrx1046 show your code please.
@zrx1046 add a get_config function like this
```
def get_config(self):
config = {'pool_size': self.pool_size, 'num_rois': self.num_rois}
base_config = super(MyLayer, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
I have the similar error as:
__init__() got an unexpected keyword argument 'ragged'
Can someone help?
The command I am using is
model = load_model('Model1.h5')
I have the similar error as:
__init__() got an unexpected keyword argument 'ragged'Can someone help?
The command I am using is
model = load_model('Model1.h5')
@Aisha1214 load the model using:
from tensorflow.keras.models import load_model
instead of:
from keras.models import load_model
Most helpful comment
When I make a class inherited keras.model.Model, I met the same problem.
I add **kwargs to my init function, and it works. thanks.