我正在使用tensorflow==1.15.5。
我正在加载这样的预训练模型
# loading pre trained model to extract elmo vectors
# this is required to load custom model
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
class DeepLearningBot:
dirName = os.path.dirname(__file__)
stopwords = ['i', 'am', 'a',
'to', 'is', 'do', 'in', 'of']
labelModelPath = os.path.join(dirName, "config/labels_mapping.pkl")
modelPath = os.path.join(dirName,"models/elmo-model-bilstm2.h5")
def init():
Logger.log("Using tensorflow : " + tf.__version__)
DeepLearningBot.modelBlstm = tf.keras.models.load_model(DeepLearningBot.modelPath)要加载自定义模型,需要elmo变量(需要在模型内部使用elmo变量)
我已经在类外部初始化了变量elmo,但是当我在init()方法内部加载模型时,它会说
return deserialize(config, custom_objects=custom_objects)
File "c:\\env37\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py", line 105, in deserialize
printable_module_name='layer')
File "c:\PERSONAL\projects\AdamAI\adam-chat-server\env37\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 191, in deserialize_keras_object
list(custom_objects.items())))
File "c:\env37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1081, in from_config
process_node(layer, node_data)
File "c:\PERSONAL\projects\AdamAI\adam-chat-server\env37\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1039, in process_node
layer(input_tensors, **kwargs)
File "c:\env37\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 854, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "c:\env37\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 789, in call
return self.function(inputs, **arguments)
File "<ipython-input-45-e850aa69f190>", line 3, in ELMoEmbedding_bl
NameError: name 'elmo' is not defined现在我该如何解决这个问题呢?
有没有其他方法来配置预先训练好的模型?
发布于 2021-07-08 20:49:11
模型使用的是变量elmo和tf,因此它希望在加载模型之前声明这些变量。
在使用custom_objects属性加载模型时,还有其他方法可以传递它
DeepLearningBot.modelBlstm = tf.keras.models.load_model(DeepLearningBot.modelPath,
custom_objects={
'elmo': hub.Module("https://tfhub.dev/google/elmo/2", trainable=False),
'tf': tf
})https://stackoverflow.com/questions/68271800
复制相似问题