fluid.io

load_inference_model

paddle.fluid.io.load_inference_model(dirname, executor, model_filename=None, params_filename=None, pserver_endpoints=None)[source]

Load inference model from a directory. By this API, you can get the model structure(inference program) and model parameters. If you just want to load parameters of the pre-trained model, please use the load_params API. You can refer to Save and Load a Model for more details.

Parameters:
  • dirname (str) – The directory path
  • executor (Executor) – The executor to run for loading inference model.
  • model_filename (str|None) – The name of file to load inference program. If it is None, the default filename ‘__model__’ will be used. Default: None
  • params_filename (str|None) – The name of file to load all parameters. It is only used for the case that all parameters were saved in a single binary file. If parameters were saved in separate files, set it as ‘None’.
  • pserver_endpoints (list|None) – This only need by distributed inference. When use distributed look up table in training, We also need it in inference.The parameter is a list of pserver endpoints.
Returns:

The return of this function is a tuple with three elements: (program, feed_target_names, fetch_targets). The program is a Program, it’s the program for inference. The feed_target_names is a list of str, it contains Names of variables that need to feed data in the inference program. The fetch_targets is a list of Variable. It contains variables from which we can get inference results.

Return type:

tuple

Raises:

ValueError – If dirname is not a existing directory.

Examples

import paddle.fluid as fluid
import numpy as np
main_prog = fluid.Program()
startup_prog = fluid.Program()
with fluid.program_guard(main_prog, startup_prog):
    data = fluid.layers.data(name="img", shape=[64, 784], append_batch_size=False)
    w = fluid.layers.create_parameter(shape=[784, 200], dtype='float32')
    b = fluid.layers.create_parameter(shape=[200], dtype='float32')
    hidden_w = fluid.layers.matmul(x=data, y=w)
    hidden_b = fluid.layers.elementwise_add(hidden_w, b)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(startup_prog)
path = "./infer_model"
fluid.io.save_inference_model(dirname=path, feeded_var_names=['img'],
             target_vars=[hidden_b], executor=exe, main_program=main_prog)
tensor_img = np.array(np.random.random((1, 64, 784)), dtype=np.float32)
[inference_program, feed_target_names, fetch_targets] = (
    fluid.io.load_inference_model(dirname=path, executor=exe))
results = exe.run(inference_program,
              feed={feed_target_names[0]: tensor_img},
              fetch_list=fetch_targets)

# endpoints is your pserver endpoints list, the above is just an example
endpoints = ["127.0.0.1:2023","127.0.0.1:2024"]
# if we need lookup table, we will use:
[dist_inference_program, dist_feed_target_names, dist_fetch_targets] = (
    fluid.io.load_inference_model(dirname=path,
                                  executor=exe,
                                  pserver_endpoints=endpoints))

# In this example, the inference program was saved in the
# "./infer_model/__model__" and parameters were saved in
# separate files in "./infer_model".
# After getting inference program, feed target names and
# fetch targets, we can use an Executor to run the inference
# program to get the inference result.

load_params

paddle.fluid.io.load_params(executor, dirname, main_program=None, filename=None)[source]

This function filters out all parameters from the give main_program and then trys to load these parameters from the folder dirname or the file filename.

Use the dirname to specify the folder where parameters were saved. If parameters were saved in separate files in the folder dirname, set filename None; if all parameters were saved in a single file, use filename to specify the file name.

NOTICE: Some variables are not Parameter while they are necessary for training. So you can NOT save and continue your training just by save_params() and load_params(). Please use save_persistables() and load_persistables() instead. If you want to load the pre-trained model structure and parameters for the inference, please use the load_inference_model API. You can refer to Save and Load a Model for more details.

Parameters:
  • executor (Executor) – The executor to run for loading parameters.
  • dirname (str) – The directory path.
  • main_program (Program|None) – The program whose parameters will be loaded. If it is None, the default main program will be used automatically. Default: None
  • filename (str|None) – The file which saved all parameters. If parameters were saved in differnet files, set it to None. Default: None
Returns:

None

Examples

import paddle.fluid as fluid
exe = fluid.Executor(fluid.CPUPlace())
param_path = "./my_paddle_model"
prog = fluid.default_main_program()
fluid.io.load_params(executor=exe, dirname=param_path,
                    main_program=None)

load_persistables

paddle.fluid.io.load_persistables(executor, dirname, main_program=None, filename=None)[source]

This function filters out all variables with persistable==True from the give main_program and then trys to load these variables from the folder dirname or the file filename.

Use the dirname to specify the folder where persistable variables were saved. If variables were saved in separate files, set filename None; if all variables were saved in a single file, use filename to specify the file name.

Parameters:
  • executor (Executor) – The executor to run for loading persistable variables.
  • dirname (str) – The directory path.
  • main_program (Program|None) – The program whose persistbale variables will be loaded. If it is None, the default main program will be used automatically. Default: None
  • filename (str|None) – The file which saved all variables. If variables were saved in differnet files, set it to None. Default: None
Returns:

None

Examples

import paddle.fluid as fluid
exe = fluid.Executor(fluid.CPUPlace())
param_path = "./my_paddle_model"
prog = fluid.default_main_program()
fluid.io.load_persistables(executor=exe, dirname=param_path,
                           main_program=None)

load_vars

paddle.fluid.io.load_vars(executor, dirname, main_program=None, vars=None, predicate=None, filename=None)[source]

Load variables from the given directory by executor.

There are two ways to specify variables to be loaded: The first way, list variables in a list and assign it to the vars. The second way, assign the main_program with an existing program, then all variables in the program will be loaded. The first way has a higher priority. In other words if vars are assigned, the main_program and the predicate will be ignored.

The dirname are used to specify the folder where to load variables. If variables were saved in separate files in the folder dirname, set filename None; if all variables were saved in a single file, use filename to specify it.

Parameters:
  • executor (Executor) – The executor to run for loading variables.
  • dirname (str) – The directory path.
  • main_program (Program|None) – The program whose variables will be loaded. If it is None, the default main program will be used automatically. Default: None
  • vars (list[Variable]|None) – The list that contains all variables to load. It has a higher priority than the main_program. Default: None
  • predicate (function|None) – If it is not None, only variables in the main_program that makes predicate(variable)==True will be loaded. It only works when we are using the main_program to specify variables (In other words vars is None). Default: None
  • filename (str|None) – The file which saved all required variables. If variables were saved in differnet files, set it to None. Default: None
Returns:

None

Raises:

TypeError – If main_program is not an instance of Program nor None.

Examples

import paddle.fluid as fluid
main_prog = fluid.Program()
startup_prog = fluid.Program()
with fluid.program_guard(main_prog, startup_prog):
    data = fluid.layers.data(name="img", shape=[64, 784], append_batch_size=False)
    w = fluid.layers.create_parameter(shape=[784, 200], dtype='float32', name='fc_w')
    b = fluid.layers.create_parameter(shape=[200], dtype='float32', name='fc_b')
    hidden_w = fluid.layers.matmul(x=data, y=w)
    hidden_b = fluid.layers.elementwise_add(hidden_w, b)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(startup_prog)

param_path = "./my_paddle_model"
# The first usage: using `main_program` to specify variables
def name_has_fc(var):
    res = "fc" in var.name
    return res
fluid.io.save_vars(executor=exe, dirname=param_path, main_program=main_prog,
                  vars=None, predicate=name_has_fc)
fluid.io.load_vars(executor=exe, dirname=param_path, main_program=main_prog,
                   vars=None, predicate=name_has_fc)
# All variables in `main_program` whose name includes "fc" will be loaded.
# And all the variables are supposed to have been saved in differnet files.

# The second usage: using `vars` to specify variables
path = "./my_paddle_vars"
var_list = [w, b]
fluid.io.save_vars(executor=exe, dirname=path, vars=var_list,
                   filename="vars_file")
fluid.io.load_vars(executor=exe, dirname=path, vars=var_list,
                   filename="vars_file")
# w and b will be loaded. And they are supposed to haven
# been saved in the same file named 'var_file' in the path "./my_paddle_vars".

PyReader

class paddle.fluid.io.PyReader(feed_list=None, capacity=None, use_double_buffer=True, iterable=True, return_list=False)[source]

Create a reader object for data feeding in Python. Data would be prefetched using Python thread and be pushed into a queue asynchronously. Data in the queue would be extracted automatically when Executor.run(...) is called.

Parameters:
  • feed_list (list(Variable)|tuple(Variable)) – feed variable list. The variables should be created by fluid.layers.data(). it can be None under iterable mode.
  • capacity (int) – capacity of the queue maintained in PyReader object.
  • use_double_buffer (bool) – whether to use double_buffer_reader to speed up data feeding.
  • iterable (bool) – whether the created reader object is iterable.
  • return_list (bool) – whether the return value presented as list.
Returns:

the created reader object.

Return type:

reader (Reader)

Examples

  1. If iterable = False, the created PyReader object is almost the same as fluid.layers.py_reader(). Operators would be inserted into the program. User should call start() before each epoch and catch fluid.core.EOFException thrown by Executor.run() when epoch ends. Once the exception is caught, user should call reset() to reset the reader manually.
EPOCH_NUM = 3
ITER_NUM = 5
BATCH_SIZE = 3

def reader_creator_random_image_and_label(height, width):
    def reader():
        for i in range(ITER_NUM):
            fake_image = np.random.uniform(low=0,
                                           high=255,
                                           size=[height, width])
            fake_label = np.ones([1])
            yield fake_image, fake_label
    return reader

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')

reader = fluid.io.PyReader(feed_list=[image, label],
                           capacity=4,
                           iterable=False)

user_defined_reader = reader_creator_random_image_and_label(784, 784)
reader.decorate_sample_list_generator(
    paddle.batch(user_defined_reader, batch_size=BATCH_SIZE))
# definition of network is omitted
executor = fluid.Executor(fluid.CUDAPlace(0))
executor.run(fluid.default_startup_program())
for i in range(EPOCH_NUM):
    reader.start()
    while True:
        try:
            executor.run(feed=None)
        except fluid.core.EOFException:
            reader.reset()
            break
  1. If iterable=True, the created PyReader object is decoupled with the program. No operator would be inserted into the program. In this case, the created reader is a Python generator, which is iterable. User should feed the data yielded from PyReader object into Executor.run(feed=...).
EPOCH_NUM = 3
ITER_NUM = 5
BATCH_SIZE = 10

def reader_creator_random_image(height, width):
    def reader():
        for i in range(ITER_NUM):
            yield np.random.uniform(low=0, high=255, size=[height, width]),
    return reader

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
reader = fluid.io.PyReader(feed_list=[image], capacity=4, iterable=True, return_list=False)

user_defined_reader = reader_creator_random_image(784, 784)
reader.decorate_sample_list_generator(
    paddle.batch(user_defined_reader, batch_size=BATCH_SIZE),
    fluid.core.CUDAPlace(0))
# definition of network is omitted
executor = fluid.Executor(fluid.CUDAPlace(0))
executor.run(fluid.default_main_program())

for _ in range(EPOCH_NUM):
    for data in reader():
        executor.run(feed=data)
  1. If return_list=True, the return values would be presented as list instead of dict`.
import paddle
import paddle.fluid as fluid
import numpy as np

EPOCH_NUM = 3
ITER_NUM = 5
BATCH_SIZE = 10

def reader_creator_random_image(height, width):
    def reader():
        for i in range(ITER_NUM):
            yield np.random.uniform(low=0, high=255, size=[height, width]),
    return reader

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
reader = fluid.io.PyReader(feed_list=[image], capacity=4, iterable=True, return_list=True)

user_defined_reader = reader_creator_random_image(784, 784)
reader.decorate_sample_list_generator(
    paddle.batch(user_defined_reader, batch_size=BATCH_SIZE),
    fluid.core.CPUPlace())
# definition of network is omitted
executor = fluid.Executor(fluid.core.CPUPlace())
executor.run(fluid.default_main_program())

for _ in range(EPOCH_NUM):
    for data in reader():
        executor.run(feed={"image": data[0]})
start()[source]

Start the data feeding thread. Can only call when the reader object is not iterable.

Example

BATCH_SIZE = 10

def generator():
    for i in range(5):
        yield np.random.uniform(low=0, high=255, size=[784, 784]),

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
reader = fluid.io.PyReader(feed_list=[image], capacity=4, iterable=False)
reader.decorate_sample_list_generator(
    paddle.batch(generator, batch_size=BATCH_SIZE))

executor = fluid.Executor(fluid.CUDAPlace(0))
executor.run(fluid.default_startup_program())
for i in range(3):
    reader.start()
    while True:
        try:
            executor.run(feed=None)
        except fluid.core.EOFException:
            reader.reset()
            break
reset()[source]

Reset the reader object when fluid.core.EOFException raises. Can only call when the reader object is not iterable.

Example

BATCH_SIZE = 10

def generator():
    for i in range(5):
        yield np.random.uniform(low=0, high=255, size=[784, 784]),

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
reader = fluid.io.PyReader(feed_list=[image], capacity=4, iterable=False)
reader.decorate_sample_list_generator(
    paddle.batch(generator, batch_size=BATCH_SIZE))

executor = fluid.Executor(fluid.CUDAPlace(0))
executor.run(fluid.default_startup_program())
for i in range(3):
    reader.start()
    while True:
        try:
            executor.run(feed=None)
        except fluid.core.EOFException:
            reader.reset()
            break
decorate_sample_generator(sample_generator, batch_size, drop_last=True, places=None)[source]

Set the data source of the PyReader object.

The provided sample_generator should be a Python generator, which yields list(numpy.ndarray)-typed data of each sample.

places must be set when the PyReader object is iterable.

If all inputs have no lods, this method is faster than decorate_sample_list_generator(paddle.batch(sample_generator, ...)) .

Parameters:
  • sample_generator (generator) – Python generator that yields list(numpy.ndarray)-typed sample data.
  • batch_size (int) – batch size. Must be larger than 0.
  • drop_last (bool) – Whether to drop the last batch when sample number is less than batch_size.
  • places (None|list(CUDAPlace)|list(CPUPlace)) – place list. Must be provided when PyReader is iterable.

Example

EPOCH_NUM = 3
ITER_NUM = 15
BATCH_SIZE = 3

def random_image_and_label_generator(height, width):
    def generator():
        for i in range(ITER_NUM):
            fake_image = np.random.uniform(low=0,
                                           high=255,
                                           size=[height, width])
            fake_label = np.array([1])
            yield fake_image, fake_label
    return generator

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int32')
reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True)

user_defined_generator = random_image_and_label_generator(784, 784)
reader.decorate_sample_generator(user_defined_generator,
                                 batch_size=BATCH_SIZE,
                                 places=[fluid.CUDAPlace(0)])
# definition of network is omitted
executor = fluid.Executor(fluid.CUDAPlace(0))
executor.run(fluid.default_main_program())

for _ in range(EPOCH_NUM):
    for data in reader():
        executor.run(feed=data)
decorate_sample_list_generator(reader, places=None)[source]

Set the data source of the PyReader object.

The provided reader should be a Python generator, which yields list(numpy.ndarray) typed batched data.

places must be set when the PyReader object is iterable.

Parameters:
  • reader (generator) – Python generator that yields list(numpy.ndarray)-typed batched data.
  • places (None|list(CUDAPlace)|list(CPUPlace)) – place list. Must be provided when PyReader is iterable.

Example

EPOCH_NUM = 3
ITER_NUM = 15
BATCH_SIZE = 3

def random_image_and_label_generator(height, width):
    def generator():
        for i in range(ITER_NUM):
            fake_image = np.random.uniform(low=0,
                                           high=255,
                                           size=[height, width])
            fake_label = np.ones([1])
            yield fake_image, fake_label
    return generator

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int32')
reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True)

user_defined_generator = random_image_and_label_generator(784, 784)
reader.decorate_sample_list_generator(
    paddle.batch(user_defined_generator, batch_size=BATCH_SIZE),
    fluid.core.CUDAPlace(0))
# definition of network is omitted
executor = fluid.Executor(fluid.core.CUDAPlace(0))
executor.run(fluid.default_main_program())

for _ in range(EPOCH_NUM):
    for data in reader():
        executor.run(feed=data)
decorate_batch_generator(reader, places=None)[source]

Set the data source of the PyReader object.

The provided reader should be a Python generator, which yields numpy.ndarray-typed or LoDTensor-typed batched data.

places must be set when the PyReader object is iterable.

Parameters:
  • reader (generator) – Python generator that yields LoDTensor-typed batched data.
  • places (None|list(CUDAPlace)|list(CPUPlace)) – place list. Must be provided when PyReader is iterable.

Example

EPOCH_NUM = 3
ITER_NUM = 15
BATCH_SIZE = 3

def random_image_and_label_generator(height, width):
    def generator():
        for i in range(ITER_NUM):
            batch_image = np.random.uniform(low=0,
                                            high=255,
                                            size=[BATCH_SIZE, height, width])
            batch_label = np.ones([BATCH_SIZE, 1])
            yield batch_image, batch_label
    return generator

image = fluid.layers.data(name='image', shape=[784, 784], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int32')
reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True)

user_defined_generator = random_image_and_label_generator(784, 784)
reader.decorate_batch_generator(user_defined_generator, fluid.CUDAPlace(0))
# definition of network is omitted
executor = fluid.Executor(fluid.CUDAPlace(0))
executor.run(fluid.default_main_program())

for _ in range(EPOCH_NUM):
    for data in reader():
        executor.run(feed=data)

save_inference_model

paddle.fluid.io.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]

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

Parameters:
  • dirname (str) – The directory path to save the inference model.
  • feeded_var_names (list[str]) – Names of variables that need to be feeded data during inference.
  • target_vars (list[Variable]) – Variables from which we can get inference results.
  • executor (Executor) – The executor that saves the inference model.
  • main_program (Program|None) – The original program, which will be pruned to build the inference model. If is setted None, the default main program will be used. Default: None.
  • model_filename (str|None) – The name of file to save the inference program itself. If is setted None, a default filename __model__ will be used.
  • params_filename (str|None) – The name of file to save all related parameters. If it is setted 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.
  • program_only (bool) – If True, It will save inference program only, and do not save params of Program.
Returns:

The fetch variables’ name list

Return type:

target_var_name_list(list)

Raises:
  • ValueError – If feed_var_names is not a list of basestring.
  • ValueError – If target_vars is not a list of Variable.

Examples

import paddle.fluid as fluid

path = "./infer_model"

# User defined network, here a softmax regresssion example
image = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[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())
exe.run(fluid.default_startup_program())

# Feed data and train process

# Save inference model. Note we don't save label and loss in this example
fluid.io.save_inference_model(dirname=path,
                              feeded_var_names=['img'],
                              target_vars=[predict],
                              executor=exe)

# In this example, the function will prune the default main program
# to make it suitable for infering the `predict` var. 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".

save_params

paddle.fluid.io.save_params(executor, dirname, main_program=None, filename=None)[source]

This function filters out all parameters from the give main_program and then save them to the folder dirname or the file filename.

Use the dirname to specify the saving folder. If you would like to save parameters in separate files, set filename None; if you would like to save all parameters in a single file, use filename to specify the file name.

NOTICE: Some variables are not Parameter while they are necessary for training. So you can NOT save and continue your training just by save_params() and load_params(). Please use save_persistables() and load_persistables() instead. If you want to save your model for the inference, please use the save_inference_model API. You can refer to Save and Load a Model for more details.

Parameters:
  • executor (Executor) – The executor to run for saving parameters.
  • dirname (str) – The saving directory path.
  • main_program (Program|None) – The program whose parameters will be saved. If it is None, the default main program will be used automatically. Default: None
  • filename (str|None) – The file to save all parameters. If you prefer to save parameters in differnet files, set it to None. Default: None
Returns:

None

Examples

exe = fluid.Executor(fluid.CPUPlace())
param_path = "./my_paddle_model"
prog = fluid.default_main_program()
fluid.io.save_params(executor=exe, dirname=param_path,
                     main_program=None)

save_persistables

paddle.fluid.io.save_persistables(executor, dirname, main_program=None, filename=None)[source]

This function filters out all variables with persistable==True from the give main_program and then saves these variables to the folder dirname or file filename.

The dirname is used to specify the folder where persistable variables are going to be saved. If you would like to save variables in separate files, set filename None; if you would like to save all variables in a single file, use filename to specify the file name.

Parameters:
  • executor (Executor) – The executor to run for saving persistable variables.
  • dirname (str) – The directory path.
  • main_program (Program|None) – The program whose persistbale variables will be saved. If it is None, the default main program will be used automatically. Default: None
  • filename (str|None) – The file to saved all variables. If you prefer to save variables in differnet files, set it to None. Default: None
Returns:

None

Examples

exe = fluid.Executor(fluid.CPUPlace())
param_path = "./my_paddle_model"
# `prog` can be a program defined by the user
prog = fluid.default_main_program()
fluid.io.save_persistables(executor=exe, dirname=param_path,
                           main_program=prog)

save_vars

paddle.fluid.io.save_vars(executor, dirname, main_program=None, vars=None, predicate=None, filename=None)[source]

Save variables to the given directory by executor.

There are two ways to specify variables to be saved: The first way, list variables in a list and assign it to the vars. The second way, assign the main_program with an existing program, then all variables in the program will be saved. The first way has a higher priority. In other words, if vars are assigned, the main_program and the predicate will be ignored.

The dirname are used to specify the folder where to save variables. If you prefer to save variables in separate files in the folder dirname, set filename None; if you prefer to save all variables in a single file, use filename to specify it.

Parameters:
  • executor (Executor) – The executor to run for saving variables.
  • dirname (str) – The directory path.
  • main_program (Program|None) – The program whose variables will be saved. If it is None, the default main program will be used automatically. Default: None
  • vars (list[Variable]|None) – The list that contains all variables to save. It has a higher priority than the main_program. Default: None
  • predicate (function|None) – If it is not None, only variables in the main_program that makes predicate(variable)==True will be saved. It only works when we are using the main_program to specify variables (In other words vars is None). Default: None
  • filename (str|None) – The file which to save all variables. If you prefer to save variables separately, set it to None. Default: None
Returns:

None

Raises:

TypeError – If main_program is not an instance of Program nor None.

Examples

import paddle.fluid as fluid
main_prog = fluid.Program()
startup_prog = fluid.Program()
with fluid.program_guard(main_prog, startup_prog):
    data = fluid.layers.data(name="img", shape=[64, 784], append_batch_size=False)
    w = fluid.layers.create_parameter(shape=[784, 200], dtype='float32', name='fc_w')
    b = fluid.layers.create_parameter(shape=[200], dtype='float32', name='fc_b')
    hidden_w = fluid.layers.matmul(x=data, y=w)
    hidden_b = fluid.layers.elementwise_add(hidden_w, b)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(startup_prog)

param_path = "./my_paddle_model"
# The first usage: using `main_program` to specify variables
def name_has_fc(var):
    res = "fc" in var.name
    return res
fluid.io.save_vars(executor=exe, dirname=param_path, main_program=main_prog,
                   vars=None, predicate = name_has_fc)
# All variables in `main_program` whose name includes "fc" will be saved.
# And variables are going to be saved separately.


# The second usage: using `vars` to specify variables
var_list = [w, b]
path = "./my_paddle_vars"
fluid.io.save_vars(executor=exe, dirname=path, vars=var_list,
                   filename="vars_file")
# var_a, var_b and var_c will be saved. And they are going to be
# saved in the same file named 'var_file' in the path "./my_paddle_vars".