Ejemplo n.º 1
0
    def test_interface_returns_same_as_cpp(self):
        """Test that the /gp/ei endpoint does the same thing as the C++ interface."""
        tolerance = 1.0e-11
        for test_case in self.gp_test_environments:
            python_domain, python_gp = test_case
            python_cov, historical_data = python_gp.get_core_data_copy()

            cpp_cov = SquareExponential(python_cov.hyperparameters)
            cpp_gp = GaussianProcess(cpp_cov, historical_data)

            points_to_evaluate = python_domain.generate_uniform_random_points_in_domain(10)

            # EI from C++
            expected_improvement_evaluator = ExpectedImprovement(cpp_gp, None)
            # TODO(GH-99): Change test case to have the right shape:
            # (num_to_evaluate, num_to_sample, dim)
            # Here we assume the shape is (num_to_evaluate, dim) so we insert an axis, making num_to_sample = 1.
            # Also might be worth testing more num_to_sample values (will require manipulating C++ RNG state).
            cpp_expected_improvement = expected_improvement_evaluator.evaluate_at_point_list(
                points_to_evaluate[:, numpy.newaxis, :]
            )

            # EI from REST
            json_payload = self._build_json_payload(
                python_domain, python_cov, historical_data, points_to_evaluate.tolist()
            )
            resp = self.testapp.post(self.endpoint, json_payload)
            resp_schema = GpEiResponse()
            resp_dict = resp_schema.deserialize(json.loads(resp.body))
            rest_expected_improvement = numpy.asarray(resp_dict.get("expected_improvement"))

            self.assert_vector_within_relative(rest_expected_improvement, cpp_expected_improvement, tolerance)
Ejemplo n.º 2
0
    def gp_ei_view(self):
        """Endpoint for gp_ei POST requests.

        .. http:post:: /gp/ei

           Calculates the Expected Improvement (EI) of a set of points, given historical data.

           :input: :class:`moe.views.schemas.GpEiRequest`
           :output: :class:`moe.views.schemas.GpEiResponse`

           :status 201: returns a response
           :status 500: server error

        """
        params = self.get_params_from_request()

        # TODO(GH-99): Change REST interface to give points_to_evaluate with shape
        # (num_to_evaluate, num_to_sample, dim)
        # Here we assume the shape is (num_to_evaluate, dim) so we insert an axis, making num_to_sample = 1.
        points_to_evaluate = numpy.array(params.get('points_to_evaluate'))[:, numpy.newaxis, :]
        points_being_sampled = numpy.array(params.get('points_being_sampled'))
        num_mc_iterations = params.get('mc_iterations')
        max_num_threads = params.get('max_num_threads')
        gaussian_process = _make_gp_from_params(params)

        expected_improvement_evaluator = ExpectedImprovement(
                gaussian_process,
                points_being_sampled=points_being_sampled,
                num_mc_iterations=num_mc_iterations,
                )

        with timing_context(EI_COMPUTATION_TIMING_LABEL):
            expected_improvement = expected_improvement_evaluator.evaluate_at_point_list(
                points_to_evaluate,
                max_num_threads=max_num_threads,
            )

        return self.form_response({
                'endpoint': self._route_name,
                'expected_improvement': expected_improvement.tolist(),
                })
Ejemplo n.º 3
0
    def gp_ei_view(self):
        """Endpoint for gp_ei POST requests.

        .. http:post:: /gp/ei

           Calculates the Expected Improvement (EI) of a set of points, given historical data.

           :input: :class:`moe.views.schemas.rest.GpEiRequest`
           :output: :class:`moe.views.schemas.rest.GpEiResponse`

           :status 200: returns a response
           :status 500: server error

        """
        params = self.get_params_from_request()

        # TODO(GH-99): Change REST interface to give points_to_evaluate with shape
        # (num_to_evaluate, num_to_sample, dim)
        # Here we assume the shape is (num_to_evaluate, dim) so we insert an axis, making num_to_sample = 1.
        points_to_evaluate = numpy.array(params.get('points_to_evaluate'))[:, numpy.newaxis, :]
        points_being_sampled = numpy.array(params.get('points_being_sampled'))
        num_mc_iterations = params.get('mc_iterations')
        max_num_threads = params.get('max_num_threads')
        gaussian_process = _make_gp_from_params(params)

        expected_improvement_evaluator = ExpectedImprovement(
                gaussian_process,
                points_being_sampled=points_being_sampled,
                num_mc_iterations=num_mc_iterations,
                )

        with timing_context(EI_COMPUTATION_TIMING_LABEL):
            expected_improvement = expected_improvement_evaluator.evaluate_at_point_list(
                points_to_evaluate,
                max_num_threads=max_num_threads,
            )

        return self.form_response({
                'endpoint': self._route_name,
                'expected_improvement': expected_improvement.tolist(),
                })
Ejemplo n.º 4
0
    def test_interface_returns_same_as_cpp(self):
        """Test that the /gp/ei endpoint does the same thing as the C++ interface."""
        tolerance = 1.0e-11
        for test_case in self.gp_test_environments:
            python_domain, python_gp = test_case
            python_cov, historical_data = python_gp.get_core_data_copy()

            cpp_cov = SquareExponential(python_cov.hyperparameters)
            cpp_gp = GaussianProcess(cpp_cov, historical_data)

            points_to_evaluate = python_domain.generate_uniform_random_points_in_domain(
                10)

            # EI from C++
            expected_improvement_evaluator = ExpectedImprovement(
                cpp_gp,
                None,
            )
            # TODO(GH-99): Change test case to have the right shape:
            # (num_to_evaluate, num_to_sample, dim)
            # Here we assume the shape is (num_to_evaluate, dim) so we insert an axis, making num_to_sample = 1.
            # Also might be worth testing more num_to_sample values (will require manipulating C++ RNG state).
            cpp_expected_improvement = expected_improvement_evaluator.evaluate_at_point_list(
                points_to_evaluate[:, numpy.newaxis, :], )

            # EI from REST
            json_payload = self._build_json_payload(
                python_domain, python_cov, historical_data,
                points_to_evaluate.tolist())
            resp = self.testapp.post(self.endpoint, json_payload)
            resp_schema = GpEiResponse()
            resp_dict = resp_schema.deserialize(json.loads(resp.body))
            rest_expected_improvement = numpy.asarray(
                resp_dict.get('expected_improvement'))

            self.assert_vector_within_relative(
                rest_expected_improvement,
                cpp_expected_improvement,
                tolerance,
            )