Function evaluation¶
The Evaluator classes provide an abstraction layer that makes it
easier to implement sequential and/or parallel evaluation of functions.
Example:
f = pints.SumOfSquaresError(problem)
e = pints.ParallelEvaluator(f)
x = [[1, 2],
[3, 4],
[5, 6],
[7, 8],
]
fx = e.evaluate(x)
- pints.evaluate(f, x, parallel=False, args=None)[source]¶
Evaluates the function
fon every value present inxand returns a sequence of evaluationsf(x[i]).It is possible for the evaluation of
fto involve the generation of random numbers (using numpy). In this case, the results from callingevaluatecan be made reproducible by first seeding numpy’s generator with a fixed number. However, a call withparallel=Truewill use a different (but consistent) sequence of random numbers than a call withparallel=False.- Parameters:
f (callable) – The function to evaluate, called as
f(x[i], *args).x – A list of values to evaluate
fwithparallel (boolean) – Run in parallel or not. If set to
True, the evaluations will happen in parallel using a number of worker processes equal to the detected cpu core count. The number of workers can be set explicitly by settingparallelto an integer greater than 0. Parallelisation can be disabled by settingparallelto0orFalse.args (sequence) – Optional extra arguments to pass into
f.
- class pints.Evaluator(function, args=None)[source]¶
Abstract base class for classes that take a function (or callable object)
f(x)and evaluate it for list of input valuesx.This interface is shared by a parallel and a sequential implementation, allowing easy switching between parallel or sequential implementations of the same algorithm.
It is possible for the evaluation of
fto involve the generation of random numbers (using numpy). In this case, the results from callingevaluatecan be made reproducible by first seeding numpy’s generator with a fixed number. However, differentEvaluatorimplementations may use a different random sequence. In other words, each Evaluator can be made to return consistent results, but the results returned by different Evaluators may vary.- Parameters:
function (callable) – A function or other callable object
fthat takes a valuexand returns an evaluationf(x).args (sequence) – An optional sequence of extra arguments to
f. Ifargsis specified,fwill be called asf(x, *args).
- class pints.ParallelEvaluator(function, n_workers=None, max_tasks_per_worker=500, n_numpy_threads=1, args=None)[source]¶
Evaluates a single-valued function object for any set of input values given, using all available cores.
Shares an interface with the
SequentialEvaluator, allowing parallelism to be switched on and off with minimal hassle. Parallelism takes a little time to be set up, so as a general rule of thumb it’s only useful for if the total run-time is at least ten seconds (anno 2015).By default, the number of processes (“workers”) used to evaluate the function is set equal to the number of CPU cores reported by python’s
multiprocessingmodule. To override the number of workers used, setn_workersto some integer greater than0.There are two important caveats for using multiprocessing to evaluate functions:
Processes don’t share memory. This means the function to be evaluated will be duplicated (via pickling) for each process (see Avoid shared state for details).
On windows systems your code should be within an
if __name__ == '__main__':block (see Windows for details).
The evaluator will keep it’s subprocesses alive and running until it is tidied up by garbage collection.
Note that while this class uses multiprocessing, it is not thread/process safe itself: It should not be used by more than a single thread/process at a time.
Extends
Evaluator.- Parameters:
function – The function to evaluate
n_workers – The number of worker processes to use. If left at the default value
n_workers=Nonethe number of workers will equal the number of CPU cores in the machine this is run on. In many cases this will provide good performance.max_tasks_per_worker – Python garbage collection does not seem to be optimized for multi-process function evaluation. In many cases, some time can be saved by refreshing the worker processes after every
max_tasks_per_workerevaluations. This number can be tweaked for best performance on a given task / system.n_numpy_threads – Numpy and other scientific libraries may make use of threading in C or C++ based BLAS libraries, which can interfere with PINTS multiprocessing and cause slower execution. To prevent this, the number of threads to use will be limited to 1 by default, using the
threadpoolctlmodule. To use the current numpy default instead, setn_numpy_threadstoNone, to use the BLAS/OpenMP etc. defaults, setn_numpy_threadsto0, or to use a specific number of threads pass in any integer greater than 1.args – An optional sequence of extra arguments to
f. Ifargsis specified,fwill be called asf(x, *args).
- static cpu_count()[source]¶
Uses the multiprocessing module to guess the number of available cores.
For machines with simultaneous multithreading (“hyperthreading”) this will return the number of virtual cores.
- evaluate(positions)¶
Evaluate the function for every value in the sequence
positions.Returns a list with the returned evaluations.
- class pints.SequentialEvaluator(function, args=None)[source]¶
Evaluates a function (or callable object) for a list of input values, and returns a list containing the calculated function evaluations.
Runs sequentially, but shares an interface with the
ParallelEvaluator, allowing parallelism to be switched on/off.Extends
Evaluator.- Parameters:
function (callable) – The function to evaluate.
args (sequence) – An optional tuple containing extra arguments to
f. Ifargsis specified,fwill be called asf(x, *args).
- evaluate(positions)¶
Evaluate the function for every value in the sequence
positions.Returns a list with the returned evaluations.
- class pints.MultiSequentialEvaluator(functions, args=None)[source]¶
Evaluates a list of functions (or callable objects) for a list of input values of the same length, and returns a list containing the calculated function evaluations.
This evaluator should be used in the case where each position (for example, corresponding to each chain) needs to be evaluated on a separate function. In this way, it differs from
SequentialEvaluatorandParallelEvaluator, which evaluate multiple positions on a single callable function.Extends
Evaluator.- Parameters:
functions (list of callable) – The functions to evaluate.
args (sequence) – An optional tuple containing extra arguments to each element in functions,
f. Ifargsis specified,fwill be called asf(x, *args).
- evaluate(positions)¶
Evaluate the function for every value in the sequence
positions.Returns a list with the returned evaluations.