Skip to content

Commit

Permalink
adapt to kt 1.0.3 (#1593)
Browse files Browse the repository at this point in the history
Co-authored-by: Haifeng Jin <[email protected]>
  • Loading branch information
haifeng-jin and haifeng-jin authored Jun 17, 2021
1 parent b8b4628 commit 5e23395
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 156 deletions.
2 changes: 1 addition & 1 deletion autokeras/auto_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class AutoModel(object):
seed: Int. Random seed.
max_model_size: Int. Maximum number of scalars in the parameters of a
model. Models larger than this are rejected.
**kwargs: Any arguments supported by kerastuner.Tuner.
**kwargs: Any arguments supported by keras_tuner.Tuner.
"""

def __init__(
Expand Down
38 changes: 19 additions & 19 deletions autokeras/blocks/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Union

import tensorflow as tf
from kerastuner.engine import hyperparameters
from keras_tuner.engine import hyperparameters
from tensorflow.keras import applications
from tensorflow.keras import layers
from tensorflow.python.util import nest
Expand Down Expand Up @@ -57,15 +57,15 @@ class DenseBlock(block_module.Block):
"""Block for Dense layers.
# Arguments
num_layers: Int or kerastuner.engine.hyperparameters.Choice.
num_layers: Int or keras_tuner.engine.hyperparameters.Choice.
The number of Dense layers in the block.
If left unspecified, it will be tuned automatically.
num_units: Int or kerastuner.engine.hyperparameters.Choice.
num_units: Int or keras_tuner.engine.hyperparameters.Choice.
The number of units in each dense layer.
If left unspecified, it will be tuned automatically.
use_bn: Boolean. Whether to use BatchNormalization layers.
If left unspecified, it will be tuned automatically.
dropout: Float or kerastuner.engine.hyperparameters.Choice.
dropout: Float or keras_tuner.engine.hyperparameters.Choice.
The dropout rate for the layers.
If left unspecified, it will be tuned automatically.
"""
Expand Down Expand Up @@ -147,13 +147,13 @@ class RNNBlock(block_module.Block):
# Arguments
return_sequences: Boolean. Whether to return the last output in the
output sequence, or the full sequence. Defaults to False.
bidirectional: Boolean or kerastuner.engine.hyperparameters.Boolean.
bidirectional: Boolean or keras_tuner.engine.hyperparameters.Boolean.
Bidirectional RNN. If left unspecified, it will be
tuned automatically.
num_layers: Int or kerastuner.engine.hyperparameters.Choice.
num_layers: Int or keras_tuner.engine.hyperparameters.Choice.
The number of layers in RNN. If left unspecified, it will
be tuned automatically.
layer_type: String or or kerastuner.engine.hyperparameters.Choice.
layer_type: String or or keras_tuner.engine.hyperparameters.Choice.
'gru' or 'lstm'. If left unspecified, it will be tuned
automatically.
"""
Expand Down Expand Up @@ -244,17 +244,17 @@ class ConvBlock(block_module.Block):
"""Block for vanilla ConvNets.
# Arguments
kernel_size: Int or kerastuner.engine.hyperparameters.Choice.
kernel_size: Int or keras_tuner.engine.hyperparameters.Choice.
The size of the kernel.
If left unspecified, it will be tuned automatically.
num_blocks: Int or kerastuner.engine.hyperparameters.Choice.
num_blocks: Int or keras_tuner.engine.hyperparameters.Choice.
The number of conv blocks, each of which may contain
convolutional, max pooling, dropout, and activation. If left unspecified,
it will be tuned automatically.
num_layers: Int or hyperparameters.Choice.
The number of convolutional layers in each block. If left
unspecified, it will be tuned automatically.
filters: Int or kerastuner.engine.hyperparameters.Choice. The number of
filters: Int or keras_tuner.engine.hyperparameters.Choice. The number of
filters in the convolutional layers. If left unspecified, it will
be tuned automatically.
max_pooling: Boolean. Whether to use max pooling layer in each block. If left
Expand Down Expand Up @@ -487,20 +487,20 @@ class Transformer(block_module.Block):
# Arguments
max_features: Int. Size of the vocabulary. Must be set if not using
TextToIntSequence before this block. Defaults to 20001.
pretraining: String or kerastuner.engine.hyperparameters.Choice.
pretraining: String or keras_tuner.engine.hyperparameters.Choice.
'random' (use random weights instead any pretrained
model), 'glove', 'fasttext' or 'word2vec'. Use pretrained word embedding.
If left unspecified, it will be tuned automatically.
embedding_dim: Int or kerastuner.engine.hyperparameters.Choice.
embedding_dim: Int or keras_tuner.engine.hyperparameters.Choice.
Output dimension of the Attention block.
If left unspecified, it will be tuned automatically.
num_heads: Int or kerastuner.engine.hyperparameters.Choice.
num_heads: Int or keras_tuner.engine.hyperparameters.Choice.
The number of attention heads. If left unspecified,
it will be tuned automatically.
dense_dim: Int or kerastuner.engine.hyperparameters.Choice.
dense_dim: Int or keras_tuner.engine.hyperparameters.Choice.
The output dimension of the Feed-Forward Network. If left
unspecified, it will be tuned automatically.
dropout: Float or kerastuner.engine.hyperparameters.Choice.
dropout: Float or keras_tuner.engine.hyperparameters.Choice.
Between 0 and 1. If left unspecified, it will be
tuned automatically.
"""
Expand Down Expand Up @@ -818,14 +818,14 @@ class Embedding(block_module.Block):
# Arguments
max_features: Int. Size of the vocabulary. Must be set if not using
TextToIntSequence before this block. Defaults to 20001.
pretraining: String or kerastuner.engine.hyperparameters.Choice.
pretraining: String or keras_tuner.engine.hyperparameters.Choice.
'random' (use random weights instead any pretrained
model), 'glove', 'fasttext' or 'word2vec'. Use pretrained word embedding.
If left unspecified, it will be tuned automatically.
embedding_dim: Int or kerastuner.engine.hyperparameters.Choice.
embedding_dim: Int or keras_tuner.engine.hyperparameters.Choice.
Output dimension of the Attention block.
If left unspecified, it will be tuned automatically.
dropout: Float or kerastuner.engine.hyperparameters.Choice.
dropout: Float or keras_tuner.engine.hyperparameters.Choice.
The dropout rate for the layers.
If left unspecified, it will be tuned automatically.
"""
Expand Down Expand Up @@ -929,7 +929,7 @@ class BertBlock(block_module.Block):
clf = ak.AutoModel(inputs=input_node, outputs=output_node, max_trials=10)
```
# Arguments
max_sequence_length: Int or kerastuner.engine.hyperparameters.Choice.
max_sequence_length: Int or keras_tuner.engine.hyperparameters.Choice.
The maximum length of a sequence that is used to train the model.
"""

Expand Down
4 changes: 2 additions & 2 deletions autokeras/engine/named_hypermodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kerastuner
import keras_tuner
import tensorflow as tf

from autokeras.engine import serializable
from autokeras.utils import utils


class NamedHyperModel(kerastuner.HyperModel, serializable.Serializable):
class NamedHyperModel(keras_tuner.HyperModel, serializable.Serializable):
"""
# Arguments
Expand Down
10 changes: 5 additions & 5 deletions autokeras/engine/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import copy
import os

import kerastuner
import keras_tuner
import tensorflow as tf
from kerastuner.engine import hypermodel as hm_module
from keras_tuner.engine import hypermodel as hm_module
from tensorflow.keras import callbacks as tf_callbacks
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.python.util import nest
Expand All @@ -27,7 +27,7 @@
from autokeras.utils import utils


class AutoTuner(kerastuner.engine.tuner.Tuner):
class AutoTuner(keras_tuner.engine.tuner.Tuner):
"""A Tuner class based on KerasTuner for AutoKeras.
Different from KerasTuner's Tuner class. AutoTuner's not only tunes the
Expand All @@ -41,8 +41,8 @@ class AutoTuner(kerastuner.engine.tuner.Tuner):
The fully trained model is the best model to be used by AutoModel.
# Arguments
oracle: kerastuner Oracle.
hypermodel: kerastuner KerasHyperModel.
oracle: keras_tuner Oracle.
hypermodel: keras_tuner KerasHyperModel.
**kwargs: The args supported by KerasTuner.
"""

Expand Down
4 changes: 2 additions & 2 deletions autokeras/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kerastuner
import keras_tuner
import tensorflow as tf
from tensorflow.python.util import nest

Expand Down Expand Up @@ -51,7 +51,7 @@ def load_graph(filepath, custom_objects=None):
return Graph.from_config(io_utils.load_json(filepath))


class Graph(kerastuner.HyperModel, serializable.Serializable):
class Graph(keras_tuner.HyperModel, serializable.Serializable):
"""A graph consists of connected Blocks, or Heads.
# Arguments
Expand Down
4 changes: 2 additions & 2 deletions autokeras/tuners/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kerastuner
import keras_tuner

from autokeras.engine import tuner as tuner_module


class BayesianOptimization(kerastuner.BayesianOptimization, tuner_module.AutoTuner):
class BayesianOptimization(keras_tuner.BayesianOptimization, tuner_module.AutoTuner):
"""KerasTuner BayesianOptimization with preprocessing layer tuning."""

pass
20 changes: 10 additions & 10 deletions autokeras/tuners/greedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import List
from typing import Optional

import kerastuner
import keras_tuner
import numpy as np

from autokeras.engine import tuner as tuner_module
Expand Down Expand Up @@ -75,7 +75,7 @@ def get_hp_names(self, node):
return ret


class GreedyOracle(kerastuner.Oracle):
class GreedyOracle(keras_tuner.Oracle):
"""An oracle combining random search and greedy algorithm.
It groups the HyperParameters into several categories, namely, HyperGraph,
Expand Down Expand Up @@ -118,7 +118,7 @@ def _select_hps(self):
for hp in best_hps.space:
# Not picking the fixed hps for generating new values.
if best_hps.is_active(hp) and not isinstance(
hp, kerastuner.engine.hyperparameters.Fixed
hp, keras_tuner.engine.hyperparameters.Fixed
):
trie.insert(hp.name)
all_nodes = trie.nodes
Expand All @@ -139,11 +139,11 @@ def _next_initial_hps(self):
self._tried_initial_hps[index] = True
return hps

def _populate_space(self, trial_id):
def populate_space(self, trial_id):
if not all(self._tried_initial_hps):
values = self._next_initial_hps()
return {
"status": kerastuner.engine.trial.TrialStatus.RUNNING,
"status": keras_tuner.engine.trial.TrialStatus.RUNNING,
"values": values,
}

Expand All @@ -155,12 +155,12 @@ def _populate_space(self, trial_id):
continue
# Values found.
return {
"status": kerastuner.engine.trial.TrialStatus.RUNNING,
"status": keras_tuner.engine.trial.TrialStatus.RUNNING,
"values": values,
}
# All stages reached max collisions.
return {
"status": kerastuner.engine.trial.TrialStatus.STOPPED,
"status": keras_tuner.engine.trial.TrialStatus.STOPPED,
"values": None,
}

Expand All @@ -176,7 +176,7 @@ def _generate_hp_values(self, hp_names):

collisions = 0
while True:
hps = kerastuner.HyperParameters()
hps = keras_tuner.HyperParameters()
# Generate a set of random values.
for hp in self.hyperparameters.space:
hps.merge([hp])
Expand Down Expand Up @@ -207,12 +207,12 @@ def _generate_hp_values(self, hp_names):
class Greedy(tuner_module.AutoTuner):
def __init__(
self,
hypermodel: kerastuner.HyperModel,
hypermodel: keras_tuner.HyperModel,
objective: str = "val_loss",
max_trials: int = 10,
initial_hps: Optional[List[Dict[str, Any]]] = None,
seed: Optional[int] = None,
hyperparameters: Optional[kerastuner.HyperParameters] = None,
hyperparameters: Optional[keras_tuner.HyperParameters] = None,
tune_new_entries: bool = True,
allow_new_entries: bool = True,
**kwargs
Expand Down
4 changes: 2 additions & 2 deletions autokeras/tuners/hyperband.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kerastuner
import keras_tuner

from autokeras.engine import tuner as tuner_module


class Hyperband(kerastuner.Hyperband, tuner_module.AutoTuner):
class Hyperband(keras_tuner.Hyperband, tuner_module.AutoTuner):
"""KerasTuner Hyperband with preprocessing layer tuning."""

def __init__(
Expand Down
4 changes: 2 additions & 2 deletions autokeras/tuners/random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import kerastuner
import keras_tuner

from autokeras.engine import tuner as tuner_module


class RandomSearch(kerastuner.RandomSearch, tuner_module.AutoTuner):
class RandomSearch(keras_tuner.RandomSearch, tuner_module.AutoTuner):
"""KerasTuner RandomSearch with preprocessing layer tuning."""

pass
12 changes: 6 additions & 6 deletions autokeras/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import re

import kerastuner
import keras_tuner
import tensorflow as tf
from packaging.version import parse
from tensorflow.python.util import nest
Expand Down Expand Up @@ -48,14 +48,14 @@ def check_tf_version() -> None:


def check_kt_version() -> None:
if parse(kerastuner.__version__) < parse("1.0.2"):
if parse(keras_tuner.__version__) < parse("1.0.3"):
raise ImportError(
"The Keras Tuner package version needs to be at least 1.0.2 \n"
"The Keras Tuner package version needs to be at least 1.0.3 \n"
"for AutoKeras to run. Currently, your Keras Tuner version is \n"
"{version}. Please upgrade with \n"
"`$ pip install --upgrade keras-tuner`. \n"
"You can use `pip freeze` to check afterwards that everything is "
"ok.".format(version=kerastuner.__version__)
"ok.".format(version=keras_tuner.__version__)
)


Expand Down Expand Up @@ -124,10 +124,10 @@ def add_to_hp(hp, hps, name=None):
"""Add the HyperParameter (self) to the HyperParameters.
# Arguments
hp: kerastuner.HyperParameters.
hp: keras_tuner.HyperParameters.
name: String. If left unspecified, the hp name is used.
"""
if not isinstance(hp, kerastuner.engine.hyperparameters.HyperParameter):
if not isinstance(hp, keras_tuner.engine.hyperparameters.HyperParameter):
return hp
kwargs = hp.get_config()
if name is None:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/auto_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from unittest import mock

import kerastuner
import keras_tuner
import numpy as np
import pytest
import tensorflow as tf
Expand All @@ -38,7 +38,7 @@ def test_auto_model_objective_is_kt_objective(tmp_path):
ak.ImageInput(), ak.RegressionHead(), directory=tmp_path
)

assert isinstance(auto_model.objective, kerastuner.Objective)
assert isinstance(auto_model.objective, keras_tuner.Objective)


def test_auto_model_max_trial_field_as_specified(tmp_path):
Expand Down
Loading

0 comments on commit 5e23395

Please sign in to comment.