gpt4 book ai didi

python - 值错误 : Unknown activation function: my_custom_activation_function

转载 作者:行者123 更新时间:2023-12-01 10:19:20 25 4
gpt4 key购买 nike

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

最佳答案

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

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

我希望这会有所帮助:)

关于python - 值错误 : Unknown activation function: my_custom_activation_function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55779286/

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