Can't save custom subclassed model

前端 未结 5 1113
失恋的感觉
失恋的感觉 2020-12-13 19:01

Inspired by tf.keras.Model subclassing I created custom model.
I can train it and get successfull results, but I can\'t save it.
I use python3.6 wit

5条回答
  •  庸人自扰
    2020-12-13 19:41

    Tensorflow 2.1 allows to save subclassed models with SavedModel format

    From my beginning using Tensorflow, i was always a fan of Model Subclass, i feel this way of build models more pythonic and collaborative friendly. But saving the model was always a point of pain with this approach.

    Recently i started to update my knowledge and reach to the following information that seems to be True for Tensorflow 2.1:

    Subclassed Models

    I found this

    Second approach is by using model.save to save whole model and by using load_model to restore previously stored subclassed model.

    This last saves the model, the weight and other stuff into a SavedModel file

    And by last the confirmation:

    Saving custom objects: If you are using the SavedModel format, you can skip this section. The key difference between HDF5 and SavedModel is that HDF5 uses object configs to save the model architecture, while SavedModel saves the execution graph. Thus, SavedModels are able to save custom objects like subclassed models and custom layers without requiring the orginal code.

    I tested this personally, and efectively, model.save() for subclassed models generate a SavedModel save. There is no more need for use model.save_weights() or related functions, they now are more for specific usecases.

    This is suposed to be the end of this painful path for all of us interested in Model Subclassing.

提交回复
热议问题