towhee.hparam.hyperparameter.HyperParameter

class towhee.hparam.hyperparameter.HyperParameter(**kws)[source]

Bases: dict

HyperParameter is an extended dict with features for better parameter management.

A HyperParameter can be created with: >>> hp = HyperParameter(param1=1, param2=2, obj1={‘propA’: ‘A’})

or

>>> hp = HyperParameter(**{'param1': 1, 'param2': 2, 'obj1': {'propA': 'A'}})

Once the HyperParameter object is created, you can access the values using the object-style api: >>> hp.param1 1 >>> hp.obj1.propA ‘A’

or using the dict-style api (for legacy codes): >>> hp[‘param1’] 1 >>> hp[‘obj1’][‘propA’] ‘A’

The object-style api also support creating or updating the parameters: >>> hp.a.b.c = 1

which avoid maintaining the dict data manually like this: >>> hp = {} >>> if ‘a’ not in hp: hp[‘a’] = {} >>> if ‘b’ not in hp[‘a’]: hp[‘a’][‘b’] = {} >>> hp[‘a’][‘b’][‘c’] = 1

You can also create a parameter with a string name: >>> hp = HyperParameter() >>> hp.put(‘a.b.c’, 1)

Methods

callholder

clear

copy

dispatch

Return a call holder.

fromkeys

Create a new dictionary with keys from iterable and values set to value.

get

get a parameter by a string name

items

keys

load

Load parameters from json file, similar as json.load.

loads

Load parameters from JSON string, similar as json.loads.

pop

If key is not found, default is returned if given, otherwise KeyError is raised

popitem

Remove and return a (key, value) pair as a 2-tuple.

put

put/update a parameter with a string name

setdefault

Insert key with a value of default if key is not in the dictionary.

update

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values

__call__() Any[source]

Return a parameter accessor.

Returns:

holder of the current parameter

Return type:

Any

Examples: >>> cfg = HyperParameter(a=1, b = {‘c’:2, ‘d’: 3}) >>> cfg().a.get_or_else(‘default’) # default value for simple parameter 1 >>> cfg().b.c.get_or_else(‘default’) # default value for nested parameter 2 >>> cfg().b.undefined.get_or_else(‘default’) ‘default’

__getattr__(name)[source]

read parameter with object-style api

Examples:

for simple parameters: >>> hp = HyperParameter(a=1, b = {‘c’:2, ‘d’: 3}) >>> hp.a 1

for nested parameters: >>> hp.b.c 2

>>> getattr(hp, 'b.c')
2
__getitem__()

x.__getitem__(y) <==> x[y]

__init__(**kws)[source]
__or__(value, /)

Return self|value.

__repr__()

Return repr(self).

__setitem__(key, value)[source]

set value and convert the value into HyperParameter if necessary

clear() None.  Remove all items from D.
copy() a shallow copy of D
dispatch(callback: Optional[Callable] = None)[source]

Return a call holder.

Examples: >>> def debug_print(path, index, *arg, **kws): … return (path, index, arg, kws) >>> ch = param_scope().dispatch(debug_print) >>> ch.my.foo(a=1,b=2) (‘my.foo’, None, (), {‘a’: 1, ‘b’: 2})

>>> ch.myspace2.gee(c=1,d=2)
('myspace2.gee', None, (), {'c': 1, 'd': 2})
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(name: str) Any[source]

get a parameter by a string name

Parameters:

name (str) – parameter name

Returns:

parameter value

Return type:

Any

Examples: >>> cfg = HyperParameter(a=1, b = {‘c’:2, ‘d’: 3}) >>> cfg.get(‘a’) 1 >>> cfg.get(‘b.c’) 2

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
static load(f)[source]

Load parameters from json file, similar as json.load.

static loads(s)[source]

Load parameters from JSON string, similar as json.loads.

pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, default is returned if given, otherwise KeyError is raised

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

put(name: str, value: Any)[source]

put/update a parameter with a string name

Parameters:
  • name (str) – parameter name, ‘obj.prop’ is supported

  • value (Any) – parameter value

Examples: >>> cfg = HyperParameter() >>> cfg.put(‘param1’, 1) >>> cfg.put(‘obj1.propA’, ‘A’)

>>> cfg.param1
1
>>> cfg.obj1.propA
'A'
setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values