コード例 #1
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test_sample(self, mock_np_random):
        """Test that the method ``sample`` is being called with `n_samples and
        `self.dimensions`.
        """
        # setup
        mock_np_random.return_value = np.array([[0.1], [0.2]])
        instance = FloatHyperParam()
        n_samples = 2

        # run
        result = instance.sample(n_samples)

        # assert
        expected_result = np.array([[0.1], [0.2]])

        mock_np_random.assert_called_once_with((n_samples, instance.dimensions))
        np.testing.assert_array_equal(result, expected_result)
コード例 #2
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test___init__min_eq_max(self):
        """Test instantiation with ``min=n`` and ``max=n``"""
        # setup
        n = 0.1

        # run / assert
        with self.assertRaises(ValueError):
            FloatHyperParam(min=n, max=n)
コード例 #3
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test___init__no_min_no_max(self):
        """Test instantiation with ``min=None`` and ``max=None``"""
        # run
        instance = FloatHyperParam()

        # assert
        self.assertEqual(instance.min, sys.float_info.min)
        self.assertEqual(instance.max, sys.float_info.max)
コード例 #4
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test___init__with_np_inf(self):
        """Test instantiation with ``min=-np.inf`` and ``max=np.inf``"""
        # run
        instance = FloatHyperParam(min=-np.inf, max=np.inf)

        # assert
        self.assertEqual(instance.min, sys.float_info.min)
        self.assertEqual(instance.max, sys.float_info.max)
コード例 #5
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    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.0
        _max = 10.0
        instance = FloatHyperParam(min=_min, max=_max)
        values = np.array([[0.1], [0.9]])

        # run
        result = instance._transform(values)

        # assert
        expected_result = np.array([[0.01],
                                    [0.09]])

        np.testing.assert_array_equal(result, expected_result)
コード例 #6
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    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.float_info.max = 1000.0  # This values can be different in each OS.
        mock_sys.float_info.min = 0.0

        instance = FloatHyperParam()
        values = np.array([[0.9], [100.8]])

        # run
        result = instance._transform(values)

        # assert
        expected_result = np.array([[0.0009],
                                    [0.1008]])

        np.testing.assert_array_equal(result, expected_result)
コード例 #7
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test__inverse_transform_min_no_max(self, mock_sys):
        """Test that the method ``_inverse_transform`` performs a normalization of values between
        ``min`` and ``max`` with min value set up.
        """
        # setup
        mock_sys.float_info.max = 10.0
        _min = 1.0

        instance = FloatHyperParam(min=_min)
        values = np.array([[0.1], [0.]])

        # run
        result = instance._inverse_transform(values)

        # assert
        expected_result = np.array([[1.9],
                                    [1.0]])

        np.testing.assert_array_equal(result, expected_result)
コード例 #8
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test__transform_min_no_max(self, mock_sys):
        """Test that the method ``_transform`` performs a normalization of values between ``min``
        and ``max`` with a min value set in.
        """
        # setup
        mock_sys.float_info.max = 10.0
        _min = 1.0

        instance = FloatHyperParam(min=_min)
        values = np.array([[7.3], [4.6]])

        # run
        result = instance._transform(values)

        # assert
        expected_result = np.array([[0.7],
                                    [0.4]])

        np.testing.assert_allclose(result, expected_result)
コード例 #9
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test___init__min_max(self):
        """Test instantiation with ``min=n`` and ``max=x``"""
        # setup
        _min = 0.1
        _max = 0.9

        # run
        instance = FloatHyperParam(min=_min, max=_max)

        # assert
        self.assertEqual(instance.min, _min)
        self.assertEqual(instance.max, _max)
コード例 #10
0
    def test___init__no_min_max(self):
        """Test instantiation with ``min=None`` and ``max=n``"""
        # setup
        _max = 0.1

        # run
        instance = FloatHyperParam(min=None, max=_max)

        # assert
        self.assertEqual(instance.min, sys.float_info.min)
        self.assertEqual(instance.max, _max)
        self.assertEqual(instance.default, sys.float_info.min)
コード例 #11
0
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
コード例 #12
0
ファイル: test_numerical.py プロジェクト: x10-utils/BTB
    def test___init__min_gt_max(self):
        """Test instantiation with ``min`` being reater than ``max``"""

        # run / assert
        with self.assertRaises(ValueError):
            FloatHyperParam(min=1, max=0)
コード例 #13
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)