繁体   English   中英

Keras:如何使用预训练的 ELMO 层加载模型

[英]Keras: How to load a model with pretrained ELMO Layer

我已经训练了一个具有预训练 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, compile)

文件“C:\\

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'

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM