question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving HF wrapped in Keras

See original GitHub issue

Hi,

Trying to save a Keras model which has a HF model and a liner layer (Dense layer) on top of it. To save a model, Keras requires that every layer would have a serialize_layer_fn implemented. However it seems that HF models don’t include this function.

Spending some time understanding and googling this issue I came across the @keras_serializable which I assume is supposed to allow a HF layer to be serialized. However when I wrap my class with this, I get this issue:

AttributeError: Must set config_class to use @keras_serializable

Although the attached code which describes this issue does have a config_class member.

To test this behaviour I generated a code which creates a model based on BERT or some Bionlp-BERT variant. Then the code tries to train the model. After the short training period, we try to save the model, which includes the BERT model, BERT tokenizer (using save_pretrained) and the liner layer.

For me, the best way to save this model would be using the to_json function, which converts the Keras model into a serialized model. However, I couldn’t manage to get this working. Any idea how can this be done? Or any objection about saving HF model using the “to_json” method?

A possible workaround would be to use the save_pretrained method for the BERT model and tokenizer but then how would I save also the linear layer?

This issue relates to this issue https://github.com/huggingface/transformers/issues/2733.

compile_model.txt

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:24 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jplucommented, Feb 22, 2021

Or a much nicer version IMO:

def create_model(bert_variant, max_len):
    config = BertConfig.from_pretrained(bert_variant)
    input_word_ids = tf.keras.layers.Input(shape=(max_len,), dtype=tf.int32,
                                           name="input_word_ids")
    embedding = TFBertMainLayer(config)(input_word_ids)
    cls_layer = embedding.last_hidden_state[:,0,:]
    logits = tf.keras.layers.Dense(4, name="prediction", use_bias=True)(cls_layer)
    logits = tf.keras.layers.Reshape((1, 4))(logits)
    pred_probs = tf.keras.layers.Activation(tf.keras.activations.softmax)(logits)
    model = tf.keras.Model(inputs=[input_word_ids], outputs=[pred_probs])
    loss = tf.keras.losses.CategoricalCrossentropy(from_logits=False)
    optimizer = tf.keras.optimizers.Adam(lr=5e-5)
    model.compile(optimizer=optimizer, loss=loss)

    return model

model = create_model("bert-base-cased", 128)
model.save("model_path")  

Once saved you can load and use it like this:

model = tf.keras.models.load_model("model_path")
l = [1]*128 
inp = tf.constant([l])
model(inp)
0reactions
github-actions[bot]commented, Apr 14, 2021

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load Keras models | TensorFlow Core
Saving everything into a single archive in the TensorFlow SavedModel format (or in the older Keras H5 format). This is the standard practice....
Read more >
Models - Hugging Face
PreTrainedModel takes care of storing the configuration of the models and handles methods for loading, downloading and saving models as well as a...
Read more >
Model saving & serialization APIs - Keras
Saves all layer weights. Either saves in HDF5 or in TensorFlow format based on the save_format argument. When saving in HDF5 format, the...
Read more >
How to Save and Load Your Keras Deep Learning Model
In this post, you will discover how to save your Keras models to files and load them up again to make predictions.
Read more >
ckb vs eth - Il Vecchio Cortile
Or save the following code snippet as a bookmarklet to quickly go from any ... membungkus (wrapping) CKB dan NFT dari Nervos ke...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found