Пример #1
0
 def __init__(
     self,
     num_layers: Optional[Union[int, hyperparameters.Choice]] = None,
     num_units: Optional[Union[int, hyperparameters.Choice]] = None,
     use_batchnorm: Optional[bool] = None,
     dropout: Optional[Union[float, hyperparameters.Choice]] = None,
     **kwargs,
 ):
     super().__init__(**kwargs)
     self.num_layers = utils.get_hyperparameter(
         num_layers,
         hyperparameters.Choice("num_layers", [1, 2, 3], default=2),
         int,
     )
     self.num_units = utils.get_hyperparameter(
         num_units,
         hyperparameters.Choice("num_units",
                                [16, 32, 64, 128, 256, 512, 1024],
                                default=32),
         int,
     )
     self.use_batchnorm = use_batchnorm
     self.dropout = utils.get_hyperparameter(
         dropout,
         hyperparameters.Choice("dropout", [0.0, 0.25, 0.5], default=0.0),
         float,
     )
Пример #2
0
def test_get_hyperparameter_with_hp_return_same():
    hp = utils.get_hyperparameter(
        hyperparameters.Choice("hp", [10, 30]),
        hyperparameters.Choice("hp", [10, 20]),
        int,
    )
    assert isinstance(hp, hyperparameters.Choice)
Пример #3
0
 def __init__(
     self,
     kernel_size: Optional[Union[int, hyperparameters.Choice]] = None,
     num_blocks: Optional[Union[int, hyperparameters.Choice]] = None,
     num_layers: Optional[int] = None,
     filters: Optional[Union[int, hyperparameters.Choice]] = None,
     max_pooling: Optional[bool] = None,
     separable: Optional[bool] = None,
     dropout: Optional[float] = None,
     **kwargs,
 ):
     super().__init__(**kwargs)
     self.kernel_size = utils.get_hyperparameter(
         kernel_size,
         hyperparameters.Choice("kernel_size", [3, 5, 7], default=3),
         int,
     )
     self.num_blocks = utils.get_hyperparameter(
         num_blocks,
         hyperparameters.Choice("num_blocks", [1, 2, 3], default=2),
         int,
     )
     self.num_layers = num_layers
     self.filters = utils.get_hyperparameter(
         filters,
         hyperparameters.Choice("filters", [16, 32, 64, 128, 256, 512],
                                default=32),
         int,
     )
     self.max_pooling = max_pooling
     self.separable = separable
     self.dropout = dropout
Пример #4
0
 def __init__(
     self,
     return_sequences: bool = False,
     bidirectional: Optional[Union[bool, hyperparameters.Boolean]] = None,
     num_layers: Optional[Union[int, hyperparameters.Choice]] = None,
     layer_type: Optional[Union[str, hyperparameters.Choice]] = None,
     **kwargs,
 ):
     super().__init__(**kwargs)
     self.return_sequences = return_sequences
     self.bidirectional = utils.get_hyperparameter(
         bidirectional,
         hyperparameters.Boolean("bidirectional", default=True),
         bool,
     )
     self.num_layers = utils.get_hyperparameter(
         num_layers,
         hyperparameters.Choice("num_layers", [1, 2, 3], default=2),
         int,
     )
     self.layer_type = utils.get_hyperparameter(
         layer_type,
         hyperparameters.Choice("layer_type", ["gru", "lstm"],
                                default="lstm"),
         str,
     )
Пример #5
0
 def __init__(
     self,
     max_features: int = 20001,
     pretraining: Optional[Union[str, hyperparameters.Choice]] = None,
     embedding_dim: Optional[Union[int, hyperparameters.Choice]] = None,
     dropout: Optional[Union[float, hyperparameters.Choice]] = None,
     **kwargs,
 ):
     super().__init__(**kwargs)
     self.max_features = max_features
     self.pretraining = utils.get_hyperparameter(
         pretraining,
         hyperparameters.Choice(
             "pretraining",
             ["random", "glove", "fasttext", "word2vec", "none"],
             default="none",
         ),
         str,
     )
     self.embedding_dim = utils.get_hyperparameter(
         embedding_dim,
         hyperparameters.Choice("embedding_dim", [32, 64, 128, 256, 512],
                                default=128),
         int,
     )
     self.dropout = utils.get_hyperparameter(
         dropout,
         hyperparameters.Choice("dropout", [0.0, 0.25, 0.5], default=0.25),
         float,
     )
Пример #6
0
def test_Choice_types():
    values1 = ['a', 'b', 0]
    with pytest.raises(TypeError, match='can contain only one'):
        hp_module.Choice('a', values1)
    values2 = [{'a': 1}, {'a': 2}]
    with pytest.raises(TypeError, match='can contain only `int`'):
        hp_module.Choice('a', values2)
Пример #7
0
def test_hyperband_save_load_middle_of_bracket(tmp_dir):
    hp_list = [
        hp_module.Choice('a', [1, 2], default=1),
        hp_module.Choice('b', [3, 4], default=3),
        hp_module.Choice('c', [5, 6], default=5),
        hp_module.Choice('d', [7, 8], default=7),
        hp_module.Choice('e', [9, 0], default=9)
    ]
    oracle = hyperband_module.HyperbandOracle()

    for trial_id in range(3):
        oracle.populate_space('0_' + str(trial_id), hp_list)
    for trial_id in range(2):
        oracle.result('0_' + str(trial_id), trial_id)

    fname = os.path.join(tmp_dir, 'oracle')
    oracle.save(fname)
    oracle = hyperband_module.HyperbandOracle()
    oracle.reload(fname)

    for trial_id in range(oracle._model_sequence[0] - 2):
        hp = oracle.populate_space('1_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
    assert oracle.populate_space('idle', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[0] - 2):
        oracle.result('1_' + str(trial_id), trial_id)
def test_choice_proto():
    hp = hp_module.Choice('a', [2.3, 4.5, 6.3], ordered=True)
    proto = hp.to_proto()
    assert proto.name == 'a'
    assert proto.ordered
    assert np.allclose([v.float_value for v in proto.values], [2.3, 4.5, 6.3])
    # Proto stores the implicit default.
    assert np.isclose(proto.default.float_value, 2.3)

    new_hp = hp_module.Choice.from_proto(proto)
    assert new_hp.name == 'a'
    assert np.allclose(new_hp.values, hp.values)
    assert new_hp.ordered
    assert np.isclose(new_hp._default, 2.3)

    # Test int values.
    int_choice = hp_module.Choice('b', [1, 2, 3], ordered=False, default=2)
    new_int_choice = hp_module.Choice.from_proto(int_choice.to_proto())
    assert int_choice.get_config() == new_int_choice.get_config()

    # Test float values.
    float_choice = hp_module.Choice('b', [0.5, 2.5, 4.],
                                    ordered=False,
                                    default=2.5)
    new_float_choice = hp_module.Choice.from_proto(float_choice.to_proto())
    assert float_choice.get_config() == new_float_choice.get_config()
Пример #9
0
def test_Choice_types():
    values1 = ["a", "b", 0]
    with pytest.raises(TypeError, match="can contain only one"):
        hp_module.Choice("a", values1)
    values2 = [{"a": 1}, {"a": 2}]
    with pytest.raises(TypeError, match="can contain only `int`"):
        hp_module.Choice("a", values2)
Пример #10
0
def test_save_before_result(tmp_dir):
    hp_list = [hp_module.Choice('a', [1, 2], default=1),
               hp_module.Int('b', 3, 10, default=3),
               hp_module.Float('c', 0, 1, 0.1, default=0),
               hp_module.Fixed('d', 7),
               hp_module.Choice('e', [9, 0], default=9)]
    oracle = bo_module.BayesianOptimizationOracle()
    oracle.populate_space(str(1), hp_list)
    oracle.save(os.path.join(tmp_dir, 'temp_oracle'))
    oracle.result(str(1), 0)
Пример #11
0
def test_bayesian_oracle_with_zero_y(tmp_dir):
    hp_list = [hp_module.Choice('a', [1, 2], default=1),
               hp_module.Int('b', 3, 10, default=3),
               hp_module.Float('c', 0, 1, 0.1, default=0),
               hp_module.Fixed('d', 7),
               hp_module.Choice('e', [9, 0], default=9)]
    oracle = bo_module.BayesianOptimizationOracle()
    for i in range(100):
        oracle.populate_space(str(i), hp_list)
        oracle.result(str(i), 0)
Пример #12
0
def test_Choice():
    choice = hp_module.Choice('choice', [1, 2, 3], default=2)
    choice = hp_module.Choice.from_config(choice.get_config())
    assert choice.default == 2
    assert choice.random_sample() in [1, 2, 3]
    assert choice.random_sample(123) == choice.random_sample(123)
    # No default
    choice = hp_module.Choice('choice', [1, 2, 3])
    assert choice.default == 1
    with pytest.raises(ValueError, match='default value should be'):
        hp_module.Choice('choice', [1, 2, 3], default=4)
Пример #13
0
def test_hyperband_dynamic_space(tmp_dir):
    hp_list = [hp_module.Choice('a', [1, 2], default=1)]
    oracle = hyperband_module.HyperbandOracle()
    hp_list.append(hp_module.Choice('b', [3, 4], default=3))
    values = oracle.populate_space('0', hp_list)['values']
    assert 'b' in values
    oracle.update_space(hp_list)
    hp_list.append(hp_module.Choice('c', [5, 6], default=5))
    assert 'c' in oracle.populate_space('1', hp_list)['values']
    hp_list.append(hp_module.Choice('d', [7, 8], default=7))
    assert 'd' in oracle.populate_space('2', hp_list)['values']
    hp_list.append(hp_module.Choice('e', [9, 0], default=9))
    assert 'e' in oracle.populate_space('3', hp_list)['values']
Пример #14
0
def test_bayesian_dynamic_space(tmp_dir):
    hp_list = [hp_module.Choice('a', [1, 2], default=1)]
    oracle = bo_module.BayesianOptimizationOracle()
    for i in range(10):
        oracle.populate_space(str(i), hp_list)
        oracle.result(str(i), i)
    hp_list.append(hp_module.Int('b', 3, 10, default=3))
    assert 'b' in oracle.populate_space('1_0', hp_list)['values']
    hp_list.append(hp_module.Float('c', 0, 1, 0.1, default=0))
    assert 'c' in oracle.populate_space('1_1', hp_list)['values']
    hp_list.append(hp_module.Fixed('d', 7))
    assert 'd' in oracle.populate_space('1_2', hp_list)['values']
    hp_list.append(hp_module.Choice('e', [9, 0], default=9))
    assert 'e' in oracle.populate_space('1_3', hp_list)['values']
Пример #15
0
def test_hyperband_oracle(tmp_dir):
    hp_list = [
        hp_module.Choice('a', [1, 2], default=1),
        hp_module.Choice('b', [3, 4], default=3),
        hp_module.Choice('c', [5, 6], default=5),
        hp_module.Choice('d', [7, 8], default=7),
        hp_module.Choice('e', [9, 0], default=9)
    ]
    oracle = hyperband_module.HyperbandOracle()
    assert oracle._num_brackets == 3

    for trial_id in range(oracle._model_sequence[0]):
        hp = oracle.populate_space('0_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
        assert hp['values']['tuner/epochs'] == oracle._epoch_sequence[0]
        assert 'tuner/trial_id' not in hp['values']
    assert oracle.populate_space('idle0', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[0]):
        oracle.result('0_' + str(trial_id), trial_id)

    for trial_id in range(oracle._model_sequence[1]):
        hp = oracle.populate_space('1_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
        assert hp['values']['tuner/epochs'] == oracle._epoch_sequence[1]
        assert 'tuner/trial_id' in hp['values']
    assert oracle.populate_space('idle1', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[1]):
        oracle.result('1_' + str(trial_id), trial_id)

    for trial_id in range(oracle._model_sequence[2]):
        hp = oracle.populate_space('2_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
        assert hp['values']['tuner/epochs'] == oracle._epoch_sequence[2]
        assert 'tuner/trial_id' in hp['values']
    assert oracle.populate_space('idle2', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[2]):
        oracle.result('2_' + str(trial_id), trial_id)

    for trial_id in range(oracle._model_sequence[0]):
        hp = oracle.populate_space('3_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
        assert hp['values']['tuner/epochs'] == oracle._epoch_sequence[0]
        assert 'tuner/trial_id' not in hp['values']
    assert oracle.populate_space('idle3', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[0]):
        oracle.result('3_' + str(trial_id), trial_id)

    assert oracle.populate_space('last', hp_list)['status'] == 'RUN'
Пример #16
0
def test_graph_save_load(tmp_dir):
    input1 = ak.Input()
    input2 = ak.Input()
    output1 = ak.DenseBlock()(input1)
    output2 = ak.ConvBlock()(input2)
    output = ak.Merge()([output1, output2])
    output1 = ak.RegressionHead()(output)
    output2 = ak.ClassificationHead()(output)

    graph = graph_module.HyperGraph(inputs=[input1, input2],
                                    outputs=[output1, output2],
                                    override_hps=[
                                        hp_module.Choice(
                                            'dense_block_1/num_layers', [6],
                                            default=6)
                                    ])
    path = os.path.join(tmp_dir, 'graph')
    graph.save(path)
    config = graph.get_config()
    graph = graph_module.HyperGraph.from_config(config)
    graph.reload(path)

    assert len(graph.inputs) == 2
    assert len(graph.outputs) == 2
    assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock)
    assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock)
    assert isinstance(graph.override_hps[0], hp_module.Choice)
Пример #17
0
def test_set_hp():
    input_node = ak.Input((32, ))
    output_node = input_node
    output_node = ak.DenseBlock()(output_node)
    head = ak.RegressionHead()
    head.output_shape = (1, )
    output_node = head(output_node)

    graph = graph_module.HyperGraph(input_node,
                                    output_node,
                                    override_hps=[
                                        hp_module.Choice(
                                            'dense_block_1/num_layers', [6],
                                            default=6)
                                    ])
    hp = kerastuner.HyperParameters()
    plain_graph = graph.hyper_build(hp)
    plain_graph.build_keras_graph().build(hp)

    for single_hp in hp.space:
        if single_hp.name == 'dense_block_1/num_layers':
            assert len(single_hp.values) == 1
            assert single_hp.values[0] == 6
            return
    assert False
Пример #18
0
def test_graph_save_load(tmp_path):
    input1 = ak.Input()
    input2 = ak.Input()
    output1 = ak.DenseBlock()(input1)
    output2 = ak.ConvBlock()(input2)
    output = ak.Merge()([output1, output2])
    output1 = ak.RegressionHead()(output)
    output2 = ak.ClassificationHead()(output)

    graph = graph_module.Graph(
        inputs=[input1, input2],
        outputs=[output1, output2],
        override_hps=[
            hp_module.Choice("dense_block_1/num_layers", [6], default=6)
        ],
    )
    path = os.path.join(tmp_path, "graph")
    graph.save(path)
    graph = graph_module.load_graph(path)

    assert len(graph.inputs) == 2
    assert len(graph.outputs) == 2
    assert isinstance(graph.inputs[0].out_blocks[0], ak.DenseBlock)
    assert isinstance(graph.inputs[1].out_blocks[0], ak.ConvBlock)
    assert isinstance(graph.override_hps[0], hp_module.Choice)
Пример #19
0
def test_prob_one_choice():
    hp = hp_module.Choice('a', [0, 1, 2])
    # Check that boundaries are valid.
    value = hp_module.cumulative_prob_to_value(1, hp)
    assert value == 2

    value = hp_module.cumulative_prob_to_value(0, hp)
    assert value == 0
Пример #20
0
 def assemble(self, input_node):
     block = hyperblock.ImageBlock()
     if max(self._shape[0], self._shape[1]) < 32:
         if self._num_samples < 10000:
             self.hps.append(hp_module.Choice(
                             block.name + '_resnet/v1/conv4_depth', [6],
                             default=6))
             self.hps.append(hp_module.Choice(
                             block.name + '_resnet/v2/conv4_depth', [6],
                             default=6))
             self.hps.append(hp_module.Choice(
                             block.name + '_resnet/next/conv4_depth', [6],
                             default=6))
             self.hps.append(hp_module.Int(
                             block.name + '_xception/num_residual_blocks', 2, 4,
                             default=4))
     return block(input_node)
Пример #21
0
def test_hyperband_save_load_at_the_end_of_bandit(tmp_dir):
    hp_list = [
        hp_module.Choice('a', [1, 2], default=1),
        hp_module.Choice('b', [3, 4], default=3),
        hp_module.Choice('c', [5, 6], default=5),
        hp_module.Choice('d', [7, 8], default=7),
        hp_module.Choice('e', [9, 0], default=9)
    ]
    oracle = hyperband_module.HyperbandOracle()

    for trial_id in range(oracle._model_sequence[0]):
        hp = oracle.populate_space('0_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
    assert oracle.populate_space('idle', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[0]):
        oracle.result('0_' + str(trial_id), trial_id)

    for trial_id in range(oracle._model_sequence[1]):
        hp = oracle.populate_space('1_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
    assert oracle.populate_space('idle1', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[1]):
        oracle.result('1_' + str(trial_id), trial_id)

    for trial_id in range(oracle._model_sequence[2]):
        hp = oracle.populate_space('2_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
        assert hp['values']['tuner/epochs'] == oracle._epoch_sequence[2]
        assert 'tuner/trial_id' in hp['values']
    assert oracle.populate_space('idle2', hp_list)['status'] == 'IDLE'
    for trial_id in range(oracle._model_sequence[2]):
        oracle.result('2_' + str(trial_id), trial_id)

    fname = os.path.join(tmp_dir, 'oracle')
    oracle.save(fname)
    oracle = hyperband_module.HyperbandOracle()
    oracle.reload(fname)

    for trial_id in range(oracle._model_sequence[0]):
        hp = oracle.populate_space('3_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
        assert hp['values']['tuner/epochs'] == oracle._epoch_sequence[0]
        assert 'tuner/trial_id' not in hp['values']
    assert oracle.populate_space('idle3', hp_list)['status'] == 'IDLE'
Пример #22
0
def test_bayesian_save_reload(tmp_dir):
    hp_list = [hp_module.Choice('a', [1, 2], default=1),
               hp_module.Choice('b', [3, 4], default=3),
               hp_module.Choice('c', [5, 6], default=5),
               hp_module.Choice('d', [7, 8], default=7),
               hp_module.Choice('e', [9, 0], default=9)]
    oracle = bo_module.BayesianOptimizationOracle()

    for trial_id in range(3):
        oracle.populate_space('0_' + str(trial_id), hp_list)
    for trial_id in range(2):
        oracle.result('0_' + str(trial_id), trial_id)

    fname = os.path.join(tmp_dir, 'oracle')
    oracle.save(fname)
    oracle = bo_module.BayesianOptimizationOracle()
    oracle.reload(fname)

    for trial_id in range(20):
        hp = oracle.populate_space('1_' + str(trial_id), hp_list)
        assert hp['status'] == 'RUN'
Пример #23
0
 def __init__(
     self,
     max_sequence_length: Optional[Union[int,
                                         hyperparameters.Choice]] = None,
     **kwargs,
 ):
     super().__init__(**kwargs)
     self.max_sequence_length = utils.get_hyperparameter(
         max_sequence_length,
         hyperparameters.Choice("max_sequence_length", [128, 256, 512],
                                default=128),
         int,
     )
Пример #24
0
def test_set_hp():
    x_train = np.random.rand(100, 32)
    y_train = np.random.rand(100, 1)

    input_node = ak.Input((32, ))
    output_node = input_node
    output_node = ak.DenseBlock()(output_node)
    head = ak.RegressionHead()
    head.output_shape = (1, )
    output_node = head(output_node)

    graph = ak.hypermodel.graph.GraphHyperModel(input_node, output_node)
    hp = kerastuner.HyperParameters()
    graph.set_hps(
        [hp_module.Choice('dense_block_1/num_layers', [6], default=6)])
    graph.build(hp)

    for single_hp in hp.space:
        if single_hp.name == 'dense_block_1/num_layers':
            assert len(single_hp.values) == 1
            assert single_hp.values[0] == 6
            return
    assert False
Пример #25
0
def test_Choice_ordered_invalid():
    with pytest.raises(ValueError, match='must be `False`'):
        hp_module.Choice('a', ['a', 'b'], ordered=True)
Пример #26
0
def test_Choice_ordered(values, ordered_arg, ordered_val):
    choice = hp_module.Choice('choice', values, ordered=ordered_arg)
    assert choice.ordered == ordered_val
    choice_new = hp_module.Choice(**choice.get_config())
    assert choice_new.ordered == ordered_val
Пример #27
0
def test_get_hyperparameter_with_int_return_int():
    value = utils.get_hyperparameter(10,
                                     hyperparameters.Choice("hp",
                                                            [10, 20]), int)
    assert isinstance(value, int)
    assert value == 10
Пример #28
0
def test_get_hyperparameter_with_none_return_hp():
    hp = utils.get_hyperparameter(None, hyperparameters.Choice("hp", [10, 20]),
                                  int)
    assert isinstance(hp, hyperparameters.Choice)
Пример #29
0
def test_Choice_ordered_invalid():
    with pytest.raises(ValueError, match="must be `False`"):
        hp_module.Choice("a", ["a", "b"], ordered=True)
Пример #30
0
def test_get_hyperparameter_with_int_return_fixed():
    hp = utils.get_hyperparameter(10, hyperparameters.Choice("hp", [10, 20]),
                                  int)
    assert isinstance(hp, hyperparameters.Fixed)