I have used an activation function that I have created on my own (not usually) and I used for my LSTM. Everything went well, I trained my model and saved it as .h5
file.
Here is my customized activation function:
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))
After training, I save my model as follow:
from keras.models import load_model
model.save("model_baseline_lstm.h5")
Later, when I try to load the model, I run this :
from keras.models import load_model
model= load_model("model_baseline_lstm.h5")
BUT, I get this error:
--------------------------------------------------------------------------- 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
I want to share, how I solved this :
model= load_model("model_baseline_lsm.h5",
custom_objects = {"weibull_loglik_discrete": weibull_loglik_discrete,"activate":activate})
The pattern is as follow:
model = load_model(f"{SAVED_MODELS_DIR}/model_{model_idx}_epoch_{global_epoch}",
custom_objects = {"custom_loss": custom_loss})
I hope this help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With