def datalink_recv(socket, packet): opt = hp.Opt().loads(packet._data.decode()) #print(opt) # Set learning rate if opt.t == 'cmd': if opt.a == 'set': if opt.key == 'lr': #hp.train.set_lr_val(opt.val) pass hp.util.datalink().send_opt_sock(socket, hp.Opt(ACK='OK'))
def init_config(self): self._config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) self._config.read(self._args.c) self._opt = hp.Opt() # load config to opt for section in self._config.sections(): opt = hp.Opt() for key in self._config[section]: val_str = self._config[section][key] val = json.loads(val_str) opt[key] = val self._opt[section] = opt # override with command line args for arg in vars(self._args): val = getattr(self._args, arg) if val is None: continue val_str = str(val) if arg in self._opt.args or arg in self._default_config['args']: if arg in self._opt.args: opt_val = self._opt.args[arg] else: opt_val = self._default_config['args'][arg] if isinstance(opt_val, str): val_str = '"' + val_str + '"' if type(opt_val) is not type(val): print("[Convert Arg] {}: {}, {} => {}".format(arg, val, type(val), type(opt_val))) self._opt.args[arg] = json.loads(val_str) else: self._opt.args[arg] = val self._config['args'][arg] = val_str # add default settings for section in self._default_config: opt = hp.Opt() for key in self._default_config[section]: if key not in self._opt[section]: opt[key] = self._default_config[section][key] self._config[section][key] = json.dumps(opt[key]) self._opt[section] += opt # additional post process if self._opt.args.add is not None and type(self._opt.args.add) is dict: self._opt.args.add = hp.util.dict_to_opt(self._opt.args.add)
def dict_to_opt(d): opt = hp.Opt() for k, v in d.items(): if type(v) is dict: opt[k] = dict_to_opt(v) else: opt[k] = v return opt
def get_ctx(): global _context # merge current context res = hp.Opt() for c in reversed(_context): res += c return res
def wrapper(data, **kwargs): # kwargs parsing opt = hp.Opt(kwargs) + hp.get_ctx() # set default params #opt += hp.Opt(is_training=True, reuse=None) # call sugar function out = func(data, opt) return out
def ctx(**kwargs): global _context # set options when enter # set options when enter context_now = hp.Opt(kwargs) _context += [context_now] # if named context if context_now.name: context_now.scope_name = context_now.name context_now.name = None yield else: yield # clear options when exit del _context[-1]
def dbg_cfg(**kwargs): global _dbg_cfg _dbg_cfg *= hp.Opt(kwargs) if dbg_vld(DbgChn.STD, DbgLvl.DEBUG): hp.print_pp(_dbg_cfg)
class DbgLvl(IntEnum): NONE = 0 NOTSET = 0 MAX = 5 DEBUG = 10 MED = 15 INFO = 20 WARNING = 30 MIN = 35 ERROR = 40 CRITICAL = 50 _dbg_cfg = hp.Opt() _dbg_cfg += hp.Opt(level=DbgLvl.MAX, channel=DbgChn.STD | DbgChn.DATA | DbgChn.NET | DbgChn.VIEW | DbgChn.VALID) def dbg_cfg_val(): global _dbg_cfg return _dbg_cfg def dbg_cfg(**kwargs): global _dbg_cfg _dbg_cfg *= hp.Opt(kwargs) if dbg_vld(DbgChn.STD, DbgLvl.DEBUG): hp.print_pp(_dbg_cfg)