save_inference_model save_inference_model ( dirname, feeded_var_names, target_vars, executor, main_program=None, model_filename=None, params_filename=None, export_for_deployment=True, program_only=False ) [source]

Warning: API “” is deprecated since 2.0.0, and will be removed in future versions. Please use “paddle.static.save_inference_model” instead.


Static Graph

Prune the given main_program to build a new program especially for inference, and then save it and all related parameters to given dirname . If you just want to save parameters of your trained model, please use the api_fluid_io_save_params . You can refer to Save and Load a Model for more details.


The dirname is used to specify the folder where inference model structure and parameters are going to be saved. If you would like to save params of Program in separate files, set params_filename None; if you would like to save all params of Program in a single file, use params_filename to specify the file name.


dirname(str): The directory path to save the inference model. feeded_var_names(list[str]): list of string. Names of variables that need to be fed

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/ of, line 20)

Unexpected indentation.

data during inference.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/ of, line 21)

Block quote ends without a blank line; unexpected unindent.

target_vars(list[Variable]): list of Variable. Variables from which we can get

inference results.

executor(Executor): The executor that saves the inference model. You can refer

to Executor for more details.

main_program(Program, optional): The original program, which will be pruned to

build the inference model. If is set None, the global default _main_program_ will be used. Default: None.

model_filename(str, optional): The name of file to save the inference program

itself. If is set None, a default filename __model__ will be used.

params_filename(str, optional): The name of file to save all related parameters.

If it is set None, parameters will be saved in separate files .

export_for_deployment(bool): If True, programs are modified to only support

direct inference deployment. Otherwise, more information will be stored for flexible optimization and re-training. Currently, only True is supported. Default: True.

program_only(bool, optional): If True, It will save inference program only, and do not

save params of Program. Default: False.


The fetch variables’ name list

Return Type:



ValueError: If feed_var_names is not a list of basestring, an exception is thrown. ValueError: If target_vars is not a list of Variable, an exception is thrown.

import paddle
import paddle.fluid as fluid

path = "./infer_model"

# User defined network, here a softmax regession example
image ='img', shape=[None, 28, 28], dtype='float32')
label ='label', shape=[None, 1], dtype='int64')
feeder = fluid.DataFeeder(feed_list=[image, label], place=fluid.CPUPlace())
predict = fluid.layers.fc(input=image, size=10, act='softmax')

loss = fluid.layers.cross_entropy(input=predict, label=label)
avg_loss = fluid.layers.mean(loss)

exe = fluid.Executor(fluid.CPUPlace())

# Feed data and train process

# Save inference model. Note we don't save label and loss in this example,

# In this example, the save_inference_mode inference will prune the default
# main program according to the network's input node (img) and output node(predict).
# The pruned inference program is going to be saved in the "./infer_model/__model__"
# and parameters are going to be saved in separate files under folder
# "./infer_model".