HyperGP.PrimitiveSet Module#
- class PrimitiveSet[source]#
PrimitiveSetmodule is used to collects the primitives and terminals used in GP evolutionTo use the PrimitiveSet, we should firstly import it from HyperGP and initilize it:
>>> from HyperGP import PrimitiveSet
Methods
__init__(input_arity[, primitive_set, prefix])registerPrimitive(name, func, arity[, states])We can also register function after the
PrimitiveSetmodule has been initialized.registerTerminal([name])Except for the auto generated terminals, we can also register terminals after the
PrimitiveSetmodule has been initialized.registerEphemeralTerminal(name, func[, ...])Used to generate the terminal with functions
genFunc(name)Search the register function with its name
genTerminal(name)Search the register terminal with its name
select()randomly select from the primitive set
randomly select a function from the primitive set
randomly select a terminal from the primitive set
copy()A deep copy of the primitive set
Attributes
Get a name list of the register functions
Get a name list of the terminals
- __init__(input_arity, primitive_set=None, prefix='x')#
- Parameters:
input_arity (int) – the number of terminals.
primitive_set (list) – a list with the format [(name, func, arity, states), ..]. for each prim, a tuple with name, function, arity is needed. To make the framework more flexible, it is also supported to register the states with States module for each prim, just add it behind the three element in each tuple. The detailed examples will be provided below.
prefix (str) – determine the name used for input terminals. The pre-defined str is ‘x’, then the terminals will be printed like ‘x1’, ‘x2’, … .
Examples
Initialize the PrimitiveSet
>>> pset = PrimitiveSet( >>> input_arity=1, >>> primitive_set=[ >>> ('add', HyperGP.tensor.add, 2), >>> ('sub', HyperGP.tensor.sub, 2), >>> ('mul', HyperGP.tensor.mul, 2), >>> ('div', HyperGP.tensor.div, 2), >>> ])
For each prim, we can also register additional states. For example, when we apply HyperGP to image classification:
>>> pset = PrimitiveSet( >>> input_arity=1, >>> primitive_set=[ ("gau_filter", HyperGP.gauss_filter, 3, States(type="filter")) >>> ]) >>> print(pset.genFunc("gau_filter").type) filter
- copy()#
A deep copy of the primitive set
- Returns:
A new
PrimitiveSet
- genFunc(name)#
Search the register function with its name
- Parameters:
name (str) – the function name want to search
- Returns:
The callable register function
Examples
>>> pset = PrimitiveSet( >>> input_arity=5, >>> primitive_set=[ >>> ('add', HyperGP.tensor.add, 2), >>> ('sub', HyperGP.tensor.sub, 2), >>> ]) >>> prim = pset.genFunc('add') >>> print(prim, type(prim)) add, <class 'HyperGP.base.func_basic.Func'>
- genTerminal(name)#
Search the register terminal with its name
- Parameters:
name (str) – the terminal name want to search
- Returns:
The register terminal.
Note
If it is a callable ephemeral constant, then the generated term is a
Constantmodule with the return value of the function.Examples
>>> pset = PrimitiveSet( >>> input_arity=5, >>> primitive_set=[ >>> ('add', HyperGP.tensor.add, 2), >>> ('sub', HyperGP.tensor.sub, 2), >>> ]) >>> term = pset.genTerminal('x0') >>> print(term, type(term)) x0, <class 'HyperGP.base.func_basic.Terminal'>
- max_arity()#
Statistics the max arity of the registered function
- Returns:
A new
PrimitiveSet
- property primitiveSet#
Get a name list of the register functions
- Returns:
The name list of the register functions
Examples
>>> pset = PrimitiveSet( >>> input_arity=1, >>> primitive_set=[ >>> ('add', HyperGP.tensor.add, 2), >>> ('sub', HyperGP.tensor.sub, 2), >>> ]) >>> print(pset.primitiveSet) ['add', 'sub']
- registerEphemeralTerminal(name, func, ephemeral=True, **kwargs)#
Used to generate the terminal with functions
- Parameters:
name (str) – a sign of the terminal. It is used to search the register terminal, with
genTerminalfunction.function (function-like) – The function called when get it from the
PrimitiveSetmodule
Examples
>>> pset = PrimitiveSet( >>> input_arity=2 >>> ) >>> def constants(): ... return random.uniform(0, 1) >>> pset.registerEphemeralTerminal("y", constants) >>> term = pset.genTerminal("y") >>> print(term, type(term)) 0.9997361496151884 <class 'HyperGP.base.func_basic.Constant'>
- registerPrimitive(name, func, arity, states=None, **kwargs)#
We can also register function after the
PrimitiveSetmodule has been initialized.- Parameters:
name (str) – a sign of the function, which will be shown when print the function. It is also used to search the register function, with
genFuncfunction.func (function-like) – the function want to register.
arity (int) – the arity of the register function.
states (HyperGP.States) – the states want to register in the function, using
HyperGP.Statesmodule.kwargs – The input kwargs for each prim will be used as default parameters whenever the prim is called.
Examples
>>> pset = PrimitiveSet( >>> input_arity=1 >>> ) >>> >>> param_types_1 = ["img", "mask", "channel"] >>> param_types_2 = ["img", "channel"] >>> param_types_3 = ["img", "w_h", "region"] >>> >>> pset.registerPrimitive("gau_filter", HyperGP.gauss_filter, 3, states=States(type="filter", param=param_types_1), padding=(1, 1)) >>> pset.registerPrimitive("sobel_filter", HyperGP.sobel_filter, 2, states=States(type="filter", param=param_types_2), padding=(1, 1)) >>> pset.registerPrimitive("mean_filter", HyperGP.mean_filter, 3, states=States(param=param_types_1, type="filter"), padding=(1, 1)) >>> pset.registerPrimitive("mean", s_mean, 1, states=States(param=["img"], type="norm")) >>> pset.registerPrimitive("region_detect", region_detect, 3, states=States(param=param_types_3, type="region"))
- registerTerminal(name=None, **kwargs)#
Except for the auto generated terminals, we can also register terminals after the
PrimitiveSetmodule has been initialized.- Parameters:
name (str) – a sign of the terminal, which will be shown when print it. It is also used to search the register terminal, with
genTerminalfunction.kwargs – will be registered to the attrs of the terminal.
Examples
>>> pset = PrimitiveSet( >>> input_arity=2 >>> ) >>> >>> pset.registerTerminal("y") >>> print(pset.terminalSet) ['x0', 'x1', 'y'] >>> pset.registerTerminal() >>> print(pset.terminalSet) ['x0', 'x1', 'y', 'x2']
- select()[source]#
randomly select from the primitive set
- Returns:
a
Func,TerminalorConstantmodule
Examples
>>> prim = pset.select() >>> print(prim, type(prim)) div, <class 'HyperGP.base.func_basic.Func'>
- selectFunc()[source]#
randomly select a function from the primitive set
- Returns:
a
Funcmodule
Examples
>>> prim = pset.selectFunc() >>> print(prim, type(prim)) add, <class 'HyperGP.base.func_basic.Func'>
- selectTerminal()[source]#
randomly select a terminal from the primitive set
- Returns:
a
TerminalorConstantmodule
Examples
>>> term = pset.selectTerminal() >>> print(term, type(term)) x0, <class 'HyperGP.base.func_basic.Terminal'>
- property terminalSet#
Get a name list of the terminals
- Returns:
The name list of the register terminals
Examples
>>> pset = PrimitiveSet( >>> input_arity=5, >>> primitive_set=[ >>> ('add', HyperGP.tensor.add, 2), >>> ('sub', HyperGP.tensor.sub, 2), >>> ]) >>> print(pset.terminalSet) ['x0', 'x1', 'x2', 'x3', 'x4']