Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_model: __init__() got an unexpected keyword argument 'name' #8956

Closed
sander314 opened this issue Jan 4, 2018 · 11 comments
Closed

load_model: __init__() got an unexpected keyword argument 'name' #8956

sander314 opened this issue Jan 4, 2018 · 11 comments

Comments

@sander314
Copy link

sander314 commented Jan 4, 2018

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'])})
@awkrail
Copy link

awkrail commented Jun 29, 2018

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.

@liyuan1234
Copy link

liyuan1234 commented Jan 8, 2019

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

@eIGato
Copy link

eIGato commented Feb 25, 2019

If you use custom args for your model, then implement get_config() method. It would help you save all necessary arguments with your model.

@zrx1046
Copy link

zrx1046 commented Jun 20, 2019

@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

@eIGato
Copy link

eIGato commented Sep 3, 2019

@zrx1046 show your code please.

@shinleylee
Copy link

shinleylee commented Apr 1, 2020

@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()))

@Aisha1214
Copy link

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')

@thelmanizia
Copy link

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

@ksharm50
Copy link

i am using tensorflow version 1.11.0 some reason i am getting this issue even after using from tensorflow.keras.models import load_model

('Unrecognized keyword arguments:', dict_keys(['ragged'])): ValueError Traceback (most recent call last): File "/var/task/lambda_function.py", line 50, in lambda_handler load_model(local_path_model) File "/opt/python/tensorflow/python/keras/engine/saving.py", line 230, in load_model model = model_from_config(model_config, custom_objects=custom_objects) File "/opt/python/tensorflow/python/keras/engine/saving.py", line 310, in model_from_config return deserialize(config, custom_objects=custom_objects) File "/opt/python/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize printable_module_name='layer') File "/opt/python/tensorflow/python/keras/utils/generic_utils.py", line 173, in deserialize_keras_object list(custom_objects.items()))) File "/opt/python/tensorflow/python/keras/engine/sequential.py", line 339, in from_config custom_objects=custom_objects) File "/opt/python/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize printable_module_name='layer') File "/opt/python/tensorflow/python/keras/utils/generic_utils.py", line 175, in deserialize_keras_object return cls.from_config(config['config']) File "/opt/python/tensorflow/python/keras/engine/base_layer.py", line 1617, in from_config return cls(**config) File "/opt/python/tensorflow/python/keras/engine/input_layer.py", line 70, in __init__ raise ValueError('Unrecognized keyword arguments:', kwargs.keys()) ValueError: ('Unrecognized keyword arguments:', dict_keys(['ragged']))

@Lyn-666
Copy link

Lyn-666 commented Jan 1, 2023

@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

@liyuan1234 @eIGato @sander314 你好,请问这个问题是怎么解决的,我也是自定义层,出现以下问题: TypeError: init() 缺少 2 个必需的位置参数:'pool_size' 和 'num_rois' 我的 keras=2.2.0,,tensorflow=1.8.0

Hello, I met the same issue, Could you please share your solution? Thanks!

@Lyn-666
Copy link

Lyn-666 commented Jan 1, 2023

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

Hi, Could you please share your solution? I met the same issue. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests