fluid.executor

Executor

class paddle.fluid.executor.Executor(place)[source]

An Executor in Python, supports single/multiple-GPU running, and single/multiple-CPU running. Python executor takes a program, adds feed operators and fetch operators to this program according to feed map and fetch_list. Feed map provides input data for the program. fetch_list provides the variables(or names) that user wants to get after program runs. Note: the executor will run all operators in the program but not only the operators dependent by the fetch_list. It stores the global variables into the global scope, and creates a local scope for the temporary variables. The contents in local scope may be discarded after every minibatch forward/backward finished. But the global scope variables will be persistent through different runs.

Examples

import paddle.fluid as fluid
import paddle.fluid.compiler as compiler
import numpy
import os

use_cuda = True
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)

train_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(train_program, startup_program):
    data = fluid.layers.data(name='X', shape=[1], dtype='float32')
    hidden = fluid.layers.fc(input=data, size=10)
    loss = fluid.layers.mean(hidden)
    fluid.optimizer.SGD(learning_rate=0.01).minimize(loss)

# Run the startup program once and only once.
# Not need to optimize/compile the startup program.
startup_program.random_seed=1
exe.run(startup_program)

# Run the main program directly without compile.
x = numpy.random.random(size=(10, 1)).astype('float32')
loss_data, = exe.run(train_program,
                     feed={"X": x},
                     fetch_list=[loss.name])

# Or, compiled the program and run. See `CompiledProgram`
# for more detail.
# NOTE: If you use CPU to run the program, you need
# to specify the CPU_NUM, otherwise, fluid will use
# all the number of the logic core as the CPU_NUM,
# in that case, the batch size of the input should be
# greater than CPU_NUM, if not, the process will be
# failed by an exception.
if not use_cuda:
    os.environ['CPU_NUM'] = str(2)

compiled_prog = compiler.CompiledProgram(
    train_program).with_data_parallel(
    loss_name=loss.name)
loss_data, = exe.run(compiled_prog,
                     feed={"X": x},
                     fetch_list=[loss.name])
Parameters:place (fluid.CPUPlace|fluid.CUDAPlace(n)) – indicate the executor run on which device.
close()[source]

Close this executor.

You can no longer use this executor after calling this method. For the distributed training, this method would free the resource on PServers related to the current Trainer.

Examples

import paddle.fluid as fluid

cpu = fluid.CPUPlace()
exe = fluid.Executor(cpu)
# execute training or testing
exe.close()
run(program=None, feed=None, fetch_list=None, feed_var_name='feed', fetch_var_name='fetch', scope=None, return_numpy=True, use_program_cache=False)[source]

Run program by this Executor. Feed data by feed map, fetch result by fetch_list. Python executor takes a program, add feed operators and fetch operators to this program according to feed map and fetch_list. Feed map provides input data for the program. fetch_list provides the variables(or names) that user want to get after program run.

Note: the executor will run all operators in the program but not only the operators dependent by the fetch_list.

Examples

import paddle.fluid as fluid
import numpy

# First create the Executor.
place = fluid.CPUPlace() # fluid.CUDAPlace(0)
exe = fluid.Executor(place)

data = fluid.layers.data(name='X', shape=[1], dtype='float32')
hidden = fluid.layers.fc(input=data, size=10)
loss = fluid.layers.mean(hidden)
adam = fluid.optimizer.Adam()
adam.minimize(loss)

# Run the startup program once and only once.
exe.run(fluid.default_startup_program())

x = numpy.random.random(size=(10, 1)).astype('float32')
outs = exe.run(feed={'X': x},
               fetch_list=[loss.name])
Parameters:
  • program (Program|CompiledProgram) – the program that need to run, if not provided, then default_main_program (not compiled) will be used.
  • feed (dict) – feed variable map, e.g. {“image”: ImageData, “label”: LabelData}
  • fetch_list (list) – a list of variable or variable names that user wants to get, this method will return them according to this list.
  • feed_var_name (str) – the name for the input variable of feed Operator.
  • fetch_var_name (str) – the name for the output variable of fetch Operator.
  • scope (Scope) – the scope used to run this program, you can switch it to different scope. default is global_scope
  • return_numpy (bool) – if convert the fetched tensor to numpy
  • use_program_cache (bool) – whether to use the cached program settings across batches. Setting it be true would be faster only when (1) the program is not compiled with data parallel, and (2) program, feed variable names and fetch_list variable names do not changed compared to the last step.
Returns:

fetch result according to fetch_list.

Return type:

list(numpy.array)

infer_from_dataset(program=None, dataset=None, scope=None, thread=0, debug=False, fetch_list=None, fetch_info=None, print_period=100)[source]

The document of infer_from_dataset is almost the same as train_from_dataset, except that in distributed training, push gradients will be disabled in infer_from_dataset. infer_from_dataset() can be used for evaluation in multi-thread very easily.

Parameters:
  • program (Program|CompiledProgram) – the program that needs to be run, if not provided, then default_main_program (not compiled) will be used.
  • dataset (paddle.fluid.Dataset) – dataset created outside this function, a user should provide a well-defined dataset before calling this function. Please check the document of Dataset if needed. default is None
  • scope (Scope) – the scope used to run this program, you can switch it to different scope for each run. default is global_scope
  • thread (int) – number of thread a user wants to run in this function. The actual number of thread will be min(Dataset.thread_num, thread) if thread > 0, default is 0
  • debug (bool) – whether a user wants to run infer_from_dataset, default is False
  • fetch_list (Variable List) – fetch variable list, each variable will be printed during training, default is None
  • fetch_info (String List) – print information for each variable, default is None
  • print_period (int) – the number of mini-batches for each print, default is 100
Returns:

None

Examples

import paddle.fluid as fluid

place = fluid.CPUPlace() # you can set place = fluid.CUDAPlace(0) to use gpu
exe = fluid.Executor(place)
x = fluid.layers.data(name="x", shape=[10, 10], dtype="int64")
y = fluid.layers.data(name="y", shape=[1], dtype="int64", lod_level=1)
dataset = fluid.DatasetFactory().create_dataset()
dataset.set_use_var([x, y])
dataset.set_thread(1)
filelist = [] # you should set your own filelist, e.g. filelist = ["dataA.txt"]
dataset.set_filelist(filelist)
exe.run(fluid.default_startup_program())
exe.infer_from_dataset(program=fluid.default_main_program(),
                       dataset=dataset)
train_from_dataset(program=None, dataset=None, scope=None, thread=0, debug=False, fetch_list=None, fetch_info=None, print_period=100)[source]

Train from a pre-defined Dataset. Dataset is defined in paddle.fluid.dataset. Given a program, either a program or compiled program, train_from_dataset will consume all data samples in dataset. Input scope can be given by users. By default, scope is global_scope(). The total number of thread run in training is thread. Thread number used in training will be minimum value of threadnum in Dataset and the value of thread in this interface. Debug can be set so that executor will display Run-Time for all operators and the throughputs of current training task.

Note: train_from_dataset will destroy all resources created within executor for each run.

Parameters:
  • program (Program|CompiledProgram) – the program that needs to be run, if not provided, then default_main_program (not compiled) will be used.
  • dataset (paddle.fluid.Dataset) – dataset created outside this function, a user should provide a well-defined dataset before calling this function. Please check the document of Dataset if needed.
  • scope (Scope) – the scope used to run this program, you can switch it to different scope for each run. default is global_scope
  • thread (int) – number of thread a user wants to run in this function. The actual number of thread will be min(Dataset.thread_num, thread)
  • debug (bool) – whether a user wants to run train_from_dataset
  • fetch_list (Variable List) – fetch variable list, each variable will be printed during training
  • fetch_info (String List) – print information for each variable
  • print_period (int) – the number of mini-batches for each print
Returns:

None

Examples

import paddle.fluid as fluid

place = fluid.CPUPlace() # you can set place = fluid.CUDAPlace(0) to use gpu
exe = fluid.Executor(place)
x = fluid.layers.data(name="x", shape=[10, 10], dtype="int64")
y = fluid.layers.data(name="y", shape=[1], dtype="int64", lod_level=1)
dataset = fluid.DatasetFactory().create_dataset()
dataset.set_use_var([x, y])
dataset.set_thread(1)
filelist = [] # you should set your own filelist, e.g. filelist = ["dataA.txt"]
dataset.set_filelist(filelist)
exe.run(fluid.default_startup_program())
exe.train_from_dataset(program=fluid.default_main_program(),
                       dataset=dataset)

global_scope

paddle.fluid.executor.global_scope()[source]

Get the global/default scope instance. There are a lot of APIs use global_scope as its default value, e.g., Executor.run

Examples

import paddle.fluid as fluid
import numpy

fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace())
numpy.array(fluid.global_scope().find_var("data").get_tensor())
Returns:The global/default scope instance.
Return type:Scope

scope_guard

paddle.fluid.executor.scope_guard(scope)[source]

Change the global/default scope instance by Python with statement. All variable in runtime will assigned to the new scope.

Parameters:scope – The new global/default scope.

Examples

import paddle.fluid as fluid
import numpy

new_scope = fluid.Scope()
with fluid.scope_guard(new_scope):
     fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace())
numpy.array(new_scope.find_var("data").get_tensor())