我已经训练了一个深度学习网络,它有一个预先训练过的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(filepath,custom_objects,编译)
文件"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py",第159行,在load_model_from_hdf5提起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) )但这一错误失败了:
elmo_BiDirectional_model.load_weights('weights.h5')中的
文件"C:\temp\Simon\perdict_elmo.py",第28行
文件"C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py",行182,在load_weights中返回超级(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.atts‘’keras_version‘..decode(’utf8‘)
AttributeError:'str‘对象没有属性'decode’
版本:
keras.__version__
'2.2.4-tf'
tensorflow.__version__
'1.15.0'发布于 2021-11-11 16:03:32
终于来了!我必须降低两个依赖项的级别,然后使用策略2将权重加载到模型中。
pip install astroid==2.3.0 --force-reinstall --user
pip install h5py==2.10.0 --force-reinstall --userhttps://stackoverflow.com/questions/69903100
复制相似问题