Repository: keras-team/autokeras

Issue:
KeyError: 'image_block_1/xception_block_1/trainable'
### Bug Description
I've ran the ImageClassifier for 100 trials. Some times there were some downloads. 
And after one the generator couldn't produce valid networks because of error.
It was after +-25 trials.

```
 Traceback (most recent call last):
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\kerastuner\engine\hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\kerastuner\engine\hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\autokeras\graph.py", line 263, in build
    outputs = block.build(hp, inputs=temp_inputs)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\autokeras\engine\block.py", line 48, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\kerastuner\engine\hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\autokeras\blocks\wrapper.py", line 102, in build
    output_node = self._build_block(hp, output_node, block_type)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\autokeras\blocks\wrapper.py", line 77, in _build_block
    return basic.XceptionBlock().build(hp, output_node)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\autokeras\engine\block.py", line 48, in _build_wrapper
    return super()._build_wrapper(hp, *args, **kwargs)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\kerastuner\engine\hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\autokeras\blocks\basic.py", line 574, in build
    model.trainable = hp.Boolean("trainable", default=False)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\kerastuner\engine\hyperparameters.py", line 814, in Boolean
    return self._retrieve(hp)
  File "P:\ProgramFiles\anaconda\envs\python\test\lib\site-packages\kerastuner\engine\hyperparameters.py", line 625, in _retrieve
    return self.values[hp.name]
KeyError: 'image_block_1/xception_block_1/trainable'
```

### Bug Reproduction
```
model = ak.ImageClassifier(
            distribution_strategy=tf.distribute.experimental.MultiWorkerMirroredStrategy(),
            objective="val_loss",
            max_trials=100)
model.fit(x=train_im, y=train_lb, epochs=None, validation_data=(val_im, val_lb))
```
Data used by the code:
There are images 128x128 divided in 13 classes
### Expected Behavior
There shouldn't be KeyError

### Setup Details
Include the details about the versions of:
 - OS type and version: Win 10 2004
 - Python: 3.7
 - autokeras: 1.0.6
 - keras-tuner: 1.0.2rc1
 - scikit-learn: 0.23.2
 - numpy: 1.18.5
 - pandas: 1.10
 - tensorflow: 2.3 (gpu)

### Additional context
I am new to issue opening, so there can be some mistakes...



Discussion:

Resolving patch:
diff --git a/autokeras/blocks/basic.py b/autokeras/blocks/basic.py
--- a/autokeras/blocks/basic.py
+++ b/autokeras/blocks/basic.py
@@ -36,6 +36,8 @@
     "resnet152_v2": applications.ResNet152V2,
 }
 
+PRETRAINED = "pretrained"
+
 
 class DenseBlock(block_module.Block):
     """Block for Dense layers.
@@ -547,7 +549,12 @@ def build(self, hp, inputs=None):
                 )
             pretrained = False
         if pretrained is None:
-            pretrained = hp.Boolean("pretrained", default=False)
+            pretrained = hp.Boolean(PRETRAINED, default=False)
+            if pretrained:
+                with hp.conditional_scope(PRETRAINED, [True]):
+                    trainable = hp.Boolean("trainable", default=False)
+        elif pretrained:
+            trainable = hp.Boolean("trainable", default=False)
 
         if len(self.models) > 1:
             version = hp.Choice("version", list(self.models.keys()))
@@ -571,7 +578,7 @@ def build(self, hp, inputs=None):
 
         if pretrained:
             model = self.models[version](weights="imagenet", include_top=False)
-            model.trainable = hp.Boolean("trainable", default=False)
+            model.trainable = trainable
         else:
             model = self.models[version](
                 weights=None, include_top=False, input_shape=input_node.shape[1:]
diff --git a/autokeras/tuners/greedy.py b/autokeras/tuners/greedy.py
--- a/autokeras/tuners/greedy.py
+++ b/autokeras/tuners/greedy.py
@@ -160,9 +160,9 @@ def _populate_space(self, trial_id):
     def _get_best_hps(self):
         best_trials = self.get_best_trials()
         if best_trials:
-            return best_trials[0].hyperparameters
+            return best_trials[0].hyperparameters.copy()
         else:
-            return self.hyperparameters
+            return self.hyperparameters.copy()
 
     def _generate_hp_values(self, hp_names):
         best_hps = self._get_best_hps()
@@ -171,13 +171,14 @@ def _generate_hp_values(self, hp_names):
         while True:
             hps = kerastuner.HyperParameters()
             # Generate a set of random values.
-            for hp in best_hps.space:
+            for hp in self.hyperparameters.space:
                 hps.merge([hp])
                 # if not active, do nothing.
                 # if active, check if selected to be changed.
                 if hps.is_active(hp):
                     # if was active and not selected, do nothing.
                     if best_hps.is_active(hp.name) and hp.name not in hp_names:
+                        hps.values[hp.name] = best_hps.values[hp.name]
                         continue
                     # if was not active or selected, sample.
                     hps.values[hp.name] = hp.random_sample(self._seed_state)


Test patch:
diff --git a/tests/autokeras/tuners/greedy_test.py b/tests/autokeras/tuners/greedy_test.py
--- a/tests/autokeras/tuners/greedy_test.py
+++ b/tests/autokeras/tuners/greedy_test.py
@@ -15,13 +15,15 @@
 from unittest import mock
 
 import kerastuner
+import tensorflow as tf
 
 from autokeras.tuners import greedy
+from autokeras.tuners import task_specific
 from tests import utils
 
 
 def test_greedy_oracle_get_state_update_space_can_run():
-    oracle = greedy.GreedyOracle(objective="val_loss",)
+    oracle = greedy.GreedyOracle(objective="val_loss")
     oracle.set_state(oracle.get_state())
     hp = kerastuner.HyperParameters()
     hp.Boolean("test")
@@ -33,7 +35,7 @@ def test_greedy_oracle_populate_different_values(get_best_trials):
     hp = kerastuner.HyperParameters()
     utils.build_graph().build(hp)
 
-    oracle = greedy.GreedyOracle(objective="val_loss", seed=utils.SEED,)
+    oracle = greedy.GreedyOracle(objective="val_loss", seed=utils.SEED)
     trial = mock.Mock()
     trial.hyperparameters = hp
     get_best_trials.return_value = [trial]
@@ -45,6 +47,30 @@ def test_greedy_oracle_populate_different_values(get_best_trials):
     assert not all([values_a[key] == values_b[key] for key in values_a])
 
 
+@mock.patch("autokeras.tuners.greedy.GreedyOracle.get_best_trials")
+def test_greedy_oracle_populate_doesnt_crash_with_init_hps(get_best_trials):
+    hp = kerastuner.HyperParameters()
+    graph = utils.build_graph()
+    graph.build(hp)
+
+    oracle = greedy.GreedyOracle(
+        initial_hps=task_specific.IMAGE_CLASSIFIER,
+        objective="val_loss",
+        seed=utils.SEED,
+    )
+    trial = mock.Mock()
+    trial.hyperparameters = hp
+    get_best_trials.return_value = [trial]
+
+    for i in range(10):
+        tf.keras.backend.clear_session()
+        values = oracle._populate_space("a")["values"]
+        hp = oracle.hyperparameters.copy()
+        hp.values = values
+        graph.build(hp)
+        oracle.update_space(hp)
+
+
 @mock.patch("autokeras.tuners.greedy.GreedyOracle._compute_values_hash")
 @mock.patch("autokeras.tuners.greedy.GreedyOracle.get_best_trials")
 def test_greedy_oracle_stop_reach_max_collision(
@@ -54,7 +80,7 @@ def test_greedy_oracle_stop_reach_max_collision(
     hp = kerastuner.HyperParameters()
     utils.build_graph().build(hp)
 
-    oracle = greedy.GreedyOracle(objective="val_loss", seed=utils.SEED,)
+    oracle = greedy.GreedyOracle(objective="val_loss", seed=utils.SEED)
     trial = mock.Mock()
     trial.hyperparameters = hp
     get_best_trials.return_value = [trial]
@@ -72,7 +98,7 @@ def test_greedy_oracle_stop_reach_max_collision(
 def test_greedy_oracle_populate_space_with_no_hp(get_best_trials):
     hp = kerastuner.HyperParameters()
 
-    oracle = greedy.GreedyOracle(objective="val_loss", seed=utils.SEED,)
+    oracle = greedy.GreedyOracle(objective="val_loss", seed=utils.SEED)
     trial = mock.Mock()
     trial.hyperparameters = hp
     get_best_trials.return_value = [trial]