gpt4 book ai didi

python - 类型错误 : __init__() got an unexpected keyword argument 'trainable'

转载 作者:行者123 更新时间:2023-12-03 18:45:28 32 4
gpt4 key购买 nike

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

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

这是我正在使用的自定义图层
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) 中的自定义层详细信息中删除了“可训练”和“名称”(键值对),并且模型似乎正在加载而没有任何错误。但这看起来像是一种修复,每次训练模型时我都必须这样做。

最佳答案

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

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 标志。

关于python - 类型错误 : __init__() got an unexpected keyword argument 'trainable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53098566/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com