<commit_before># Copyright 2019 The Keras Tuner Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import queue
import random
import json
from ..engine import tuner as tuner_module
from ..engine import oracle as oracle_module
from ..abstractions.tensorflow import TENSORFLOW_UTILS as tf_utils


def queue_to_list(queue):
    ret_list = []
    while not queue.empty():
        ret_list.append(queue.get())
    return ret_list


class HyperbandOracle(oracle_module.Oracle):
    """Oracle class for Hyperband.

    Args:
        factor: Int. Reduction factor for the number of epochs
            and number of models for each bracket.
        min_epochs: Int. The minimum number of epochs to train a model.
        max_epochs: Int. The maximum number of epochs to train a model.
        seed: Int. Random seed.
    """

    def __init__(self,
                 factor=3,
                 min_epochs=3,
                 max_epochs=10,
                 seed=None):
        super(HyperbandOracle, self).__init__()
        if min_epochs >= max_epochs:
            raise ValueError('max_epochs needs to be larger than min_epochs.')
        if factor < 2:
            raise ValueError('factor needs to be a int larger than 1.')
        self.seed = seed or random.randint(1, 1e4)
        self.factor = factor
        self.min_epochs = min_epochs
        self.max_epochs = max_epochs
        self._queue = queue.Queue()
        self._trial_count = 0
        self._running = {}
        self._trial_id_to_candidate_index = {}
        self._candidates = None
        self._candidate_score = None
        self._max_collisions = 20
        self._seed_state = self.seed
        self._tried_so_far = set()
        self._index_to_id = {}
        self._num_brackets = self._get_num_brackets()
        self._bracket_index = self._num_brackets
        self._model_sequence = self._get_model_sequence()
        self._epoch_sequence = self._get_epoch_sequence()

    def result(self, trial_id, score):
        self._running[trial_id] = False
        self._candidate_score[
            self._trial_id_to_candidate_index[trial_id]] = score

    def populate_space(self, trial_id, space):
        self.update_space(space)
        # Queue is not empty means it is in one bracket.
        if not self._queue.empty():
            return self._run_values(space, trial_id)

        # Wait the current bracket to finish
        if any([value for key, value in self._running.items()]):
            return {'status': 'IDLE'}

        # Start the next bracket if not end of bandit.
        if self._bracket_index + 1 < self._num_brackets:
            self._bracket_index += 1
            self._select_candidates()
        # If the current band ends
        else:
            self._bracket_index = 0
            self._generate_candidates()
            self._index_to_id = {}

        return self._run_values(space, trial_id)

    def _run_values(self, space, trial_id):
        self._trial_count += 1
        self._running[trial_id] = True
        candidate_index = self._queue.get()
        if candidate_index not in self._index_to_id:
            self._index_to_id[candidate_index] = trial_id
        candidate = self._candidates[candidate_index]
        self._trial_id_to_candidate_index[trial_id] = candidate_index
        if candidate is not None:
            values = self._copy_values(space, candidate)
            if trial_id != self._index_to_id[candidate_index]:
                values['tuner/trial_id'] = self._index_to_id[candidate_index]
            values['tuner/epochs'] = self._epoch_sequence[self._bracket_index]
            return {'status': 'RUN', 'values': values}
        return {'status': 'EXIT'}

    @staticmethod
    def _copy_values(space, values):
        return_values = values.copy()
        for hyperparameter in space:
            if hyperparameter.name not in values:
                return_values[hyperparameter.name] = hyperparameter.default
        return return_values

    def _generate_candidates(self):
        self._candidates = []
        self._candidate_score = []
        num_models = self._model_sequence[0]

        for index in range(num_models):
            instance = self._new_trial()
            if instance is not None:
                self._candidates.append(instance)
                self._candidate_score.append(None)

        for index, instance in enumerate(self._candidates):
            self._queue.put(index)

    def _select_candidates(self):
        sorted_candidates = sorted(list(range(len(self._candidates))),
                                   key=lambda i: self._candidate_score[i],
                                   reverse=True)
        num_selected_candidates = self._model_sequence[self._bracket_index]
        for index in sorted_candidates[:num_selected_candidates]:
            self._queue.put(index)

    def _new_trial(self):<commit_msg>select candidates lower the better<commit_after># Copyright 2019 The Keras Tuner Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import queue
import random
import json
from ..engine import tuner as tuner_module
from ..engine import oracle as oracle_module
from ..abstractions.tensorflow import TENSORFLOW_UTILS as tf_utils


def queue_to_list(queue):
    ret_list = []
    while not queue.empty():
        ret_list.append(queue.get())
    return ret_list


class HyperbandOracle(oracle_module.Oracle):
    """Oracle class for Hyperband.

    Args:
        factor: Int. Reduction factor for the number of epochs
            and number of models for each bracket.
        min_epochs: Int. The minimum number of epochs to train a model.
        max_epochs: Int. The maximum number of epochs to train a model.
        seed: Int. Random seed.
    """

    def __init__(self,
                 factor=3,
                 min_epochs=3,
                 max_epochs=10,
                 seed=None):
        super(HyperbandOracle, self).__init__()
        if min_epochs >= max_epochs:
            raise ValueError('max_epochs needs to be larger than min_epochs.')
        if factor < 2:
            raise ValueError('factor needs to be a int larger than 1.')
        self.seed = seed or random.randint(1, 1e4)
        self.factor = factor
        self.min_epochs = min_epochs
        self.max_epochs = max_epochs
        self._queue = queue.Queue()
        self._trial_count = 0
        self._running = {}
        self._trial_id_to_candidate_index = {}
        self._candidates = None
        self._candidate_score = None
        self._max_collisions = 20
        self._seed_state = self.seed
        self._tried_so_far = set()
        self._index_to_id = {}
        self._num_brackets = self._get_num_brackets()
        self._bracket_index = self._num_brackets
        self._model_sequence = self._get_model_sequence()
        self._epoch_sequence = self._get_epoch_sequence()

    def result(self, trial_id, score):
        self._running[trial_id] = False
        self._candidate_score[
            self._trial_id_to_candidate_index[trial_id]] = score

    def populate_space(self, trial_id, space):
        self.update_space(space)
        # Queue is not empty means it is in one bracket.
        if not self._queue.empty():
            return self._run_values(space, trial_id)

        # Wait the current bracket to finish
        if any([value for key, value in self._running.items()]):
            return {'status': 'IDLE'}

        # Start the next bracket if not end of bandit.
        if self._bracket_index + 1 < self._num_brackets:
            self._bracket_index += 1
            self._select_candidates()
        # If the current band ends
        else:
            self._bracket_index = 0
            self._generate_candidates()
            self._index_to_id = {}

        return self._run_values(space, trial_id)

    def _run_values(self, space, trial_id):
        self._trial_count += 1
        self._running[trial_id] = True
        candidate_index = self._queue.get()
        if candidate_index not in self._index_to_id:
            self._index_to_id[candidate_index] = trial_id
        candidate = self._candidates[candidate_index]
        self._trial_id_to_candidate_index[trial_id] = candidate_index
        if candidate is not None:
            values = self._copy_values(space, candidate)
            if trial_id != self._index_to_id[candidate_index]:
                values['tuner/trial_id'] = self._index_to_id[candidate_index]
            values['tuner/epochs'] = self._epoch_sequence[self._bracket_index]
            return {'status': 'RUN', 'values': values}
        return {'status': 'EXIT'}

    @staticmethod
    def _copy_values(space, values):
        return_values = values.copy()
        for hyperparameter in space:
            if hyperparameter.name not in values:
                return_values[hyperparameter.name] = hyperparameter.default
        return return_values

    def _generate_candidates(self):
        self._candidates = []
        self._candidate_score = []
        num_models = self._model_sequence[0]

        for index in range(num_models):
            instance = self._new_trial()
            if instance is not None:
                self._candidates.append(instance)
                self._candidate_score.append(None)

        for index, instance in enumerate(self._candidates):
            self._queue.put(index)

    def _select_candidates(self):
        sorted_candidates = sorted(list(range(len(self._candidates))),
                                   key=lambda i: self._candidate_score[i])
        num_selected_candidates = self._model_sequence[self._bracket_index]
        for index in sorted_candidates[:num_selected_candidates]:
            self._queue.put(index)

    def _new_trial(self):
{'fraction_non_alphanumeric': 0.08090013018411754, 'fraction_numerical': 0.004649432769202157, 'mean_word_length': 2.408874801901743, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861224', 'n_tokens_mistral': 3141, 'n_tokens_neox': 3005, 'n_words': 836}