【问题标题】:Keras: How to load a model with pretrained ELMO LayerKeras:如何使用预训练的 ELMO 层加载模型
【发布时间】:2021-11-09 18:13:44
【问题描述】:

我已经训练了一个具有预训练 ELMO 层的深度学习网络。我已经使用下面的代码保存了模型和权重。

model.save("model.h5")
model.save_weights("weights.h5")

我现在需要加载负载,但我不确定什么是正确的方法。我尝试了两种技术,但都失败了。

1:尝试仅加载模型,但由于 get_config 错误而失败

import numpy as np
import io
import re
from tensorflow import keras 

elmo_BiDirectional_model = keras.models.load_model("model.h5")

x_data = np.zeros((1, 1), dtype='object')
x_data[0] = "test token"

with tf.Session() as session:
    session.run(tf.global_variables_initializer()) 
    session.run(tf.tables_initializer())
    print( elmo_BiDirectional_model.predict(x_data) )

文件“C:\temp\Simon\perdict_elmo.py”,第 36 行,在 elmo_BiDirectional_model = keras.models.load_model("model.h5")

文件 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\save.py", 第 143 行,在 load_model 中 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)

文件 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", 第 159 行,在 load_model_from_hdf5 raise ValueError('在配置文件中找不到模型。')

ValueError: 在配置文件中找不到模型。

2:尝试构建模型并设置权重:

import tensorflow_hub as hub
import tensorflow as tf

elmo = hub.Module("https://tfhub.dev/google/elmo/3", trainable=False)

from tensorflow.keras.layers import Input, Lambda, Bidirectional, Dense, Dropout, Flatten, LSTM
from tensorflow.keras.models import Model

def ELMoEmbedding(input_text):
    return elmo(tf.reshape(tf.cast(input_text, tf.string), [-1]), signature="default", as_dict=True)["elmo"]

def build_model():
    input_layer = Input(shape=(1,), dtype="string", name="Input_layer")    
    embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ), name="Elmo_Embedding")(input_layer)
    BiLSTM = Bidirectional(LSTM(128, return_sequences= False, recurrent_dropout=0.2, dropout=0.2), name="BiLSTM")(embedding_layer)
    Dense_layer_1 = Dense(64, activation='relu')(BiLSTM)
    Dropout_layer_1 = Dropout(0.5)(Dense_layer_1)
    Dense_layer_2 = Dense(32, activation='relu')(Dropout_layer_1)
    Dropout_layer_2 = Dropout(0.5)(Dense_layer_2)
    output_layer = Dense(3, activation='sigmoid')(Dropout_layer_2)
    model = Model(inputs=[input_layer], outputs=output_layer, name="BiLSTM with ELMo Embeddings")
    model.summary()
    model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
    return model


elmo_BiDirectional_model = build_model()
elmo_BiDirectional_model.load_weights('weights.h5')

import numpy as np
import io
import re
from tensorflow import keras 

x_data = np.zeros((1, 1), dtype='object')
x_data[0] = "test token"

with tf.Session() as session:
    session.run(tf.global_variables_initializer()) 
    session.run(tf.tables_initializer())
    print( elmo_BiDirectional_model.predict(x_data) )

但这失败并出现错误:

文件“C:\temp\Simon\perdict_elmo.py”,第 28 行,在 elmo_BiDirectional_model.load_weights('weights.h5')

文件 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", 第 182 行,在 load_weights 中 return super(Model, self).load_weights(filepath, by_name)

文件 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py", 第 1373 行,在 load_weights 中 save.load_weights_from_hdf5_group(f, self.layers)

文件 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", 第 645 行,在 load_weights_from_hdf5_group original_keras_version = f.attrs['keras_version'].decode('utf8')

AttributeError: 'str' 对象没有属性 'decode'

版本:

keras.__version__
'2.2.4-tf'

tensorflow.__version__
'1.15.0'

【问题讨论】:

    标签: python tensorflow keras elmo


    【解决方案1】:

    终于!我不得不降级两个依赖项,然后我使用策略 #2 将权重加载到模型中。

    pip install astroid==2.3.0 --force-reinstall --user
    pip install h5py==2.10.0 --force-reinstall --user
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2019-05-31
      • 2017-07-16
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多