Tensorflow 在keras中,如何克隆具有自定义对象的模型?
我有一个自定义激活的模型。因此,Tensorflow 在keras中,如何克隆具有自定义对象的模型?,tensorflow,keras,Tensorflow,Keras,我有一个自定义激活的模型。因此, model2 = keras.models.clone_model(model) 给出了一个错误。我可以使用custom\u objects关键字加载保存的模型,但在clone\u模型上看不到这样的选项。除了重新制作模型和转移权重之外,还有其他方法吗 编辑: 下面是示例代码(玩具问题): 以及错误转储: ~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/models
model2 = keras.models.clone_model(model)
给出了一个错误。我可以使用custom\u objects关键字加载保存的模型,但在clone\u模型上看不到这样的选项。除了重新制作模型和转移权重之外,还有其他方法吗
编辑:
下面是示例代码(玩具问题):
以及错误转储:
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/models.py in clone_model(model, input_tensors)
269 return _clone_sequential_model(model, input_tensors=input_tensors)
270 else:
--> 271 return _clone_functional_model(model, input_tensors=input_tensors)
272
273
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/models.py in _clone_functional_model(model, input_tensors)
129 if layer not in layer_map:
130 # Clone layer.
--> 131 new_layer = layer.__class__.from_config(layer.get_config())
132 layer_map[layer] = new_layer
133 layer = new_layer
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in from_config(cls, config)
400 A layer instance.
401 """
--> 402 return cls(**config)
403
404 def compute_output_shape(self, input_shape):
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in __init__(self, units, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
920 activity_regularizer=regularizers.get(activity_regularizer), **kwargs)
921 self.units = int(units)
--> 922 self.activation = activations.get(activation)
923 self.use_bias = use_bias
924 self.kernel_initializer = initializers.get(kernel_initializer)
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/activations.py in get(identifier)
209 if isinstance(identifier, six.string_types):
210 identifier = str(identifier)
--> 211 return deserialize(identifier)
212 elif callable(identifier):
213 return identifier
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/activations.py in deserialize(name, custom_objects)
200 module_objects=globals(),
201 custom_objects=custom_objects,
--> 202 printable_module_name='activation function')
203
204
~/.conda/envs/tf-gpu/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
210 if fn is None:
211 raise ValueError('Unknown ' + printable_module_name + ':' +
--> 212 function_name)
213 return fn
214 else:
ValueError: Unknown activation function:myTanh
这是一个开放的凯拉斯
建议的解决方法是使用Lambda
层代替激活
层
x=keras.layers.Lambda(我的自定义激活函数)(x)
我通过调用
keras.utils.get_custom_objects().update(custom_objects)
在定义keras必须知道的其他对象之后,正确克隆模型
def lrelu(x, alpha=0.2):
return tf.nn.relu(x) * (1 - alpha) + x * alpha
custom_object = {
'lrelu': lrelu,
}
keras.utils.get_custom_objects().update(custom_objects)
你能提供一些你遇到的错误的代码和回溯吗?用一个导致错误的例子进行后期编辑。
def lrelu(x, alpha=0.2):
return tf.nn.relu(x) * (1 - alpha) + x * alpha
custom_object = {
'lrelu': lrelu,
}
keras.utils.get_custom_objects().update(custom_objects)