示例#1
0
def propose_queries_blackbox_optimizer(config, current_input,
                                       blackbox_optimizer, iteration):
    core_hyperparameters = blackbox_optimizer.get_hyperparameters()
    current_input_vectorized = zero_order_pb2.Vector(
        values=current_input.tolist())

    proposed_perturbations = []
    requests = []
    for _ in range(config.test_workers):
        # only hyperparameters and current input matter here
        null_perturbation = [0.0] * len(current_input)
        request = zero_order_pb2.EvaluationRequest(
            current_input=current_input_vectorized,
            hyperparameters=core_hyperparameters,
            perturbations=[zero_order_pb2.Vector(values=null_perturbation)],
            tag=iteration)
        requests.append(request)

    for i in range(config.total_num_perturbations):
        perturbation = np.random.normal(
            size=(len(current_input))) * config.es_precision_parameter
        proposed_perturbations.append(perturbation)
        tag = i + 1
        for _ in range(config.num_repeats):
            request = zero_order_pb2.EvaluationRequest(
                current_input=current_input_vectorized,
                hyperparameters=core_hyperparameters,
                perturbations=[
                    zero_order_pb2.Vector(values=perturbation.tolist())
                ],
                tag=tag)
            requests.append(request)

        if config.es_est_type == 'antithetic':
            antiperturbation = -perturbation
            proposed_perturbations.append(antiperturbation)
            for _ in range(config.num_repeats):
                request = zero_order_pb2.EvaluationRequest(
                    current_input=current_input_vectorized,
                    hyperparameters=core_hyperparameters,
                    perturbations=[
                        zero_order_pb2.Vector(values=antiperturbation.tolist())
                    ],
                    tag=-tag)
                requests.append(request)
    for _ in range(config.num_repeats + config.num_exact_evals):
        null_perturbation = [0.0] * len(current_input)
        request = zero_order_pb2.EvaluationRequest(
            current_input=current_input_vectorized,
            hyperparameters=core_hyperparameters,
            perturbations=[zero_order_pb2.Vector(values=null_perturbation)],
            tag=0)
        requests.append(request)

    return requests, proposed_perturbations
示例#2
0
    def TrainEvaluate(self, request, context):
        current_input = []
        core_hyperparameters = []
        for i in range(len(request.current_input.values)):
            current_input.append(request.current_input.values[i])
        for i in range(len(request.hyperparameters)):
            core_hyperparameters.append(request.hyperparameters[i])
        hyperparameters = [self.worker_id] + core_hyperparameters
        current_input_reshaped = np.array(current_input)
        tag = request.tag

        proposed_perturbations = []
        for j in range(len(request.perturbations)):
            proposed_perturbation = []
            for k in range(len(request.perturbations[j].values)):
                proposed_perturbation.append(
                    request.perturbations[j].values[k])
            proposed_perturbations.append(proposed_perturbation)

        perturbations = []
        function_values = []
        evaluation_stats = []

        for i in range(len(proposed_perturbations)):
            perturbation = np.array(proposed_perturbations[i])
            perturbations.append(
                zero_order_pb2.Vector(values=perturbation.tolist()))
            task_id_list = np.random.choice(self.task_ids,
                                            size=self.task_batch_size,
                                            replace=False)

            task_list = [
                self.blackbox_object.config.make_task_fn(task_id=task_id)
                for task_id in task_id_list
            ]

            function_value, evaluation_stat = self.blackbox_object.execute(
                current_input_reshaped + perturbation,
                task_list,
                hyperparameters=hyperparameters)

            evaluation_stats.append(evaluation_stat)
            function_values.append(function_value)

        evaluation_stats_reduced = [sum(x) for x in zip(*evaluation_stats)]
        if not proposed_perturbations:
            results = zero_order_pb2.EvaluationResponse(
                perturbations=perturbations,
                function_values=function_values,
                evaluation_stats=evaluation_stats_reduced,
                tag=tag)
        else:
            results = zero_order_pb2.EvaluationResponse(
                perturbations=[],
                function_values=function_values,
                evaluation_stats=evaluation_stats_reduced,
                tag=tag)
        return results