Saving HF wrapped in Keras
See original GitHub issueHi,
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.
Issue Analytics
- State:
- Created 3 years ago
- Comments:24 (11 by maintainers)
Top GitHub Comments
Or a much nicer version IMO:
Once saved you can load and use it like this:
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.