I have a problem with loading the previously saved model.
This is my save:
def build_rnn_lstm_model(tokenizer, layers):
model = tf.keras.Sequential([
tf.keras.layers.Embedding(len(tokenizer.word_index) + 1, layers,input_length=843),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(layers, kernel_regularizer=l2(0.01), recurrent_regularizer=l2(0.01), bias_regularizer=l2(0.01))),
tf.keras.layers.Dense(layers, activation='relu', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)),
tf.keras.layers.Dense(layers/2, activation='relu', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',f1,precision, recall])
print("Layers: ", len(model.layers))
return model
model_path = str(Path(__file__).parents[2]) + os.path.sep + 'model'
data_train_sequence, data_test_sequence, labels_train, labels_test, tokenizer = get_training_test_data_local()
model = build_rnn_lstm_model(tokenizer, 32)
model.fit(data_train_sequence, labels_train, epochs=num_epochs, validation_data=(data_test_sequence, labels_test))
model.save(model_path + os.path.sep + 'auditor_model', save_format='tf')
After this I can see that auditor_model
is saved in model
directory.
now I would like to load this model with:
model = tf.keras.models.load_model(model_path + os.path.sep + 'auditor_model')
but I get:
ValueError: Unable to restore custom object of type _tf_keras_metric currently. Please make sure that the layer implements
get_config
andfrom_config
when saving. In addition, please use thecustom_objects
arg when callingload_model()
.
I have read about custom_objects
in TensorFlow
docs but I don't understand how to implement it while I use no custom layers but the predefined ones.
Could anyone give me a hint how to make it work? I use TensorFlow 2.2 and Python3
Using save_weights() method Now you can simply save the weights of all the layers using the save_weights() method. It saves the weights of the layers contained in the model. It is advised to use the save() method to save h5 models instead of save_weights() method for saving a model using tensorflow.
Save Your Neural Network Model to JSON The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.
Your example is missing the definition of f1
, precision
and recall
functions. If the builtin metrics e.g. 'f1'
(note it is a string) do not fit your usecase you can pass the custom_objects
as follows:
def f1(y_true, y_pred):
return 1
model = tf.keras.models.load_model(path_to_model, custom_objects={'f1':f1})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With