def test01_optimize(self): space = Optimizable()._get_space({'x': ['uniform', ['x', 1, 10]]}) best = fmin(fn=lambda opt: opt['x']**2, space=space, algo=tpe.suggest, max_evals=100) self.assertEqual(int(best['x']), 1)
def __init__(self, opt_path=None, *args, **kwargs): Algo.__init__(self, *args, **kwargs) CFROption.__init__(self, *args, **kwargs) Evaluable.__init__(self, *args, **kwargs) Serializable.__init__(self, *args, **kwargs) Optimizable.__init__(self, *args, **kwargs) if opt_path is None: opt_path = CFROption().get_default_option() self.logger = log.get_logger('CFR') # put options into cython class with type assertion # see comments on options.py for the description of each parameter self.opt, self.opt_path = self.get_option(opt_path) self.obj = CyCFR() # check the validity of option self.is_valid_option(self.opt) assert self.obj.init(self.opt_path.encode( "utf8")), "putting parameter to cython object failed" # ensure embedding matrix is initialzed for preventing segmentation fault self.is_initialized = False self.data = None data = kwargs.get('data') data_opt = self.opt.get('data_opt') data_opt = kwargs.get('data_opt', data_opt) if data_opt: assert data_opt.data.internal_data_type == "matrix", \ f"internal data type is {data_opt.data.internal_data_type}, not matrix" self.data = buffalo.data.load(data_opt) assert self.data.data_type == 'stream' self.data.create() elif isinstance(data, Data): self.data = data self.logger.info('CFR ({})'.format(json.dumps(self.opt, indent=2))) if self.data: self.logger.info(self.data.show_info()) assert self.data.data_type in ['stream']
def __init__(self, opt_path=None, *args, **kwargs): Algo.__init__(self, *args, **kwargs) W2VOption.__init__(self, *args, **kwargs) Evaluable.__init__(self, *args, **kwargs) Serializable.__init__(self, *args, **kwargs) Optimizable.__init__(self, *args, **kwargs) if opt_path is None: opt_path = W2VOption().get_default_option() self.logger = log.get_logger('W2V') self.opt, self.opt_path = self.get_option(opt_path) self.obj = CyW2V() assert self.obj.init(bytes( self.opt_path, 'utf-8')), 'cannot parse option file: %s' % opt_path self.data = None data = kwargs.get('data') data_opt = self.opt.get('data_opt') data_opt = kwargs.get('data_opt', data_opt) if data_opt: self.data = buffalo.data.load(data_opt) assert self.data.data_type == 'stream' self.data.create() elif isinstance(data, Data): self.data = data self.logger.info('W2V(%s)' % json.dumps(self.opt, indent=2)) if self.data: self.logger.info(self.data.show_info()) assert self.data.data_type in ['stream'] self._vocab = aux.Option({ 'size': 0, 'index': None, 'inv_index': None, 'scale': None, 'dist': None, 'total_word_count': 0 })
def test02_optimize(self): def mock_fn(opt): loss = 1.0 - opt['adaptive_reg'] / 1.0 loss += 1.0 / (opt['d']**2 + 1) loss += 1.0 / opt['alpha'] loss += (opt['reg_i'] / 2.0) loss += (opt['reg_u'] / 2.0) return loss option = ALSOption().get_default_optimize_option() space = Optimizable()._get_space(option.space) best = fmin(fn=mock_fn, space=space, algo=tpe.suggest, max_evals=600) self.assertGreaterEqual(int(best['d']), 2) # this is shifted by 10 self.assertGreaterEqual(int(best['alpha']), 15) self.assertLessEqual(best['reg_i'], 0.3) self.assertLessEqual(best['reg_u'], 0.3) self.assertEqual(best['adaptive_reg'], 1)
def test2_optimize(self): def mock_fn(opt): loss = 1.0 - opt['adaptive_reg'] / 1.0 loss += 1.0 / opt['d'] loss += 1.0 / opt['alpha'] loss += 1.0 / opt['reg_i'] loss += 1.0 / opt['reg_u'] return loss option = ALSOption().get_default_optimize_option() space = Optimizable()._get_space(option.space) best = fmin(fn=mock_fn, space=space, algo=tpe.suggest, max_evals=600) self.assertGreaterEqual(int(best['d']), 16) self.assertGreaterEqual(int(best['alpha']), 16) self.assertGreaterEqual(best['reg_i'], 0.5) self.assertGreaterEqual(best['reg_u'], 0.5) self.assertEqual(best['adaptive_reg'], 1)
def test00_get_space(self): Optimizable()._get_space({'x': ['uniform', ['x', 1, 10]]}) self.assertTrue(True)