I have trained a Deep Learning network that has a pretrained ELMO layer. I've saved the model and weights using the code below.
model.save("model.h5")
model.save_weights("weights.h5")
I now need to load the load but I'm not sure whats the right way. I've tried two techniques and both of them fail.
1: Tried just loading the model but fails with a get_config error
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) )
File "C:\temp\Simon\perdict_elmo.py", line 36, in elmo_BiDirectional_model = keras.models.load_model("model.h5")
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 143, in load_model return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 159, in load_model_from_hdf5 raise ValueError('No model found in config file.')
ValueError: No model found in config file.
2: Tried building the model and just setting the weights:
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) )
But this failed with error:
File "C:\temp\Simon\perdict_elmo.py", line 28, in elmo_BiDirectional_model.load_weights('weights.h5')
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 182, in load_weights return super(Model, self).load_weights(filepath, by_name)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1373, in load_weights saving.load_weights_from_hdf5_group(f, self.layers)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 645, in load_weights_from_hdf5_group original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'
Versions:
keras.__version__
'2.2.4-tf'
tensorflow.__version__
'1.15.0'