Save and Load a Model

To save and load model, there are eight APIs playing an important role: fluid.io.save_vars, fluid.io.save_params, fluid.io.save_persistables, fluid.io.save_inference_model, fluid.io.load_vars, fluid.io.load_params, fluid.io.load_persistables and fluid.io.load_inference_model .

Variables, Persistables and Parameters

In Paddle , every input and output of operator( Operator ) is a variable( Variable ), and parameter( Parameter ) is a derived class of Variable( Variable ). Persistables (Persistables) are variables that won’t be deleted after each iteration. Parameter is a kind of persistable variable which will be updated by optimizer ( Optimizer ) after each iteration. Training of neural network in essence is to update parameters.

Introduction to APIs for saving a model

  • fluid.io.save_vars: Variables are saved in specified directory by executor( Executor ). There are two ways to save variables:

    1)Set vars in the API to assign the variable list to be saved.

    2)Assign an existed program( Program ) to main_program in the API, and then all variables in the program will be saved.

    The first one has a higher priority than the second one.

    For API Reference , please refer to api_fluid_io_save_vars.

  • fluid.io.save_params: Set main_program in the API with the model Program( Program ). This API will filter all parameters( Parameter ) of targeted program and save them in folder assigned by dirname or file assigned by filename .

    For API Reference , please refer to api_fluid_io_save_params.

  • fluid.io.save_persistables: main_program of API assigns program( Program ). This API will filter all persistables( persistable==True ) of targeted program and save them in folder assigned by dirname or file assigned by filename .

    For API Reference, please refer to api_fluid_io_save_persistables.

  • fluid.io.save_inference_model: please refer to Inference Engine.

Introduction to APIs for loading a model

  • fluid.io.load_vars: Executor( Executor ) loads variables into the target directory. There are two ways to load variables:

    1):code:vars in the API assigns variable list to be loaded.

    2)Assign an existed program( Program ) to the main_program field in the API, and then all variables in the program will be loaded.

    The first loading method has higher priority than the second one.

    For API Reference, please refer to api_fluid_io_load_vars.

  • fluid.io.load_params: This API filters all parameters( Parameter ) in program assigned by main_program and load these parameters from folder assigned by dirname or file assigned by filename .

    For API Reference, please refer to api_fluid_io_load_params .

  • fluid.io.load_persistables:This API filters all persistables( persistable==True ) in program assigned by main_program and load these persistables from folder assigned by dirname or file assigned by filename .

    For API Reference, please refer to api_fluid_io_load_persistables .

  • fluid.io.load_inference_model: please refer to Inference Engine .