We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add evaluate function to AutoModel
Would it be relevant to have an `evaluate` method as well? e.g. a standard practice I would expect is to have 2 validation sets, one used for search during training, and one used at the end as the actual validation set (to prevent validation overfitting).

_Originally posted by @fchollet in https://github.com/keras-team/autokeras/pull/691_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `autokeras/auto_model.py`
Content:
```
1 import numpy as np
2 import pandas as pd
3 import tensorflow as tf
4 from tensorflow.python.util import nest
5 
6 import kerastuner
7 from autokeras import meta_model
8 from autokeras import tuner
9 from autokeras import utils
10 from autokeras.hypermodel import graph
11 from autokeras.hypermodel import head
12 from autokeras.hypermodel import node
13 
14 
15 class AutoModel(object):
16     """ A Model defined by inputs and outputs.
17 
18     AutoModel combines a HyperModel and a Tuner to tune the HyperModel.
19     The user can use it in a similar way to a Keras model since it
20     also has `fit()` and  `predict()` methods.
21 
22     The user can specify the inputs and outputs of the AutoModel. It will infer
23     the rest of the high-level neural architecture.
24 
25     # Arguments
26         inputs: A list of or a HyperNode instance.
27             The input node(s) of the AutoModel.
28         outputs: A list of or a HyperHead instance.
29             The output head(s) of the AutoModel.
30         name: String. The name of the AutoModel. Defaults to 'auto_model'.
31         max_trials: Int. The maximum number of different Keras Models to try.
32             The search may finish before reaching the max_trials. Defaults to 100.
33         directory: String. The path to a directory for storing the search outputs.
34             Defaults to None, which would create a folder with the name of the
35             AutoModel in the current directory.
36         seed: Int. Random seed.
37     """
38 
39     def __init__(self,
40                  inputs,
41                  outputs,
42                  name='auto_model',
43                  max_trials=100,
44                  directory=None,
45                  seed=None):
46         self.inputs = nest.flatten(inputs)
47         self.outputs = nest.flatten(outputs)
48         self.name = name
49         self.tuner = None
50         self.max_trials = max_trials
51         self.directory = directory
52         self.seed = seed
53         self.hypermodel = None
54         if all([isinstance(output_node, head.Head)
55                 for output_node in self.outputs]):
56             self.heads = self.outputs
57         else:
58             self.heads = [output_node.in_blocks[0] for output_node in self.outputs]
59 
60     def _meta_build(self, dataset):
61         self.hypermodel = meta_model.assemble(inputs=self.inputs,
62                                               outputs=self.outputs,
63                                               dataset=dataset,
64                                               seed=self.seed)
65         self.outputs = self.hypermodel.outputs
66 
67     def fit(self,
68             x=None,
69             y=None,
70             validation_split=0,
71             validation_data=None,
72             **kwargs):
73         """Search for the best model and hyperparameters for the AutoModel.
74 
75         It will search for the best model based on the performances on
76         validation data.
77 
78         # Arguments
79             x: numpy.ndarray or tensorflow.Dataset. Training data x.
80             y: numpy.ndarray or tensorflow.Dataset. Training data y.
81             validation_split: Float between 0 and 1.
82                 Fraction of the training data to be used as validation data.
83                 The model will set apart this fraction of the training data,
84                 will not train on it, and will evaluate
85                 the loss and any model metrics
86                 on this data at the end of each epoch.
87                 The validation data is selected from the last samples
88                 in the `x` and `y` data provided, before shuffling. This argument is
89                 not supported when `x` is a dataset.
90             validation_data: Data on which to evaluate
91                 the loss and any model metrics at the end of each epoch.
92                 The model will not be trained on this data.
93                 `validation_data` will override `validation_split`.
94                 `validation_data` could be:
95                   - tuple `(x_val, y_val)` of Numpy arrays or tensors
96                   - tuple `(x_val, y_val, val_sample_weights)` of Numpy arrays
97                   - dataset or a dataset iterator
98                 For the first two cases, `batch_size` must be provided.
99                 For the last case, `validation_steps` must be provided.
100             **kwargs: Any arguments supported by keras.Model.fit.
101         """
102         dataset, validation_data = self._prepare_data(
103             x=x,
104             y=y,
105             validation_data=validation_data,
106             validation_split=validation_split)
107 
108         # Initialize the hypermodel.
109         self._meta_build(dataset)
110         self.hypermodel.set_io_shapes(dataset)
111 
112         # Build the hypermodel in tuner init.
113         hp = kerastuner.HyperParameters()
114         self.hypermodel.hyper_build(hp)
115         self.hypermodel.preprocess(
116             hp=kerastuner.HyperParameters(),
117             dataset=dataset,
118             validation_data=validation_data,
119             fit=True)
120         self.tuner = tuner.RandomSearch(
121             hypermodel=self.hypermodel,
122             objective='val_loss',
123             max_trials=self.max_trials,
124             directory=self.directory,
125             seed=self.seed,
126             project_name=self.name)
127         self.hypermodel.clear_preprocessors()
128 
129         # TODO: allow early stop if epochs is not specified.
130         self.tuner.search(x=dataset,
131                           validation_data=validation_data,
132                           **kwargs)
133 
134     def _process_xy(self, x, y=None, fit=False, predict=False):
135         """Convert x, y to tf.data.Dataset.
136 
137         # Arguments
138             x: Any type allowed by the corresponding input node.
139             y: Any type allowed by the corresponding head.
140             fit: Boolean. Whether to fit the type converter with the provided data.
141             predict: Boolean. If it is called by the predict function of AutoModel.
142 
143         # Returns
144             A tf.data.Dataset containing both x and y.
145         """
146         if isinstance(x, tf.data.Dataset):
147             if y is None and not predict:
148                 return x
149             if isinstance(y, tf.data.Dataset):
150                 return tf.data.Dataset.zip((x, y))
151 
152         x = nest.flatten(x)
153         new_x = []
154         for data, input_node in zip(x, self.inputs):
155             if fit:
156                 input_node.fit(data)
157             data = input_node.transform(data)
158             new_x.append(data)
159         x = tf.data.Dataset.zip(tuple(new_x))
160 
161         if predict:
162             return tf.data.Dataset.zip((x, x))
163 
164         if not isinstance(y, tf.data.Dataset):
165             y = nest.flatten(y)
166             new_y = []
167             for data, head_block in zip(y, self.heads):
168                 if fit:
169                     head_block.fit(data)
170                 data = head_block.transform(data)
171                 new_y.append(data)
172             y = tf.data.Dataset.zip(tuple(new_y))
173 
174         return tf.data.Dataset.zip((x, y))
175 
176     def _prepare_data(self, x, y, validation_data, validation_split):
177         """Convert the data to tf.data.Dataset."""
178         # Check validation information.
179         if not validation_data and not validation_split:
180             raise ValueError('Either validation_data or validation_split'
181                              'should be provided.')
182         # TODO: Handle other types of input, zip dataset, tensor, dict.
183         # Prepare the dataset.
184         dataset = self._process_xy(x, y, fit=True)
185         if validation_data:
186             val_x, val_y = validation_data
187             validation_data = self._process_xy(val_x, val_y)
188         # Split the data with validation_split.
189         if validation_data is None and validation_split:
190             dataset, validation_data = utils.split_dataset(dataset, validation_split)
191         return dataset, validation_data
192 
193     def predict(self, x, batch_size=32, **kwargs):
194         """Predict the output for a given testing data.
195 
196         # Arguments
197             x: tf.data.Dataset or numpy.ndarray. Testing data.
198             batch_size: Int. Defaults to 32.
199             **kwargs: Any arguments supported by keras.Model.predict.
200 
201         # Returns
202             A list of numpy.ndarray objects or a single numpy.ndarray.
203             The predicted results.
204         """
205         best_model = self.tuner.get_best_models(1)[0]
206         best_trial = self.tuner.get_best_trials(1)[0]
207         best_hp = best_trial.hyperparameters
208 
209         self.tuner.load_trial(best_trial)
210         x = self._process_xy(x, predict=True)
211         x = self.hypermodel.preprocess(best_hp, x)
212         x = x.batch(batch_size)
213         y = best_model.predict(x, **kwargs)
214         y = self._postprocess(y)
215         if isinstance(y, list) and len(y) == 1:
216             y = y[0]
217         return y
218 
219     def _postprocess(self, y):
220         y = nest.flatten(y)
221         new_y = []
222         for temp_y, head_block in zip(y, self.heads):
223             if isinstance(head_block, head.Head):
224                 temp_y = head_block.postprocess(temp_y)
225             new_y.append(temp_y)
226         return new_y
227 
228 
229 class GraphAutoModel(AutoModel):
230     """A HyperModel defined by a graph of HyperBlocks.
231 
232     GraphAutoModel is a subclass of HyperModel. Besides the HyperModel properties,
233     it also has a tuner to tune the HyperModel. The user can use it in a similar
234     way to a Keras model since it also has `fit()` and  `predict()` methods.
235 
236     The user can specify the high-level neural architecture by connecting the
237     HyperBlocks with the functional API, which is the same as
238     the Keras functional API.
239 
240     # Arguments
241         inputs: A list of or a HyperNode instances.
242             The input node(s) of the GraphAutoModel.
243         outputs: A list of or a HyperNode instances.
244             The output node(s) of the GraphAutoModel.
245         name: String. The name of the AutoModel. Defaults to 'graph_auto_model'.
246         max_trials: Int. The maximum number of different Keras Models to try.
247             The search may finish before reaching the max_trials. Defaults to 100.
248         directory: String. The path to a directory for storing the search outputs.
249             Defaults to None, which would create a folder with the name of the
250             AutoModel in the current directory.
251         seed: Int. Random seed.
252     """
253 
254     def __init__(self,
255                  inputs,
256                  outputs,
257                  name='graph_auto_model',
258                  max_trials=100,
259                  directory=None,
260                  seed=None):
261         super().__init__(
262             inputs=inputs,
263             outputs=outputs,
264             name=name,
265             max_trials=max_trials,
266             directory=directory,
267             seed=seed
268         )
269         self.hypermodel = graph.GraphHyperModel(self.inputs, self.outputs)
270 
271     def _meta_build(self, dataset):
272         pass
273
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/autokeras/auto_model.py b/autokeras/auto_model.py
--- a/autokeras/auto_model.py
+++ b/autokeras/auto_model.py
@@ -194,7 +194,7 @@
         """Predict the output for a given testing data.
 
         # Arguments
-            x: tf.data.Dataset or numpy.ndarray. Testing data.
+            x: Any allowed types according to the input node. Testing data.
             batch_size: Int. Defaults to 32.
             **kwargs: Any arguments supported by keras.Model.predict.
 
@@ -202,14 +202,11 @@
             A list of numpy.ndarray objects or a single numpy.ndarray.
             The predicted results.
         """
-        best_model = self.tuner.get_best_models(1)[0]
-        best_trial = self.tuner.get_best_trials(1)[0]
-        best_hp = best_trial.hyperparameters
-
-        self.tuner.load_trial(best_trial)
-        x = self._process_xy(x, predict=True)
-        x = self.hypermodel.preprocess(best_hp, x)
-        x = x.batch(batch_size)
+        best_model, x = self._prepare_best_model_and_data(
+            x=x,
+            y=None,
+            batch_size=batch_size,
+            predict=True)
         y = best_model.predict(x, **kwargs)
         y = self._postprocess(y)
         if isinstance(y, list) and len(y) == 1:
@@ -225,6 +222,39 @@
             new_y.append(temp_y)
         return new_y
 
+    def evaluate(self, x, y=None, batch_size=32, **kwargs):
+        """Evaluate the best model for the given data.
+
+        # Arguments
+            x: Any allowed types according to the input node. Testing data.
+            y: Any allowed types according to the head. Testing targets.
+                Defaults to None.
+            batch_size: Int. Defaults to 32.
+            **kwargs: Any arguments supported by keras.Model.evaluate.
+
+        # Returns
+            Scalar test loss (if the model has a single output and no metrics) or
+            list of scalars (if the model has multiple outputs and/or metrics).
+            The attribute model.metrics_names will give you the display labels for
+            the scalar outputs.
+        """
+        best_model, data = self._prepare_best_model_and_data(
+            x=x,
+            y=y,
+            batch_size=batch_size)
+        return best_model.evaluate(data, **kwargs)
+
+    def _prepare_best_model_and_data(self, x, y, batch_size, predict=False):
+        best_model = self.tuner.get_best_models(1)[0]
+        best_trial = self.tuner.get_best_trials(1)[0]
+        best_hp = best_trial.hyperparameters
+
+        self.tuner.load_trial(best_trial)
+        x = self._process_xy(x, y, predict=predict)
+        x = self.hypermodel.preprocess(best_hp, x)
+        x = x.batch(batch_size)
+        return best_model, x
+
 
 class GraphAutoModel(AutoModel):
     """A HyperModel defined by a graph of HyperBlocks.

{"golden_diff": "diff --git a/autokeras/auto_model.py b/autokeras/auto_model.py\n--- a/autokeras/auto_model.py\n+++ b/autokeras/auto_model.py\n@@ -194,7 +194,7 @@\n         \"\"\"Predict the output for a given testing data.\n \n         # Arguments\n-            x: tf.data.Dataset or numpy.ndarray. Testing data.\n+            x: Any allowed types according to the input node. Testing data.\n             batch_size: Int. Defaults to 32.\n             **kwargs: Any arguments supported by keras.Model.predict.\n \n@@ -202,14 +202,11 @@\n             A list of numpy.ndarray objects or a single numpy.ndarray.\n             The predicted results.\n         \"\"\"\n-        best_model = self.tuner.get_best_models(1)[0]\n-        best_trial = self.tuner.get_best_trials(1)[0]\n-        best_hp = best_trial.hyperparameters\n-\n-        self.tuner.load_trial(best_trial)\n-        x = self._process_xy(x, predict=True)\n-        x = self.hypermodel.preprocess(best_hp, x)\n-        x = x.batch(batch_size)\n+        best_model, x = self._prepare_best_model_and_data(\n+            x=x,\n+            y=None,\n+            batch_size=batch_size,\n+            predict=True)\n         y = best_model.predict(x, **kwargs)\n         y = self._postprocess(y)\n         if isinstance(y, list) and len(y) == 1:\n@@ -225,6 +222,39 @@\n             new_y.append(temp_y)\n         return new_y\n \n+    def evaluate(self, x, y=None, batch_size=32, **kwargs):\n+        \"\"\"Evaluate the best model for the given data.\n+\n+        # Arguments\n+            x: Any allowed types according to the input node. Testing data.\n+            y: Any allowed types according to the head. Testing targets.\n+                Defaults to None.\n+            batch_size: Int. Defaults to 32.\n+            **kwargs: Any arguments supported by keras.Model.evaluate.\n+\n+        # Returns\n+            Scalar test loss (if the model has a single output and no metrics) or\n+            list of scalars (if the model has multiple outputs and/or metrics).\n+            The attribute model.metrics_names will give you the display labels for\n+            the scalar outputs.\n+        \"\"\"\n+        best_model, data = self._prepare_best_model_and_data(\n+            x=x,\n+            y=y,\n+            batch_size=batch_size)\n+        return best_model.evaluate(data, **kwargs)\n+\n+    def _prepare_best_model_and_data(self, x, y, batch_size, predict=False):\n+        best_model = self.tuner.get_best_models(1)[0]\n+        best_trial = self.tuner.get_best_trials(1)[0]\n+        best_hp = best_trial.hyperparameters\n+\n+        self.tuner.load_trial(best_trial)\n+        x = self._process_xy(x, y, predict=predict)\n+        x = self.hypermodel.preprocess(best_hp, x)\n+        x = x.batch(batch_size)\n+        return best_model, x\n+\n \n class GraphAutoModel(AutoModel):\n     \"\"\"A HyperModel defined by a graph of HyperBlocks.\n", "issue": "Add evaluate function to AutoModel\nWould it be relevant to have an `evaluate` method as well? e.g. a standard practice I would expect is to have 2 validation sets, one used for search during training, and one used at the end as the actual validation set (to prevent validation overfitting).\r\n\r\n_Originally posted by @fchollet in https://github.com/keras-team/autokeras/pull/691_\n", "before_files": [{"content": "import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.python.util import nest\n\nimport kerastuner\nfrom autokeras import meta_model\nfrom autokeras import tuner\nfrom autokeras import utils\nfrom autokeras.hypermodel import graph\nfrom autokeras.hypermodel import head\nfrom autokeras.hypermodel import node\n\n\nclass AutoModel(object):\n    \"\"\" A Model defined by inputs and outputs.\n\n    AutoModel combines a HyperModel and a Tuner to tune the HyperModel.\n    The user can use it in a similar way to a Keras model since it\n    also has `fit()` and  `predict()` methods.\n\n    The user can specify the inputs and outputs of the AutoModel. It will infer\n    the rest of the high-level neural architecture.\n\n    # Arguments\n        inputs: A list of or a HyperNode instance.\n            The input node(s) of the AutoModel.\n        outputs: A list of or a HyperHead instance.\n            The output head(s) of the AutoModel.\n        name: String. The name of the AutoModel. Defaults to 'auto_model'.\n        max_trials: Int. The maximum number of different Keras Models to try.\n            The search may finish before reaching the max_trials. Defaults to 100.\n        directory: String. The path to a directory for storing the search outputs.\n            Defaults to None, which would create a folder with the name of the\n            AutoModel in the current directory.\n        seed: Int. Random seed.\n    \"\"\"\n\n    def __init__(self,\n                 inputs,\n                 outputs,\n                 name='auto_model',\n                 max_trials=100,\n                 directory=None,\n                 seed=None):\n        self.inputs = nest.flatten(inputs)\n        self.outputs = nest.flatten(outputs)\n        self.name = name\n        self.tuner = None\n        self.max_trials = max_trials\n        self.directory = directory\n        self.seed = seed\n        self.hypermodel = None\n        if all([isinstance(output_node, head.Head)\n                for output_node in self.outputs]):\n            self.heads = self.outputs\n        else:\n            self.heads = [output_node.in_blocks[0] for output_node in self.outputs]\n\n    def _meta_build(self, dataset):\n        self.hypermodel = meta_model.assemble(inputs=self.inputs,\n                                              outputs=self.outputs,\n                                              dataset=dataset,\n                                              seed=self.seed)\n        self.outputs = self.hypermodel.outputs\n\n    def fit(self,\n            x=None,\n            y=None,\n            validation_split=0,\n            validation_data=None,\n            **kwargs):\n        \"\"\"Search for the best model and hyperparameters for the AutoModel.\n\n        It will search for the best model based on the performances on\n        validation data.\n\n        # Arguments\n            x: numpy.ndarray or tensorflow.Dataset. Training data x.\n            y: numpy.ndarray or tensorflow.Dataset. Training data y.\n            validation_split: Float between 0 and 1.\n                Fraction of the training data to be used as validation data.\n                The model will set apart this fraction of the training data,\n                will not train on it, and will evaluate\n                the loss and any model metrics\n                on this data at the end of each epoch.\n                The validation data is selected from the last samples\n                in the `x` and `y` data provided, before shuffling. This argument is\n                not supported when `x` is a dataset.\n            validation_data: Data on which to evaluate\n                the loss and any model metrics at the end of each epoch.\n                The model will not be trained on this data.\n                `validation_data` will override `validation_split`.\n                `validation_data` could be:\n                  - tuple `(x_val, y_val)` of Numpy arrays or tensors\n                  - tuple `(x_val, y_val, val_sample_weights)` of Numpy arrays\n                  - dataset or a dataset iterator\n                For the first two cases, `batch_size` must be provided.\n                For the last case, `validation_steps` must be provided.\n            **kwargs: Any arguments supported by keras.Model.fit.\n        \"\"\"\n        dataset, validation_data = self._prepare_data(\n            x=x,\n            y=y,\n            validation_data=validation_data,\n            validation_split=validation_split)\n\n        # Initialize the hypermodel.\n        self._meta_build(dataset)\n        self.hypermodel.set_io_shapes(dataset)\n\n        # Build the hypermodel in tuner init.\n        hp = kerastuner.HyperParameters()\n        self.hypermodel.hyper_build(hp)\n        self.hypermodel.preprocess(\n            hp=kerastuner.HyperParameters(),\n            dataset=dataset,\n            validation_data=validation_data,\n            fit=True)\n        self.tuner = tuner.RandomSearch(\n            hypermodel=self.hypermodel,\n            objective='val_loss',\n            max_trials=self.max_trials,\n            directory=self.directory,\n            seed=self.seed,\n            project_name=self.name)\n        self.hypermodel.clear_preprocessors()\n\n        # TODO: allow early stop if epochs is not specified.\n        self.tuner.search(x=dataset,\n                          validation_data=validation_data,\n                          **kwargs)\n\n    def _process_xy(self, x, y=None, fit=False, predict=False):\n        \"\"\"Convert x, y to tf.data.Dataset.\n\n        # Arguments\n            x: Any type allowed by the corresponding input node.\n            y: Any type allowed by the corresponding head.\n            fit: Boolean. Whether to fit the type converter with the provided data.\n            predict: Boolean. If it is called by the predict function of AutoModel.\n\n        # Returns\n            A tf.data.Dataset containing both x and y.\n        \"\"\"\n        if isinstance(x, tf.data.Dataset):\n            if y is None and not predict:\n                return x\n            if isinstance(y, tf.data.Dataset):\n                return tf.data.Dataset.zip((x, y))\n\n        x = nest.flatten(x)\n        new_x = []\n        for data, input_node in zip(x, self.inputs):\n            if fit:\n                input_node.fit(data)\n            data = input_node.transform(data)\n            new_x.append(data)\n        x = tf.data.Dataset.zip(tuple(new_x))\n\n        if predict:\n            return tf.data.Dataset.zip((x, x))\n\n        if not isinstance(y, tf.data.Dataset):\n            y = nest.flatten(y)\n            new_y = []\n            for data, head_block in zip(y, self.heads):\n                if fit:\n                    head_block.fit(data)\n                data = head_block.transform(data)\n                new_y.append(data)\n            y = tf.data.Dataset.zip(tuple(new_y))\n\n        return tf.data.Dataset.zip((x, y))\n\n    def _prepare_data(self, x, y, validation_data, validation_split):\n        \"\"\"Convert the data to tf.data.Dataset.\"\"\"\n        # Check validation information.\n        if not validation_data and not validation_split:\n            raise ValueError('Either validation_data or validation_split'\n                             'should be provided.')\n        # TODO: Handle other types of input, zip dataset, tensor, dict.\n        # Prepare the dataset.\n        dataset = self._process_xy(x, y, fit=True)\n        if validation_data:\n            val_x, val_y = validation_data\n            validation_data = self._process_xy(val_x, val_y)\n        # Split the data with validation_split.\n        if validation_data is None and validation_split:\n            dataset, validation_data = utils.split_dataset(dataset, validation_split)\n        return dataset, validation_data\n\n    def predict(self, x, batch_size=32, **kwargs):\n        \"\"\"Predict the output for a given testing data.\n\n        # Arguments\n            x: tf.data.Dataset or numpy.ndarray. Testing data.\n            batch_size: Int. Defaults to 32.\n            **kwargs: Any arguments supported by keras.Model.predict.\n\n        # Returns\n            A list of numpy.ndarray objects or a single numpy.ndarray.\n            The predicted results.\n        \"\"\"\n        best_model = self.tuner.get_best_models(1)[0]\n        best_trial = self.tuner.get_best_trials(1)[0]\n        best_hp = best_trial.hyperparameters\n\n        self.tuner.load_trial(best_trial)\n        x = self._process_xy(x, predict=True)\n        x = self.hypermodel.preprocess(best_hp, x)\n        x = x.batch(batch_size)\n        y = best_model.predict(x, **kwargs)\n        y = self._postprocess(y)\n        if isinstance(y, list) and len(y) == 1:\n            y = y[0]\n        return y\n\n    def _postprocess(self, y):\n        y = nest.flatten(y)\n        new_y = []\n        for temp_y, head_block in zip(y, self.heads):\n            if isinstance(head_block, head.Head):\n                temp_y = head_block.postprocess(temp_y)\n            new_y.append(temp_y)\n        return new_y\n\n\nclass GraphAutoModel(AutoModel):\n    \"\"\"A HyperModel defined by a graph of HyperBlocks.\n\n    GraphAutoModel is a subclass of HyperModel. Besides the HyperModel properties,\n    it also has a tuner to tune the HyperModel. The user can use it in a similar\n    way to a Keras model since it also has `fit()` and  `predict()` methods.\n\n    The user can specify the high-level neural architecture by connecting the\n    HyperBlocks with the functional API, which is the same as\n    the Keras functional API.\n\n    # Arguments\n        inputs: A list of or a HyperNode instances.\n            The input node(s) of the GraphAutoModel.\n        outputs: A list of or a HyperNode instances.\n            The output node(s) of the GraphAutoModel.\n        name: String. The name of the AutoModel. Defaults to 'graph_auto_model'.\n        max_trials: Int. The maximum number of different Keras Models to try.\n            The search may finish before reaching the max_trials. Defaults to 100.\n        directory: String. The path to a directory for storing the search outputs.\n            Defaults to None, which would create a folder with the name of the\n            AutoModel in the current directory.\n        seed: Int. Random seed.\n    \"\"\"\n\n    def __init__(self,\n                 inputs,\n                 outputs,\n                 name='graph_auto_model',\n                 max_trials=100,\n                 directory=None,\n                 seed=None):\n        super().__init__(\n            inputs=inputs,\n            outputs=outputs,\n            name=name,\n            max_trials=max_trials,\n            directory=directory,\n            seed=seed\n        )\n        self.hypermodel = graph.GraphHyperModel(self.inputs, self.outputs)\n\n    def _meta_build(self, dataset):\n        pass\n", "path": "autokeras/auto_model.py"}], "after_files": [{"content": "import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.python.util import nest\n\nimport kerastuner\nfrom autokeras import meta_model\nfrom autokeras import tuner\nfrom autokeras import utils\nfrom autokeras.hypermodel import graph\nfrom autokeras.hypermodel import head\nfrom autokeras.hypermodel import node\n\n\nclass AutoModel(object):\n    \"\"\" A Model defined by inputs and outputs.\n\n    AutoModel combines a HyperModel and a Tuner to tune the HyperModel.\n    The user can use it in a similar way to a Keras model since it\n    also has `fit()` and  `predict()` methods.\n\n    The user can specify the inputs and outputs of the AutoModel. It will infer\n    the rest of the high-level neural architecture.\n\n    # Arguments\n        inputs: A list of or a HyperNode instance.\n            The input node(s) of the AutoModel.\n        outputs: A list of or a HyperHead instance.\n            The output head(s) of the AutoModel.\n        name: String. The name of the AutoModel. Defaults to 'auto_model'.\n        max_trials: Int. The maximum number of different Keras Models to try.\n            The search may finish before reaching the max_trials. Defaults to 100.\n        directory: String. The path to a directory for storing the search outputs.\n            Defaults to None, which would create a folder with the name of the\n            AutoModel in the current directory.\n        seed: Int. Random seed.\n    \"\"\"\n\n    def __init__(self,\n                 inputs,\n                 outputs,\n                 name='auto_model',\n                 max_trials=100,\n                 directory=None,\n                 seed=None):\n        self.inputs = nest.flatten(inputs)\n        self.outputs = nest.flatten(outputs)\n        self.name = name\n        self.tuner = None\n        self.max_trials = max_trials\n        self.directory = directory\n        self.seed = seed\n        self.hypermodel = None\n        if all([isinstance(output_node, head.Head)\n                for output_node in self.outputs]):\n            self.heads = self.outputs\n        else:\n            self.heads = [output_node.in_blocks[0] for output_node in self.outputs]\n\n    def _meta_build(self, dataset):\n        self.hypermodel = meta_model.assemble(inputs=self.inputs,\n                                              outputs=self.outputs,\n                                              dataset=dataset,\n                                              seed=self.seed)\n        self.outputs = self.hypermodel.outputs\n\n    def fit(self,\n            x=None,\n            y=None,\n            validation_split=0,\n            validation_data=None,\n            **kwargs):\n        \"\"\"Search for the best model and hyperparameters for the AutoModel.\n\n        It will search for the best model based on the performances on\n        validation data.\n\n        # Arguments\n            x: numpy.ndarray or tensorflow.Dataset. Training data x.\n            y: numpy.ndarray or tensorflow.Dataset. Training data y.\n            validation_split: Float between 0 and 1.\n                Fraction of the training data to be used as validation data.\n                The model will set apart this fraction of the training data,\n                will not train on it, and will evaluate\n                the loss and any model metrics\n                on this data at the end of each epoch.\n                The validation data is selected from the last samples\n                in the `x` and `y` data provided, before shuffling. This argument is\n                not supported when `x` is a dataset.\n            validation_data: Data on which to evaluate\n                the loss and any model metrics at the end of each epoch.\n                The model will not be trained on this data.\n                `validation_data` will override `validation_split`.\n                `validation_data` could be:\n                  - tuple `(x_val, y_val)` of Numpy arrays or tensors\n                  - tuple `(x_val, y_val, val_sample_weights)` of Numpy arrays\n                  - dataset or a dataset iterator\n                For the first two cases, `batch_size` must be provided.\n                For the last case, `validation_steps` must be provided.\n            **kwargs: Any arguments supported by keras.Model.fit.\n        \"\"\"\n        dataset, validation_data = self._prepare_data(\n            x=x,\n            y=y,\n            validation_data=validation_data,\n            validation_split=validation_split)\n\n        # Initialize the hypermodel.\n        self._meta_build(dataset)\n        self.hypermodel.set_io_shapes(dataset)\n\n        # Build the hypermodel in tuner init.\n        hp = kerastuner.HyperParameters()\n        self.hypermodel.hyper_build(hp)\n        self.hypermodel.preprocess(\n            hp=kerastuner.HyperParameters(),\n            dataset=dataset,\n            validation_data=validation_data,\n            fit=True)\n        self.tuner = tuner.RandomSearch(\n            hypermodel=self.hypermodel,\n            objective='val_loss',\n            max_trials=self.max_trials,\n            directory=self.directory,\n            seed=self.seed,\n            project_name=self.name)\n        self.hypermodel.clear_preprocessors()\n\n        # TODO: allow early stop if epochs is not specified.\n        self.tuner.search(x=dataset,\n                          validation_data=validation_data,\n                          **kwargs)\n\n    def _process_xy(self, x, y=None, fit=False, predict=False):\n        \"\"\"Convert x, y to tf.data.Dataset.\n\n        # Arguments\n            x: Any type allowed by the corresponding input node.\n            y: Any type allowed by the corresponding head.\n            fit: Boolean. Whether to fit the type converter with the provided data.\n            predict: Boolean. If it is called by the predict function of AutoModel.\n\n        # Returns\n            A tf.data.Dataset containing both x and y.\n        \"\"\"\n        if isinstance(x, tf.data.Dataset):\n            if y is None and not predict:\n                return x\n            if isinstance(y, tf.data.Dataset):\n                return tf.data.Dataset.zip((x, y))\n\n        x = nest.flatten(x)\n        new_x = []\n        for data, input_node in zip(x, self.inputs):\n            if fit:\n                input_node.fit(data)\n            data = input_node.transform(data)\n            new_x.append(data)\n        x = tf.data.Dataset.zip(tuple(new_x))\n\n        if predict:\n            return tf.data.Dataset.zip((x, x))\n\n        if not isinstance(y, tf.data.Dataset):\n            y = nest.flatten(y)\n            new_y = []\n            for data, head_block in zip(y, self.heads):\n                if fit:\n                    head_block.fit(data)\n                data = head_block.transform(data)\n                new_y.append(data)\n            y = tf.data.Dataset.zip(tuple(new_y))\n\n        return tf.data.Dataset.zip((x, y))\n\n    def _prepare_data(self, x, y, validation_data, validation_split):\n        \"\"\"Convert the data to tf.data.Dataset.\"\"\"\n        # Check validation information.\n        if not validation_data and not validation_split:\n            raise ValueError('Either validation_data or validation_split'\n                             'should be provided.')\n        # TODO: Handle other types of input, zip dataset, tensor, dict.\n        # Prepare the dataset.\n        dataset = self._process_xy(x, y, fit=True)\n        if validation_data:\n            val_x, val_y = validation_data\n            validation_data = self._process_xy(val_x, val_y)\n        # Split the data with validation_split.\n        if validation_data is None and validation_split:\n            dataset, validation_data = utils.split_dataset(dataset, validation_split)\n        return dataset, validation_data\n\n    def predict(self, x, batch_size=32, **kwargs):\n        \"\"\"Predict the output for a given testing data.\n\n        # Arguments\n            x: Any allowed types according to the input node. Testing data.\n            batch_size: Int. Defaults to 32.\n            **kwargs: Any arguments supported by keras.Model.predict.\n\n        # Returns\n            A list of numpy.ndarray objects or a single numpy.ndarray.\n            The predicted results.\n        \"\"\"\n        best_model, x = self._prepare_best_model_and_data(\n            x=x,\n            y=None,\n            batch_size=batch_size,\n            predict=True)\n        y = best_model.predict(x, **kwargs)\n        y = self._postprocess(y)\n        if isinstance(y, list) and len(y) == 1:\n            y = y[0]\n        return y\n\n    def _postprocess(self, y):\n        y = nest.flatten(y)\n        new_y = []\n        for temp_y, head_block in zip(y, self.heads):\n            if isinstance(head_block, head.Head):\n                temp_y = head_block.postprocess(temp_y)\n            new_y.append(temp_y)\n        return new_y\n\n    def evaluate(self, x, y=None, batch_size=32, **kwargs):\n        \"\"\"Evaluate the best model for the given data.\n\n        # Arguments\n            x: Any allowed types according to the input node. Testing data.\n            y: Any allowed types according to the head. Testing targets.\n                Defaults to None.\n            batch_size: Int. Defaults to 32.\n            **kwargs: Any arguments supported by keras.Model.evaluate.\n\n        # Returns\n            Scalar test loss (if the model has a single output and no metrics) or\n            list of scalars (if the model has multiple outputs and/or metrics).\n            The attribute model.metrics_names will give you the display labels for\n            the scalar outputs.\n        \"\"\"\n        best_model, data = self._prepare_best_model_and_data(\n            x=x,\n            y=y,\n            batch_size=batch_size)\n        return best_model.evaluate(data, **kwargs)\n\n    def _prepare_best_model_and_data(self, x, y, batch_size, predict=False):\n        best_model = self.tuner.get_best_models(1)[0]\n        best_trial = self.tuner.get_best_trials(1)[0]\n        best_hp = best_trial.hyperparameters\n\n        self.tuner.load_trial(best_trial)\n        x = self._process_xy(x, y, predict=predict)\n        x = self.hypermodel.preprocess(best_hp, x)\n        x = x.batch(batch_size)\n        return best_model, x\n\n\nclass GraphAutoModel(AutoModel):\n    \"\"\"A HyperModel defined by a graph of HyperBlocks.\n\n    GraphAutoModel is a subclass of HyperModel. Besides the HyperModel properties,\n    it also has a tuner to tune the HyperModel. The user can use it in a similar\n    way to a Keras model since it also has `fit()` and  `predict()` methods.\n\n    The user can specify the high-level neural architecture by connecting the\n    HyperBlocks with the functional API, which is the same as\n    the Keras functional API.\n\n    # Arguments\n        inputs: A list of or a HyperNode instances.\n            The input node(s) of the GraphAutoModel.\n        outputs: A list of or a HyperNode instances.\n            The output node(s) of the GraphAutoModel.\n        name: String. The name of the AutoModel. Defaults to 'graph_auto_model'.\n        max_trials: Int. The maximum number of different Keras Models to try.\n            The search may finish before reaching the max_trials. Defaults to 100.\n        directory: String. The path to a directory for storing the search outputs.\n            Defaults to None, which would create a folder with the name of the\n            AutoModel in the current directory.\n        seed: Int. Random seed.\n    \"\"\"\n\n    def __init__(self,\n                 inputs,\n                 outputs,\n                 name='graph_auto_model',\n                 max_trials=100,\n                 directory=None,\n                 seed=None):\n        super().__init__(\n            inputs=inputs,\n            outputs=outputs,\n            name=name,\n            max_trials=max_trials,\n            directory=directory,\n            seed=seed\n        )\n        self.hypermodel = graph.GraphHyperModel(self.inputs, self.outputs)\n\n    def _meta_build(self, dataset):\n        pass\n", "path": "autokeras/auto_model.py"}]}