TypeError:__init __()获得了意外的关键字参数"trainable" [英] TypeError: __init__() got an unexpected keyword argument 'trainable'

查看:136
本文介绍了TypeError:__init __()获得了意外的关键字参数"trainable"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用keras.models.model_from_json加载在Keras中训练的RNN模型架构,并且出现上述错误

I am trying to load a RNN model architecture trained in Keras using keras.models.model_from_json and I am getting the mentioned error

with open('model_architecture.json', 'r') as f:
    model = model_from_json(f.read(), custom_objects={'AttLayer':AttLayer})

# Load weights into the new model
model.load_weights('model_weights.h5')

这是我正在使用的自定义图层

Here is the custom layer I am using

class AttLayer(Layer):
    def __init__(self, attention_dim):
        self.init = initializers.get('normal')
        self.supports_masking = True
        self.attention_dim = attention_dim
        super(AttLayer, self).__init__()

    def build(self, input_shape):
        assert len(input_shape) == 3
        self.W = K.variable(self.init((input_shape[-1], self.attention_dim)))
        self.b = K.variable(self.init((self.attention_dim, )))
        self.u = K.variable(self.init((self.attention_dim, 1)))
        self.trainable_weights = [self.W, self.b, self.u]
        super(AttLayer, self).build(input_shape)

    def compute_mask(self, inputs, mask=None):
        return None

    def call(self, x, mask=None):
        # size of x :[batch_size, sel_len, attention_dim]
        # size of u :[batch_size, attention_dim]
        # uit = tanh(xW+b)
        uit = K.tanh(K.bias_add(K.dot(x, self.W), self.b))
        ait = K.dot(uit, self.u)
        ait = K.squeeze(ait, -1)

        ait = K.exp(ait)

        if mask is not None:
            # Cast the mask to floatX to avoid float64 upcasting in theano
            ait *= K.cast(mask, K.floatx())
        ait /= K.cast(K.sum(ait, axis=1, keepdims=True) + K.epsilon(), K.floatx())
        ait = K.expand_dims(ait)
        weighted_input = x * ait
        output = K.sum(weighted_input, axis=1)

        return output

    def compute_output_shape(self, input_shape):
        return (input_shape[0], input_shape[-1])

    def get_config(self):
        config = {'attention_dim': self.attention_dim}
        base_config = super(AttLayer, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

错误:

File "scripts/Classifier.py", line 254, in test
    model = model_from_json(f.read(), custom_objects={'AttLayer':AttLayer})
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/models.py", line 345, in model_from_json
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2489, in from_config
    process_layer(layer_data)
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2475, in process_layer
    custom_objects=custom_objects)
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/layers/wrappers.py", line 100, in from_config
    custom_objects=custom_objects)
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2489, in from_config
    process_layer(layer_data)
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2475, in process_layer
    custom_objects=custom_objects)
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 141, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/biswadip/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 1254, in from_config
    return cls(**config)
TypeError: __init__() got an unexpected keyword argument 'trainable'

版本:

Keras==2.0.8
tensorflow==1.4.1

我尝试使用不同的版本进行训练和加载,但是没有运气.最后,我从模型体系结构文件(model_architecture.json)中的自定义层详细信息中删除了可训练"和名称"(键值对),并且似乎正在加载模型而没有任何错误.但这似乎是一种解决方法,每次训练模型时我都必须这样做.

I tried training and loading using different versions, but with no luck. Finally I removed 'trainable' and 'name' (key value pairs)from my custom layer detail in the model architecture file(model_architecture.json) and model seems to be loading without any error. But this looks like a fix and I have to do this every time I train the model.

推荐答案

我认为您错过了图层定义中的一个小细节.图层的__init__方法应采用关键字参数(**kwargs),并将这些关键字参数传递给父类__init__,如下所示:

I think you missed a small detail in your layer definition. You layers' __init__ method should take keyword arguments (**kwargs) and you should pass these keyword arguments to the parent class __init__, like this:

class AttLayer(Layer):
    def __init__(self, attention_dim, **kwargs):
        self.init = initializers.get('normal')
        self.supports_masking = True
        self.attention_dim = attention_dim
        super(AttLayer, self).__init__(**kwargs)

通过这种方式,任何通用图层参数都可以正确地传递给父类(在您的情况下为trainable标志).

This way any generic layer parameter will be correctly passed to the parent class, in your case, the trainable flag.

这篇关于TypeError:__init __()获得了意外的关键字参数"trainable"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆