3

I am following this tutorial on how to train a siamese bert network:

https://keras.io/examples/nlp/semantic_similarity_with_bert/

all good, but I am not sure what is the best way to save the model after train it and save it. any suggestion?

I was trying with

model.save('models/bert_siamese_v1')

which creates a folder with save_model.bp keras_metadata.bp and two subfolders (variables and assets)

then I try to load it with:

model.load_weights('models/bert_siamese_v1/')

and it gives me this error:

2022-03-08 14:11:52.567762: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open models/bert_siamese_v1/: Failed precondition: models/bert_siamese_v1; Is a directory: perhaps your file is in a different file format and you need to use a different restore operator?

what is the best way to proceed?

AloneTogether
  • 25,814
  • 5
  • 20
  • 39
Carbo
  • 906
  • 5
  • 23

2 Answers2

1

Try using tf.saved_model.save to save your model:

tf.saved_model.save(model, 'models/bert_siamese_v1')
model = tf.saved_model.load('models/bert_siamese_v1')

The warning you get during saving can apparently be ignored. After loading your model, you can use it for inference f(test_data):

f = model.signatures["serving_default"]
x1 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
x2 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
x3 = tf.random.uniform((1, 128), maxval=100, dtype=tf.int32)
print(f)
print(f(attention_masks = x1, input_ids = x2, token_type_ids = x3))
ConcreteFunction signature_wrapper(*, token_type_ids, attention_masks, input_ids)
  Args:
    attention_masks: int32 Tensor, shape=(None, 128)
    input_ids: int32 Tensor, shape=(None, 128)
    token_type_ids: int32 Tensor, shape=(None, 128)
  Returns:
    {'dense': <1>}
      <1>: float32 Tensor, shape=(None, 3)
{'dense': <tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[0.40711606, 0.13456087, 0.45832306]], dtype=float32)>}
AloneTogether
  • 25,814
  • 5
  • 20
  • 39
  • I tried it and it lets me save and load giving e all the warning you mentioned, but seems that now the predict function doesn't work anymore: AttributeError: '_UserObject' object has no attribute 'predict' (when running model.predict() with the correct inputs which were working – Carbo Mar 08 '22 at 15:42
  • can you try running `model(test_data)` instead of `model.predict(test_data)`? Since the `SavedModel` object is not a `Keras` object. Also check this link for more information: https://stackoverflow.com/questions/58755970/how-to-load-a-model-with-tf-saved-model-and-call-the-predict-function-tensorflo – AloneTogether Mar 08 '22 at 15:54
  • I tried and it gives me a new error! ValueError: Could not find matching function to call loaded from the SavedModel. Got: Positional arguments (3 total): etc.... – Carbo Mar 08 '22 at 16:02
  • so I tried using the info you provided in based on f: input_ids , attention_masks ,token_type_ids = test_data.__getitem__(0) then i run proba = model1(attention_masks, input_ids, token_type_ids) but the error still stands: ValueError: Could not find matching function to call loaded from the SavedModel. Got: Positional arguments (3 total): * Tensor("inputs:0", shape=(1, 128), dtype=int32) * Tensor("training:0", shape=(1, 128), dtype=int32) * Tensor("mask:0", shape=(1, 128), dtype=int32) Keyword arguments: {} – Carbo Mar 08 '22 at 16:21
  • Expected these arguments to match one of the following 4 option(s): Option 1: Positional arguments (3 total): * [TensorSpec(shape=(None, 128), dtype=tf.int32, name='input_ids'), TensorSpec(shape=(None, 128), dtype=tf.int32, name='attention_masks'), TensorSpec(shape=(None, 128), dtype=tf.int32, name='token_type_ids')] * False * None Keyword arguments: {} – Carbo Mar 08 '22 at 16:21
  • Did you check my updated answer? There I am showing you how to feed arguments to your model...It works without any problems. You just have to use the input names: `attention_masks`, `input_ids`, and `token_type_ids`. I am using the exact same model that you linked. – AloneTogether Mar 08 '22 at 16:22
-1

It seems you have two options

 model.save_weights('./checkpoints/my_checkpoint')
   
 model = create_model()
   
 model.load_weights('./checkpoints/my_checkpoint') 

Call model.save to save a model's architecture, weights, and training configuration in a single file/folder. This allows you to export a model so it can be used without access to the original Python code*. Since the optimizer-state is recovered, you can resume training from exactly where you left off.

Save model

# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)

# Save the entire model as a SavedModel.
!mkdir -p saved_model
model.save('saved_model/my_model')

load model

new_model = tf.keras.models.load_model('saved_model/my_model')

It seems that you are mixing both approaches, saving model and loading weights.

kiriloff
  • 25,609
  • 37
  • 148
  • 229