HyperGP.GpOptimizer Module#
- class GpOptimizer[source]#
To use
GpOptimizermodule, we should first import it:Examples
>>> from HyperGP import GpOptimizer
Methods
__init__([states, module_states, parallel, ...])Initialize the optimizer
status_init(**kwargs)Register the needed states in the evolution.
iter_component(*args)Register the components want to be iteratively executed.
run(iter[, device, async_parallel, ...])Run the optimizer with iteration time and device
monitor(tool, track_object[, save_path])Register the components want to be iteratively executed.
detach()Copy the optimizer.
wait()Wait the optimizer finish when an asynchronous run is called.
enable(mod, **kwargs)Attributes
- __init__(states=None, module_states=None, parallel=True, gpu=True, cash=False, **kwargs)[source]#
Initialize the optimizer
- Parameters:
states (HyperGP.States)
module_states (HyperGP.States)
parallel (boolean)
gpu (boolean)
kwargs
- Returns:
a new
GPOptimizermodule
Examples
>>> optimizer = GpOptimizer()
- detach()[source]#
Copy the optimizer. It can be used to avoid repeatly register the same states or components.
- Returns:
A new
GpOptimizerwith independent same states.
Examples
>>> optimizers = [] >>> for i in range(5): ... optimizers.append(optimizer.detach())
>>> for optimizer in optimizers: ... optimizer.run(100, async_parallel=True) >>> for optimizer in optimizers: ... optimizer.wait()
- iter_component(*args)[source]#
Register the components want to be iteratively executed.
- Parameters:
args – The
HyperGP.states.ParaStatesmodule is needed to register each component.
Examples
>>> optimizer.iter_component( >>> ParaStates(func=HyperGP.RandTrCrv(), source=["p_list", "p_list"], to=["p_list", "p_list"], ... mask=set_prmask(pop_size)), >>> ParaStates(func=shuffle, source=["p_list"], to=["p_list"], ... mask=[1]), >>> ParaStates(func=HyperGP.RandTrMut(), source=["p_list", ProgBuildStates(pset=pset, depth_rg=[2, 3], len_limit=100), True], to=["p_list"], ... mask=[set_armask(pop_size), 1, 1]), >>> ParaStates(func=HyperGP.executor, source=["p_list", "input", "pset"], to=["output", None], ... mask=[1, 1, 1]), >>> ParaStates(func=HyperGP.evaluation, source=["output", "output_tensor"], to=["fit_list"], ... mask=[1, 1]) )
- monitor(tool, track_object, save_path=None)[source]#
Register the components want to be iteratively executed.
- Parameters:
tool – The monitor to be called
track_object (str-like or list) – States to be monitored
save_path – File path to save the results
Examples
>>> optimizer.monitor(HyperGP.monitors.statistics_record, "fit_list", "./records/fitness.txt")
./records/fitness.txt: min max mean var std 148792.54973409744 149393.69216920136 149353.97619133547 15368.910548484808 123.97141020608262 148730.17933285816 149387.63549782007 149297.24560745712 36924.96589522792 192.15869976461622 148758.9647289339 149387.54256014896 149171.7426450109 65744.58211613352 256.4070633117066 … )
- run(iter, device=[0], async_parallel=False, tqdm_diable=False, stop_criteria=None)[source]#
Run the optimizer with iteration time and device
- Parameters:
iter (int) – The iteration time
device (list) – GPU index list used in the optimizer
async_parallel (boolean) – Whether asynchronously execute the optimizer. If it is True, the method will immediately return, then a
waitmethod is needed to wait the evolution process finish.tqdm_diable (boolean) – Whether use a tqdm show
stop_criteria (func-like) – early stop condition
Examples
>>> optimizer.run(10)
or run it asynchronously:
>>> optimizer.run(10, async_parallel=True) >>> optimizer.wait()
early stop:
>>> optimizer.run(500, stop_criteria=lambda: HyperGP.tensor.min(optimizer.workflowstates.fit_list) < 1e10)
- status_init(**kwargs)[source]#
Register the needed states in the evolution.
The states will be stored in the
workflowstatesattribute, then we can get it with str-like object when we useiter_componentmethod, or just get it with attribute operator.Examples
>>> pop_size = 1000 >>> pset = HyperGP.PrimitiveSet(input_arity=1, primitive_set=[('add', HyperGP.tensor.add, 2),('sub', HyperGP.tensor.sub, 2)]) >>> pop = HyperGP.Population() >>> pop.initPop(pop_size=pop_size, prog_paras=ProgBuildStates(pset=pset, depth_rg=[2, 3], len_limit=10000)) >>> pop.stateRegister(cprogs = pop.states['progs'].copy) >>> input_array = HyperGP.tensor.uniform(0, 10, size=(1, 10000)) >>> optimizer = HyperGP.GpOptimizer() >>> optimizer.status_init( ... p_list=pop.states['cprogs'].indivs, fit_list = pop.states['cprogs'].fitness, input=input_array, pset=pset, output=None)