我使用了一个自己创建的激活函数(通常不是),并用于我的LSTM。一切顺利,我训练了我的模型并将其保存为.h5
文件。
以下是我的自定义激活函数:
from keras import backend as k
def activate(ab):
a = k.exp(ab[:, 0])
b = k.softplus(ab[:, 1])
a = k.reshape(a, (k.shape(a)[0], 1))
b = k.reshape(b, (k.shape(b)[0], 1))
return k.concatenate((a, b), axis=1)
def weibull_loglik_discrete(y_true, ab_pred, name=None):
y_ = y_true[:, 0]
u_ = y_true[:, 1]
a_ = ab_pred[:, 0]
b_ = ab_pred[:, 1]
hazard0 = k.pow((y_ + 1e-35) / a_, b_)
hazard1 = k.pow((y_ + 1) / a_, b_)
return -1 * k.mean(u_ * k.log(k.exp(hazard1 - hazard0) - 1.0) - hazard1)
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(max_time, 39)))
model.add(LSTM(20, input_dim=11))
model.add(Dense(2))
# Apply the custom activation function mentioned above
model.add(Activation(activate))
# discrete log-likelihood for Weibull survival data as my loss function
model.compile(loss=weibull_loglik_discrete, optimizer=RMSprop(lr=.001))
# Fit!
model.fit(train_x, train_y, nb_epoch=250, batch_size=2000, verbose=2, validation_data=(test_x, test_y))
字符串
在训练之后,我保存我的模型如下:
from keras.models import load_model
model.save("model_baseline_lstm.h5")
型
稍后,当我尝试加载模型时,我运行以下命令:
from keras.models import load_model
model= load_model("model_baseline_lstm.h5")
型
但是,我得到了这个错误:
--------------------------------------------------------------------------- ValueError
Traceback (most recent call last) <ipython-input-11-d3f9f7415b5c> in <module>()
13 # model.save("model_baseline_lsm.h5")
14 from keras.models import load_model
---> 15 model= load_model("model_baseline_lsm.h5")
/anaconda3/lib/python3.6/site-packages/keras/models.py in load_model(filepath, custom_objects, compile)
238 raise ValueError('No model found in config file.')
239 model_config = json.loads(model_config.decode('utf-8'))
--> 240 model = model_from_config(model_config, custom_objects=custom_objects)
241
242 # set weights
/anaconda3/lib/python3.6/site-packages/keras/models.py in model_from_config(config, custom_objects)
312 'Maybe you meant to use '
313 '`Sequential.from_config(config)`?')
--> 314 return layer_module.deserialize(config, custom_objects=custom_objects)
315
316
/anaconda3/lib/python3.6/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
53 module_objects=globs,
54 custom_objects=custom_objects,
---> 55 printable_module_name='layer')
/anaconda3/lib/python3.6/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
138 return cls.from_config(config['config'],
139 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 140 list(custom_objects.items())))
141 with CustomObjectScope(custom_objects):
142 return cls.from_config(config['config'])
/anaconda3/lib/python3.6/site-packages/keras/models.py in from_config(cls, config, custom_objects)
1321 model = cls()
1322 for conf in config:
-> 1323 layer = layer_module.deserialize(conf, custom_objects=custom_objects)
1324 model.add(layer)
1325 return model
/anaconda3/lib/python3.6/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
53 module_objects=globs,
54 custom_objects=custom_objects,
---> 55 printable_module_name='layer')
/anaconda3/lib/python3.6/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
140 list(custom_objects.items())))
141 with CustomObjectScope(custom_objects):
--> 142 return cls.from_config(config['config'])
143 else:
144 # Then `cls` may be a function returning a class.
/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in from_config(cls, config)
1251 A layer instance.
1252 """
-> 1253 return cls(**config)
1254
1255 def count_params(self):
/anaconda3/lib/python3.6/site-packages/keras/layers/core.py in
__init__(self, activation, **kwargs)
289 super(Activation, self).__init__(**kwargs)
290 self.supports_masking = True
--> 291 self.activation = activations.get(activation)
292
293 def call(self, inputs):
/anaconda3/lib/python3.6/site-packages/keras/activations.py in get(identifier)
93 if isinstance(identifier, six.string_types):
94 identifier = str(identifier)
---> 95 return deserialize(identifier)
96 elif callable(identifier):
97 if isinstance(identifier, Layer):
/anaconda3/lib/python3.6/site-packages/keras/activations.py in deserialize(name, custom_objects)
85 module_objects=globals(),
86 custom_objects=custom_objects,
---> 87 printable_module_name='activation function')
88
89
/anaconda3/lib/python3.6/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
158 if fn is None:
159 raise ValueError('Unknown ' + printable_module_name +
--> 160 ':' + function_name)
161 return fn
162 else:
ValueError: Unknown activation function:activate
型
2条答案
按热度按时间mcdcgff01#
我想分享一下我是如何解决这个问题的:
字符串
模式如下:
型
希望这对你有帮助:)
b1zrtrql2#
在我的情况下,我有一个类似的错误保存和加载权重与自定义激活功能,工作良好的培训,并需要使用装饰器
字符串
在本页https://keras.io/guides/serialization_and_saving/中记录