Executor¶
- class paddle.static. Executor ( place=None ) [source]
-
- Api_attr
-
Static Graph
An Executor in Python, supports single/multiple-GPU running, and single/multiple-CPU running.
- Parameters
-
place (paddle.CPUPlace()|paddle.CUDAPlace(n)|str|None) – This parameter represents which device the executor runs on. When this parameter is None, PaddlePaddle will set the default device according to its installation version. If Paddle is CPU version, the default device would be set to CPUPlace() . If Paddle is GPU version, the default device would be set to CUDAPlace(0) . Default is None. If
place
is string, it can becpu
, andgpu:x
, wherex
is the index of the GPUs. Note: users only pass one Place or None to initialize Executor when using multiple-cards. Other APIs will override the cards. See document for multiple-cards - Returns
-
Executor
Examples
>>> import paddle >>> import numpy >>> import os >>> # Executor is only used in static graph mode >>> paddle.enable_static() >>> # Set place explicitly. >>> # use_cuda = True >>> # place = paddle.CUDAPlace(0) if use_cuda else paddle.CPUPlace() >>> # exe = paddle.static.Executor(place) >>> # If you don't set place, PaddlePaddle sets the default device. >>> exe = paddle.static.Executor() >>> train_program = paddle.static.Program() >>> startup_program = paddle.static.Program() >>> with paddle.static.program_guard(train_program, startup_program): ... data = paddle.static.data(name='X', shape=[None, 1], dtype='float32') ... hidden = paddle.static.nn.fc(data, 10) ... loss = paddle.mean(hidden) ... paddle.optimizer.SGD(learning_rate=0.01).minimize(loss) ... >>> # Run the startup program once and only once. >>> # Not need to optimize/compile the startup program. >>> 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 details. >>> compiled_prog = paddle.static.CompiledProgram( ... train_program) >>> loss_data, = exe.run(compiled_prog, feed={"X": x}, fetch_list=[loss.name])
-
close
(
)
close¶
-
Close the executor. This interface is used for distributed training (PServers mode). This executor can not be used after calling the interface, because this interface releases resources associated with the current Trainer.
- Returns
-
None
Examples
>>> import paddle >>> cpu = paddle.CPUPlace() >>> exe = paddle.static.Executor(cpu) >>> # execute training or testing >>> exe.close()
-
flush
(
)
flush¶
-
flush all trainer param to root_scope
-
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,
use_prune=False
)
run¶
-
Run the specified
Program
orCompiledProgram
. It should be noted that the executor will execute all the operators inProgram
orCompiledProgram
without pruning some operators of theProgram
orCompiledProgram
according to fetch_list. And you could specify the scope to store theTensor
during the executor running if the scope is not set, the executor will use the global scope, i.e.paddle.static.global_scope()
.- Parameters
-
program (Program|CompiledProgram) – This parameter represents the
Program
orCompiledProgram
to be executed. If this parameter is not provided, that parameter is None, the program will be set topaddle.static.default_main_program()
. The default is None.feed (list|dict) – This parameter represents the input Tensors of the model. If it is single card training, the feed is dict type, and if it is multi-card training, the parameter feed can be dict or list of Tensors. If the parameter type is dict, the data in the feed will be split and sent to multiple devices (CPU/GPU), that is to say, the input data will be evenly sent to different devices, so you should make sure the number of samples of the current mini-batch must be greater than the number of places; if the parameter type is list, those data are copied directly to each device, so the length of this list should be equal to the number of places. The default is None.
fetch_list (list) – This parameter represents the Tensors that need to be returned after the model runs. The default is None.
feed_var_name (str) – This parameter represents the name of the input Tensor of the feed operator. The default is “feed”.
fetch_var_name (str) – This parameter represents the name of the output Tensor of the fetch operator. The default is “fetch”.
scope (Scope) – the scope used to run this program, you can switch it to different scope. default is
paddle.static.global_scope()
return_numpy (bool) – This parameter indicates whether convert the fetched Tensors (the Tensor specified in the fetch list) to numpy.ndarray. if it is False, the type of the return value is a list of
LoDTensor
. The default is True.use_program_cache (bool) – This parameter indicates whether the input
Program
is cached. If the parameter is True, the model may run faster in the following cases: the input program ispaddle.static.Program
, and the parameters(program, feed Tensor name and fetch_list Tensor) of this interface remains unchanged during running. The default is False.use_prune (bool) – This parameter indicates whether the input
Program
will be pruned. If the parameter is True, the program will be pruned according to the given feed and fetch_list, which means the operators and variables in program that generatefeed
and are not needed to generatefetch_list
will be pruned. The default is False, which means the program will not pruned and all the operators and variables will be executed during running. Note that if the tuple returned fromOptimizer.minimize()
is passed tofetch_list
,use_prune
will be overridden to True, and the program will be pruned.
- Returns
-
The fetched result list.
- Return type
-
List
Examples
>>> import paddle >>> import numpy >>> # First create the Executor. >>> paddle.enable_static() >>> place = paddle.CPUPlace() # paddle.CUDAPlace(0) >>> exe = paddle.static.Executor(place) >>> data = paddle.static.data(name='X', shape=[None, 1], dtype='float32') >>> hidden = paddle.static.nn.fc(data, 10) >>> loss = paddle.mean(hidden) >>> adam = paddle.optimizer.Adam() >>> adam.minimize(loss) >>> i = paddle.zeros(shape=[1], dtype='int64') >>> array = paddle.tensor.array_write(x=loss, i=i) >>> # Run the startup program once and only once. >>> exe.run(paddle.static.default_startup_program()) >>> x = numpy.random.random(size=(10, 1)).astype('float32') >>> loss_val, array_val = exe.run(feed={'X': x}, ... fetch_list=[loss.name, array.name]) >>> print(array_val) >>> [array(0.16870381, dtype=float32)] >>>
>>> >>> import paddle >>> import numpy as np >>> # First create the Executor. >>> paddle.enable_static() >>> place = paddle.CUDAPlace(0) >>> exe = paddle.static.Executor(place) >>> data = paddle.static.data(name='X', shape=[None, 1], dtype='float32') >>> class_dim = 2 >>> prediction = paddle.static.nn.fc(data, class_dim) >>> loss = paddle.mean(prediction) >>> adam = paddle.optimizer.Adam() >>> adam.minimize(loss) >>> # Run the startup program once and only once. >>> exe.run(paddle.static.default_startup_program()) >>> build_strategy = paddle.static.BuildStrategy() >>> binary = paddle.static.CompiledProgram( ... paddle.static.default_main_program(), build_strategy=build_strategy) >>> batch_size = 6 >>> x = np.random.random(size=(batch_size, 1)).astype('float32') >>> prediction, = exe.run(binary, ... feed={'X': x}, ... fetch_list=[prediction.name]) >>> # If the user uses two GPU cards to run this python code, the printed result will be >>> # (6, class_dim). The first dimension value of the printed result is the batch_size. >>> print("The prediction shape: {}".format( ... np.array(prediction).shape)) The prediction shape: (6, 2) >>> print(prediction) >>> [[-0.37789783 -0.19921964] [-0.3577645 -0.18863106] [-0.24274671 -0.12814042] [-0.24635398 -0.13003758] [-0.49232286 -0.25939852] [-0.44514108 -0.2345845 ]] >>>
-
infer_from_dataset
(
program=None,
dataset=None,
scope=None,
thread=0,
debug=False,
fetch_list=None,
fetch_info=None,
print_period=100,
fetch_handler=None
)
infer_from_dataset¶
-
Infer from a pre-defined Dataset. Dataset is defined in paddle.base.dataset. Given a program, either a program or compiled program, infer_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 infer task.
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-threadvery 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.base.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. Default is 0, which means using thread num of dataset
debug (bool) – whether a user wants to run infer_from_dataset, default is False
fetch_list (Tensor List) – fetch Tensor list, each Tensor will be printed during training, default is None
fetch_info (String List) – print information for each Tensor, default is None
print_period (int) – the number of mini-batches for each print, default is 100
fetch_handler (FetchHandler) – a user define class for fetch output.
- Returns
-
None
Examples
>>> import paddle >>> paddle.enable_static() >>> place = paddle.CPUPlace() # you can set place = paddle.CUDAPlace(0) to use gpu >>> exe = paddle.static.Executor(place) >>> x = paddle.static.data(name="x", shape=[None, 10, 10], dtype="int64") >>> y = paddle.static.data(name="y", shape=[None, 1], dtype="int64", lod_level=1) >>> dataset = paddle.base.DatasetFactory().create_dataset() >>> dataset.set_use_var([x, y]) >>> dataset.set_thread(1) >>> # you should set your own filelist, e.g. filelist = ["dataA.txt"] >>> filelist = [] >>> dataset.set_filelist(filelist) >>> exe.run(paddle.static.default_startup_program()) >>> exe.infer_from_dataset(program=paddle.static.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,
fetch_handler=None
)
train_from_dataset¶
-
Train from a pre-defined Dataset. Dataset is defined in paddle.base.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.base.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. Default is 0, which means using thread num of dataset
debug (bool) – whether a user wants to run train_from_dataset
fetch_list (Tensor List) – fetch Tensor list, each variable will be printed during training
fetch_info (String List) – print information for each Tensor, its length should be equal to fetch_list
print_period (int) – the number of mini-batches for each print, default is 100
fetch_handler (FetchHandler) – a user define class for fetch output.
- Returns
-
None
Examples
>>> import paddle >>> paddle.enable_static() >>> place = paddle.CPUPlace() # you can set place = paddle.CUDAPlace(0) to use gpu >>> exe = paddle.static.Executor(place) >>> x = paddle.static.data(name="x", shape=[None, 10, 10], dtype="int64") >>> y = paddle.static.data(name="y", shape=[None, 1], dtype="int64", lod_level=1) >>> dataset = paddle.base.DatasetFactory().create_dataset() >>> dataset.set_use_var([x, y]) >>> dataset.set_thread(1) >>> # you should set your own filelist, e.g. filelist = ["dataA.txt"] >>> filelist = [] >>> dataset.set_filelist(filelist) >>> exe.run(paddle.static.default_startup_program()) >>> exe.train_from_dataset(program=paddle.static.default_main_program(), ... dataset=dataset)