def test_sample(self, mock__inverse_transform, mock__transform, mock_np_random): """Test that the method ``sample`` returns random generated numbers and process them thro the internal methods to convert them in the range of our search space.""" # setup instance = IntHyperParam() n_samples = 2 # run result = instance.sample(n_samples) # assert mock_np_random.assert_called_once_with((n_samples, instance.dimensions)) mock__inverse_transform.assert_called_once_with(mock_np_random.return_value) mock__transform.assert_called_once_with(mock__inverse_transform.return_value) self.assertEqual(result, mock__transform.return_value)
def test__transform_no_min_no_max(self, mock_sys): """Test that the method ``_transform`` performs a normalization of values between ``min`` and ``max`` with no limit set on them. """ # setup mock_sys.maxsize = 1000 # This values can be different in each OS. instance = IntHyperParam() values = np.array([[9], [100]]) # run result = instance._transform(values) # assert expected_result = np.array([[0.50899101], [0.5999001]]) np.testing.assert_allclose(result, expected_result)
def test__inverse_transform_no_min_no_max(self, mock_sys): """Test that the method ``_inverse_transform`` performs a normalization of values between ``min`` and ``max`` with no min or max set. """ # setup mock_sys.maxsize = 1000 instance = IntHyperParam() values = np.array([[0.0009], [0.1008]]) # run result = instance._inverse_transform(values) # assert expected_result = np.array([[-500], [-400]]) np.testing.assert_array_equal(result, expected_result.astype(int))
def test___init__min_eq_max(self): """Test instantiation with ``min=n`` and ``max=n``""" # setup n = 1 # run / assert with self.assertRaises(ValueError): IntHyperParam(min=n, max=n)
def test__inverse_transform_min_max(self, mock_sys): """Test that the method ``_inverse_transform`` performs a normalization of values between ``min`` and ``max`` with a min and max value set up. """ # setup _min = 0 _max = 10 instance = IntHyperParam(min=_min, max=_max) values = np.array([[0.1], [0.9]]) # run result = instance._inverse_transform(values) # assert expected_result = np.array([[1], [9]]) np.testing.assert_array_equal(result, expected_result.astype(int))
def test__transform_no_min_max(self, mock_sys): """Test that the method ``_transform`` performs a normalization of values between ``min`` and ``max`` with a max value set in. """ # setup mock_sys.maxsize = 1000 _max = 10 instance = IntHyperParam(max=_max) values = np.array([[1], [9]]) # run result = instance._transform(values) # assert expected_result = np.array([[0.981409], [0.99706458]]) np.testing.assert_allclose(result, expected_result)
def test__transform_min_max(self, mock_sys): """Test that the method ``_transform`` performs a normalization of values between ``min`` and ``max`` with a max and min value set in. """ # setup _min = 0 _max = 10 instance = IntHyperParam(min=_min, max=_max) values = np.array([[9], [1]]) # run result = instance._transform(values) # assert expected_result = np.array([[0.86363636], [0.13636364]]) np.testing.assert_allclose(result, expected_result)
def test___init__min_max_invalid_step(self): """Test instantiation with ``min=n`` and ``max=x``""" # setup _min = 1 _max = 9 _step = 5 # run / assert with self.assertRaises(ValueError): IntHyperParam(min=_min, max=_max, step=_step)
def test___init__min_no_max(self): """Test instantiation with ``min=n`` and ``max=None``""" # setup _min = 1 # run instance = IntHyperParam(min=_min, max=None) # assert self.assertEqual(instance.min, _min) self.assertEqual(instance.max, sys.maxsize / 2) self.assertEqual(instance.step, 1)
def test___init__no_min_exclude_max(self): """Test instantiation with ``min=None`` and ``max=None`` excluding ``max``.""" # run instance = IntHyperParam(include_max=False) # assert expected_min = int(-(sys.maxsize / 2)) expected_max = int(sys.maxsize / 2) - 1 self.assertEqual(instance.min, expected_min) self.assertEqual(instance.max, expected_max) self.assertEqual(instance.step, 1)
def test___init__no_min_no_max(self): """Test instantiation with ``min=None`` and ``max=None``""" # run instance = IntHyperParam() # assert expected_min = -(sys.maxsize / 2) expected_max = sys.maxsize / 2 self.assertEqual(instance.min, expected_min) self.assertEqual(instance.max, expected_max) self.assertEqual(instance.step, 1)
def test___init__min_max(self): """Test instantiation with ``min=n`` and ``max=x``""" # setup _min = 1 _max = 9 # run instance = IntHyperParam(min=_min, max=_max) # assert self.assertEqual(instance.min, _min) self.assertEqual(instance.max, _max) self.assertEqual(instance.step, 1)
def test___init__min_max_step(self): """Test instantiation with ``min=n`` and ``max=x`` and step.""" # setup _min = 0 _max = 10 _step = 2 # run instance = IntHyperParam(min=_min, max=_max, step=_step) # assert self.assertEqual(instance.min, 0) self.assertEqual(instance.max, 10) self.assertEqual(instance.step, 2)
def _generate_bounded_hyperparam(hyperparam, default): hp_dict = { 'min': hyperparam.lower, 'max': hyperparam.upper, 'default': default, 'include_min': hyperparam.lower_inclusive, 'include_max': hyperparam.upper_inclusive, } if hyperparam.structural_type is float: btb_hyperparam = FloatHyperParam(**hp_dict) elif hyperparam.structural_type is int: btb_hyperparam = IntHyperParam(**hp_dict) else: return None return btb_hyperparam
def test___init__min_gt_max(self): """Test instantiation with ``min`` being reater than ``max``""" # run / assert with self.assertRaises(ValueError): IntHyperParam(min=1, max=0)
def from_dict(cls, dict_hyperparams): """Create an instance from a dictionary containing information over hyperparameters. Class method that creates an instance from a dictionary that describes the type of a hyperparameter, the range or values that this can have and the default value of the hyperparameter. Args: dict_hyperparams (dict): A python dictionary containing as `key` the given name for the hyperparameter and as value a dictionary containing the following keys: - Type (str): ``bool`` for ``BoolHyperParam``, ``int`` for ``IntHyperParam``, ``float`` for ``FloatHyperParam``, ``str`` for ``CategoricalHyperParam``. - Range or Values (list): Range / values that this hyperparameter can take, in case of ``CategoricalHyperParam`` those will be used as the ``choices``, for ``NumericalHyperParams`` the ``min`` value will be used as the minimum value and the ``max`` value will be used as the ``maximum`` value. - Default (str, bool, int, float or None): The default value for the hyperparameter. Returns: Tunable: A ``Tunable`` instance with the given hyperparameters. """ if not isinstance(dict_hyperparams, dict): raise TypeError('Hyperparams must be a dictionary.') hyperparams = {} for name, hyperparam in dict_hyperparams.items(): hp_type = hyperparam['type'] hp_default = hyperparam.get('default') if hp_type == 'int': hp_range = hyperparam.get('range') or hyperparam.get('values') hp_min = min(hp_range) if hp_range else None hp_max = max(hp_range) if hp_range else None hp_instance = IntHyperParam(min=hp_min, max=hp_max, default=hp_default) elif hp_type == 'float': hp_range = hyperparam.get('range') or hyperparam.get('values') hp_min = min(hp_range) hp_max = max(hp_range) hp_instance = FloatHyperParam(min=hp_min, max=hp_max, default=hp_default) elif hp_type == 'bool': hp_instance = BooleanHyperParam(default=hp_default) elif hp_type == 'str': hp_choices = hyperparam.get('range') or hyperparam.get( 'values') hp_instance = CategoricalHyperParam(choices=hp_choices, default=hp_default) hyperparams[name] = hp_instance return cls(hyperparams)