1

我已经训练了一个具有预训练 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 返回 hdf5_format.load_model_from_hdf5(文件路径,custom_objects,编译)

文件“C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py”,第 159 行,在 load_model_from_hdf5 中引发 ValueError('No model found in config file.')

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 中返回 super(Model, self).load_weights(filepath, by_name)

文件“C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py”,第 1373 行,在 load_weights Saving.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'
4

1 回答 1

0

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

pip install astroid==2.3.0 --force-reinstall --user
pip install h5py==2.10.0 --force-reinstall --user
于 2021-11-11T16:03:32.630 回答