Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for compatibility issue with kt 1.1.0 #1687

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions autokeras/auto_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ def _build_graph(self):
elif all([isinstance(output, head_module.Head) for output in self.outputs]):
# Clear session to reset get_uid(). The names of the blocks will
# start to count from 1 for new blocks in a new AutoModel afterwards.
# When initializing multiple AutoModel with Task API, if not
# counting from 1 for each of the AutoModel, the predefined hp
# values in task specifiec tuners would not match the names.
tf.keras.backend.clear_session()
graph = self._assemble()
self.outputs = graph.outputs
Expand Down
6 changes: 3 additions & 3 deletions autokeras/engine/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _prepare_model_build(self, hp, **kwargs):
return pipeline, dataset, validation_data

def _build_and_fit_model(self, trial, *args, **kwargs):
model = self.hypermodel.build(trial.hyperparameters)
model = self._try_build(trial.hyperparameters)
(
pipeline,
kwargs["x"],
Expand Down Expand Up @@ -188,7 +188,7 @@ def search(
# Populate initial search space.
hp = self.oracle.get_space()
self._prepare_model_build(hp, **fit_kwargs)
self.hypermodel.build(hp)
self._try_build(hp)
self.oracle.update_space(hp)
super().search(
epochs=epochs, callbacks=new_callbacks, verbose=verbose, **fit_kwargs
Expand Down Expand Up @@ -255,7 +255,7 @@ def _get_best_trial_epochs(self):
def _build_best_model(self):
best_trial = self.oracle.get_best_trials(1)[0]
best_hp = best_trial.hyperparameters
return self.hypermodel.build(best_hp)
return self._try_build(best_hp)

def final_fit(self, **kwargs):
best_trial = self.oracle.get_best_trials(1)[0]
Expand Down
26 changes: 26 additions & 0 deletions autokeras/engine/tuner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tensorflow as tf
from tensorflow.keras.layers.experimental import preprocessing

import autokeras as ak
from autokeras import keras_layers
from autokeras import test_utils
from autokeras.engine import tuner as tuner_module
Expand Down Expand Up @@ -188,3 +189,28 @@ def test_adapt_with_model_with_preprocessing_layer_only():
(np.random.rand(100, 10), np.random.rand(100, 10))
).batch(32),
)


def test_build_block_in_blocks_with_same_name(tmp_path):
class Block1(ak.Block):
def build(self, hp, inputs):
hp.Boolean("a")
return tf.keras.layers.Dense(3)(tf.nest.flatten(inputs)[0])

class Block2(ak.Block):
def build(self, hp, inputs):
hp.Boolean("b")
return Block1().build(hp, inputs)

inputs = ak.Input()
outputs = Block2()(inputs)
outputs = ak.RegressionHead()(outputs)
auto_model = ak.AutoModel(inputs, outputs, max_trials=5, directory=tmp_path)
auto_model.fit(np.random.rand(100, 5), np.random.rand(100, 1), epochs=1)

trials = [trial for trial_id, trial in auto_model.tuner.oracle.trials.items()]
for trial in trials:
print(trial.hyperparameters.values)
assert len(trial.hyperparameters.values) == len(
trials[0].hyperparameters.values
)