ValueError:未知激活函数:my_custom_activation_function

Posted

技术标签:

【中文标题】ValueError:未知激活函数:my_custom_activation_function【英文标题】:ValueError: Unknown activation function: my_custom_activation_function 【发布时间】:2019-09-10 18:06:06 【问题描述】:

我使用了一个我自己创建的激活函数(通常不是这样),并用于我的 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

【问题讨论】:

【参考方案1】:

我想分享一下,我是如何解决这个问题的:

model= load_model("model_baseline_lsm.h5",
                  custom_objects = "weibull_loglik_discrete": weibull_loglik_discrete,"activate":activate)

模式如下:

model = load_model(f"SAVED_MODELS_DIR/model_model_idx_epoch_global_epoch", 
                   custom_objects = "custom_loss": custom_loss)

希望对你有帮助:)

【讨论】:

以上是关于ValueError:未知激活函数:my_custom_activation_function的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:未知标签类型:RandomForestClassifier 中的“未知”

ValueError:未知标签类型:“未知”-标签是数字

ValueError:未知标签类型

ValueError:未知投影“3d”(再次)

ValueError:未知优化器:优化器

MLP 分类器:“ValueError:未知标签类型”