{"id":3827,"date":"2020-09-01T19:00:46","date_gmt":"2020-09-01T19:00:46","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2020\/09\/01\/how-to-use-autokeras-for-classification-and-regression\/"},"modified":"2020-09-01T19:00:46","modified_gmt":"2020-09-01T19:00:46","slug":"how-to-use-autokeras-for-classification-and-regression","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2020\/09\/01\/how-to-use-autokeras-for-classification-and-regression\/","title":{"rendered":"How to Use AutoKeras for Classification and Regression"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>AutoML refers to techniques for automatically discovering the best-performing model for a given dataset.<\/p>\n<p>When applied to neural networks, this involves both discovering the model architecture and the hyperparameters used to train the model, generally referred to as <strong>neural architecture search<\/strong>.<\/p>\n<p>AutoKeras is an open-source library for performing AutoML for deep learning models. The search is performed using so-called Keras models via the TensorFlow tf.keras API.<\/p>\n<p>It provides a simple and effective approach for automatically finding top-performing models for a wide range of predictive modeling tasks, including tabular or so-called structured classification and regression datasets.<\/p>\n<p>In this tutorial, you will discover how to use AutoKeras to find good neural network models for classification and regression tasks.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>AutoKeras is an implementation of AutoML for deep learning that uses neural architecture search.<\/li>\n<li>How to use AutoKeras to find a top-performing model for a binary classification dataset.<\/li>\n<li>How to use AutoKeras to find a top-performing model for a regression dataset.<\/li>\n<\/ul>\n<p>Let&rsquo;s get started.<\/p>\n<div id=\"attachment_10450\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-10450\" loading=\"lazy\" class=\"size-full wp-image-10450\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/06\/How-to-Use-AutoKeras-for-Classification-and-Regression.jpg\" alt=\"How to Use AutoKeras for Classification and Regression\" width=\"800\" height=\"536\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/06\/How-to-Use-AutoKeras-for-Classification-and-Regression.jpg 800w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/06\/How-to-Use-AutoKeras-for-Classification-and-Regression-300x201.jpg 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/06\/How-to-Use-AutoKeras-for-Classification-and-Regression-768x515.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\"><\/p>\n<p id=\"caption-attachment-10450\" class=\"wp-caption-text\">How to Use AutoKeras for Classification and Regression<br \/>Photo by <a href=\"https:\/\/flickr.com\/photos\/kateure1309\/24972028303\/\">kanu101<\/a>, some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is divided into three parts; they are:<\/p>\n<ol>\n<li>AutoKeras for Deep Learning<\/li>\n<li>AutoKeras for Classification<\/li>\n<li>AutoKeras for Regression<\/li>\n<\/ol>\n<h2>AutoKeras for Deep Learning<\/h2>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Automated_machine_learning\">Automated Machine Learning<\/a>, or AutoML for short, refers to automatically finding the best combination of data preparation, model, and model hyperparameters for a predictive modeling problem.<\/p>\n<p>The benefit of AutoML is allowing machine learning practitioners to quickly and effectively address predictive modeling tasks with very little input, e.g. fire and forget.<\/p>\n<blockquote>\n<p>Automated Machine Learning (AutoML) has become a very important research topic with wide applications of machine learning techniques. The goal of AutoML is to enable people with limited machine learning background knowledge to use machine learning models easily.<\/p>\n<\/blockquote>\n<p>&mdash; <a href=\"https:\/\/www.kdd.org\/kdd2019\/accepted-papers\/view\/auto-keras-an-efficient-neural-architecture-search-system\">Auto-keras: An efficient neural architecture search system<\/a>, 2019.<\/p>\n<p>AutoKeras is an implementation of AutoML for deep learning models using the Keras API, specifically the <a href=\"https:\/\/machinelearningmastery.com\/tensorflow-tutorial-deep-learning-with-tf-keras\/\">tf.keras API provided by TensorFlow 2<\/a>.<\/p>\n<p>It uses a process of searching through neural network architectures to best address a modeling task, referred to more generally as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Neural_architecture_search\">Neural Architecture Search<\/a>, or NAS for short.<\/p>\n<blockquote>\n<p>&hellip; we have developed a widely adopted open-source AutoML system based on our proposed method, namely Auto-Keras. It is an open-source AutoML system, which can be downloaded and installed locally.<\/p>\n<\/blockquote>\n<p>&mdash; <a href=\"https:\/\/www.kdd.org\/kdd2019\/accepted-papers\/view\/auto-keras-an-efficient-neural-architecture-search-system\">Auto-keras: An efficient neural architecture search system<\/a>, 2019.<\/p>\n<p>In the spirit of Keras, AutoKeras provides an easy-to-use interface for different tasks, such as image classification, structured data classification or regression, and more. The user is only required to specify the location of the data and the number of models to try and is returned a model that achieves the best performance (under the configured constraints) on that dataset.<\/p>\n<p><strong>Note<\/strong>: AutoKeras provides a TensorFlow 2 Keras model (e.g. tf.keras) and not a Standalone Keras model. As such, the library assumes that you have Python 3 and TensorFlow 2.1 or higher installed.<\/p>\n<p>To install AutoKeras, you can use Pip, as follows:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">sudo pip install autokeras<\/pre>\n<p>You can confirm the installation was successful and check the version number as follows:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">sudo pip show autokeras<\/pre>\n<p>You should see output like the following:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">Name: autokeras\r\nVersion: 1.0.1\r\nSummary: AutoML for deep learning\r\nHome-page: http:\/\/autokeras.com\r\nAuthor: Data Analytics at Texas A&amp;M (DATA) Lab, Keras Team\r\nAuthor-email: jhfjhfj1@gmail.com\r\nLicense: MIT\r\nLocation: ...\r\nRequires: scikit-learn, packaging, pandas, keras-tuner, numpy\r\nRequired-by:<\/pre>\n<p>Once installed, you can then apply AutoKeras to find a good or great neural network model for your predictive modeling task.<\/p>\n<p>We will take a look at two common examples where you may want to use AutoKeras, classification and regression on tabular data, so-called structured data.<\/p>\n<h2>AutoKeras for Classification<\/h2>\n<p>AutoKeras can be used to discover a good or great model for classification tasks on tabular data.<\/p>\n<p>Recall tabular data are those datasets composed of rows and columns, such as a table or data as you would see in a spreadsheet.<\/p>\n<p>In this section, we will develop a model for the Sonar classification dataset for classifying sonar returns as rocks or mines. This dataset consists of 208 rows of data with 60 input features and a target class label of 0 (rock) or 1 (mine).<\/p>\n<p>A naive model can achieve a classification accuracy of about 53.4 percent via repeated 10-fold cross-validation, which provides a lower-bound. A good model can achieve an accuracy of about 88.2 percent, providing an upper-bound.<\/p>\n<p>You can learn more about the dataset here:<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/sonar.csv\">Sonar Dataset (sonar.csv)<\/a><\/li>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/sonar.names\">Sonar Dataset Description (sonar.names)<\/a><\/li>\n<\/ul>\n<p>No need to download the dataset; we will download it automatically as part of the example.<\/p>\n<p>First, we can download the dataset and split it into a randomly selected train and test set, holding 33 percent for test and using 67 percent for training.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># load the sonar dataset\r\nfrom pandas import read_csv\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.preprocessing import LabelEncoder\r\n# load dataset\r\nurl = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/sonar.csv'\r\ndataframe = read_csv(url, header=None)\r\nprint(dataframe.shape)\r\n# split into input and output elements\r\ndata = dataframe.values\r\nX, y = data[:, :-1], data[:, -1]\r\nprint(X.shape, y.shape)\r\n# basic data preparation\r\nX = X.astype('float32')\r\ny = LabelEncoder().fit_transform(y)\r\n# separate into train and test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\nprint(X_train.shape, X_test.shape, y_train.shape, y_test.shape)<\/pre>\n<p>Running the example first downloads the dataset and summarizes the shape, showing the expected number of rows and columns.<\/p>\n<p>The dataset is then split into input and output elements, then these elements are further split into train and test datasets.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(208, 61)\r\n(208, 60) (208,)\r\n(139, 60) (69, 60) (139,) (69,)<\/pre>\n<p>We can use AutoKeras to automatically discover an effective neural network model for this dataset.<\/p>\n<p>This can be achieved by using the <a href=\"https:\/\/autokeras.com\/structured_data_classifier\/\">StructuredDataClassifier<\/a> class and specifying the number of models to search. This defines the search to perform.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the search\r\nsearch = StructuredDataClassifier(max_trials=15)<\/pre>\n<p>We can then execute the search using our loaded dataset.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# perform the search\r\nsearch.fit(x=X_train, y=y_train, verbose=0)<\/pre>\n<p>This may take a few minutes and will report the progress of the search.<\/p>\n<p>Next, we can evaluate the model on the test dataset to see how it performs on new data.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# evaluate the model\r\nloss, acc = search.evaluate(X_test, y_test, verbose=0)\r\nprint('Accuracy: %.3f' % acc)<\/pre>\n<p>We then use the model to make a prediction for a new row of data.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# use the model to make a prediction\r\nrow = [0.0200,0.0371,0.0428,0.0207,0.0954,0.0986,0.1539,0.1601,0.3109,0.2111,0.1609,0.1582,0.2238,0.0645,0.0660,0.2273,0.3100,0.2999,0.5078,0.4797,0.5783,0.5071,0.4328,0.5550,0.6711,0.6415,0.7104,0.8080,0.6791,0.3857,0.1307,0.2604,0.5121,0.7547,0.8537,0.8507,0.6692,0.6097,0.4943,0.2744,0.0510,0.2834,0.2825,0.4256,0.2641,0.1386,0.1051,0.1343,0.0383,0.0324,0.0232,0.0027,0.0065,0.0159,0.0072,0.0167,0.0180,0.0084,0.0090,0.0032]\r\nX_new = asarray([row]).astype('float32')\r\nyhat = search.predict(X_new)\r\nprint('Predicted: %.3f' % yhat[0])<\/pre>\n<p>We can retrieve the final model, which is an instance of a TensorFlow Keras model.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# get the best performing model\r\nmodel = search.export_model()<\/pre>\n<p>We can then summarize the structure of the model to see what was selected.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# summarize the loaded model\r\nmodel.summary()<\/pre>\n<p>Finally, we can save the model to file for later use, which can be loaded using the TensorFlow <a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/models\/load_model\">load_model() function<\/a>.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# save the best performing model to file\r\nmodel.save('model_sonar.h5')<\/pre>\n<p>Tying this together, the complete example of applying AutoKeras to find an effective neural network model for the Sonar dataset is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># use autokeras to find a model for the sonar dataset\r\nfrom numpy import asarray\r\nfrom pandas import read_csv\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.preprocessing import LabelEncoder\r\nfrom autokeras import StructuredDataClassifier\r\n# load dataset\r\nurl = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/sonar.csv'\r\ndataframe = read_csv(url, header=None)\r\nprint(dataframe.shape)\r\n# split into input and output elements\r\ndata = dataframe.values\r\nX, y = data[:, :-1], data[:, -1]\r\nprint(X.shape, y.shape)\r\n# basic data preparation\r\nX = X.astype('float32')\r\ny = LabelEncoder().fit_transform(y)\r\n# separate into train and test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\nprint(X_train.shape, X_test.shape, y_train.shape, y_test.shape)\r\n# define the search\r\nsearch = StructuredDataClassifier(max_trials=15)\r\n# perform the search\r\nsearch.fit(x=X_train, y=y_train, verbose=0)\r\n# evaluate the model\r\nloss, acc = search.evaluate(X_test, y_test, verbose=0)\r\nprint('Accuracy: %.3f' % acc)\r\n# use the model to make a prediction\r\nrow = [0.0200,0.0371,0.0428,0.0207,0.0954,0.0986,0.1539,0.1601,0.3109,0.2111,0.1609,0.1582,0.2238,0.0645,0.0660,0.2273,0.3100,0.2999,0.5078,0.4797,0.5783,0.5071,0.4328,0.5550,0.6711,0.6415,0.7104,0.8080,0.6791,0.3857,0.1307,0.2604,0.5121,0.7547,0.8537,0.8507,0.6692,0.6097,0.4943,0.2744,0.0510,0.2834,0.2825,0.4256,0.2641,0.1386,0.1051,0.1343,0.0383,0.0324,0.0232,0.0027,0.0065,0.0159,0.0072,0.0167,0.0180,0.0084,0.0090,0.0032]\r\nX_new = asarray([row]).astype('float32')\r\nyhat = search.predict(X_new)\r\nprint('Predicted: %.3f' % yhat[0])\r\n# get the best performing model\r\nmodel = search.export_model()\r\n# summarize the loaded model\r\nmodel.summary()\r\n# save the best performing model to file\r\nmodel.save('model_sonar.h5')<\/pre>\n<p>Running the example will report a lot of debug information about the progress of the search.<\/p>\n<p>The models and results are all saved in a folder called &ldquo;<em>structured_data_classifier<\/em>&rdquo; in your current working directory.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n[Trial complete]\r\n[Trial summary]\r\n |-Trial ID: e8265ad768619fc3b69a85b026f70db6\r\n |-Score: 0.9259259104728699\r\n |-Best step: 0\r\n &gt; Hyperparameters:\r\n |-classification_head_1\/dropout_rate: 0\r\n |-optimizer: adam\r\n |-structured_data_block_1\/dense_block_1\/dropout_rate: 0.0\r\n |-structured_data_block_1\/dense_block_1\/num_layers: 2\r\n |-structured_data_block_1\/dense_block_1\/units_0: 32\r\n |-structured_data_block_1\/dense_block_1\/units_1: 16\r\n |-structured_data_block_1\/dense_block_1\/units_2: 512\r\n |-structured_data_block_1\/dense_block_1\/use_batchnorm: False\r\n |-structured_data_block_1\/dense_block_2\/dropout_rate: 0.25\r\n |-structured_data_block_1\/dense_block_2\/num_layers: 3\r\n |-structured_data_block_1\/dense_block_2\/units_0: 32\r\n |-structured_data_block_1\/dense_block_2\/units_1: 16\r\n |-structured_data_block_1\/dense_block_2\/units_2: 16\r\n |-structured_data_block_1\/dense_block_2\/use_batchnorm: False<\/pre>\n<p>The best-performing model is then evaluated on the hold-out test dataset.<\/p>\n<p><strong>Note<\/strong>: Your <a href=\"https:\/\/machinelearningmastery.com\/different-results-each-time-in-machine-learning\/\">results may vary<\/a> given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.<\/p>\n<p>In this case, we can see that the model achieved a classification accuracy of about 82.6 percent.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">Accuracy: 0.826<\/pre>\n<p>Next, the architecture of the best-performing model is reported.<\/p>\n<p>We can see a model with two hidden layers with dropout and ReLU activation.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">Model: \"model\"\r\n_________________________________________________________________\r\nLayer (type)                 Output Shape              Param #\r\n=================================================================\r\ninput_1 (InputLayer)         [(None, 60)]              0\r\n_________________________________________________________________\r\ncategorical_encoding (Catego (None, 60)                0\r\n_________________________________________________________________\r\ndense (Dense)                (None, 256)               15616\r\n_________________________________________________________________\r\nre_lu (ReLU)                 (None, 256)               0\r\n_________________________________________________________________\r\ndropout (Dropout)            (None, 256)               0\r\n_________________________________________________________________\r\ndense_1 (Dense)              (None, 512)               131584\r\n_________________________________________________________________\r\nre_lu_1 (ReLU)               (None, 512)               0\r\n_________________________________________________________________\r\ndropout_1 (Dropout)          (None, 512)               0\r\n_________________________________________________________________\r\ndense_2 (Dense)              (None, 1)                 513\r\n_________________________________________________________________\r\nclassification_head_1 (Sigmo (None, 1)                 0\r\n=================================================================\r\nTotal params: 147,713\r\nTrainable params: 147,713\r\nNon-trainable params: 0\r\n_________________________________________________________________<\/pre>\n<\/p>\n<h2>AutoKeras for Regression<\/h2>\n<p>AutoKeras can also be used for regression tasks, that is, predictive modeling problems where a numeric value is predicted.<\/p>\n<p>We will use the auto insurance dataset that involves predicting the total payment from claims given the total number of claims. The dataset has 63 rows and one input and one output variable.<\/p>\n<p>A naive model can achieve a mean absolute error (MAE) of about 66 using repeated 10-fold cross-validation, providing a lower-bound on expected performance. A good model can achieve a MAE of about 28, providing a performance upper-bound.<\/p>\n<p>You can learn more about this dataset here:<\/p>\n<ul>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/auto-insurance.csv\">Auto Insurance Dataset (auto-insurance.csv)<\/a><\/li>\n<li><a href=\"https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/auto-insurance.names\">Auto Insurance Dataset (auto-insurance.names)<\/a><\/li>\n<\/ul>\n<p>We can load the dataset and split it into input and output elements and then train and test datasets.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># load the sonar dataset\r\nfrom pandas import read_csv\r\nfrom sklearn.model_selection import train_test_split\r\n# load dataset\r\nurl = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/auto-insurance.csv'\r\ndataframe = read_csv(url, header=None)\r\nprint(dataframe.shape)\r\n# split into input and output elements\r\ndata = dataframe.values\r\ndata = data.astype('float32')\r\nX, y = data[:, :-1], data[:, -1]\r\nprint(X.shape, y.shape)\r\n# separate into train and test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\nprint(X_train.shape, X_test.shape, y_train.shape, y_test.shape)<\/pre>\n<p>Running the example loads the dataset, confirming the number of rows and columns, then splits the dataset into train and test sets.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(63, 2)\r\n(63, 1) (63,)\r\n(42, 1) (21, 1) (42,) (21,)<\/pre>\n<p>AutoKeras can be applied to a regression task using the <a href=\"https:\/\/autokeras.com\/structured_data_regressor\/\">StructuredDataRegressor<\/a> class and configured for the number of models to trial.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the search\r\nsearch = StructuredDataRegressor(max_trials=15, loss='mean_absolute_error')<\/pre>\n<p>The search can then be run and the best model saved, much like in the classification case.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define the search\r\nsearch = StructuredDataRegressor(max_trials=15, loss='mean_absolute_error')\r\n# perform the search\r\nsearch.fit(x=X_train, y=y_train, verbose=0)<\/pre>\n<p>We can then use the best-performing model and evaluate it on the hold out dataset, make a prediction on new data, and summarize its structure.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# evaluate the model\r\nmae, _ = search.evaluate(X_test, y_test, verbose=0)\r\nprint('MAE: %.3f' % mae)\r\n# use the model to make a prediction\r\nX_new = asarray([[108]]).astype('float32')\r\nyhat = search.predict(X_new)\r\nprint('Predicted: %.3f' % yhat[0])\r\n# get the best performing model\r\nmodel = search.export_model()\r\n# summarize the loaded model\r\nmodel.summary()\r\n# save the best performing model to file\r\nmodel.save('model_insurance.h5')<\/pre>\n<p>Tying this together, the complete example of using AutoKeras to discover an effective neural network model for the auto insurance dataset is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># use autokeras to find a model for the insurance dataset\r\nfrom numpy import asarray\r\nfrom pandas import read_csv\r\nfrom sklearn.model_selection import train_test_split\r\nfrom autokeras import StructuredDataRegressor\r\n# load dataset\r\nurl = 'https:\/\/raw.githubusercontent.com\/jbrownlee\/Datasets\/master\/auto-insurance.csv'\r\ndataframe = read_csv(url, header=None)\r\nprint(dataframe.shape)\r\n# split into input and output elements\r\ndata = dataframe.values\r\ndata = data.astype('float32')\r\nX, y = data[:, :-1], data[:, -1]\r\nprint(X.shape, y.shape)\r\n# separate into train and test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\nprint(X_train.shape, X_test.shape, y_train.shape, y_test.shape)\r\n# define the search\r\nsearch = StructuredDataRegressor(max_trials=15, loss='mean_absolute_error')\r\n# perform the search\r\nsearch.fit(x=X_train, y=y_train, verbose=0)\r\n# evaluate the model\r\nmae, _ = search.evaluate(X_test, y_test, verbose=0)\r\nprint('MAE: %.3f' % mae)\r\n# use the model to make a prediction\r\nX_new = asarray([[108]]).astype('float32')\r\nyhat = search.predict(X_new)\r\nprint('Predicted: %.3f' % yhat[0])\r\n# get the best performing model\r\nmodel = search.export_model()\r\n# summarize the loaded model\r\nmodel.summary()\r\n# save the best performing model to file\r\nmodel.save('model_insurance.h5')<\/pre>\n<p>Running the example will report a lot of debug information about the progress of the search.<\/p>\n<p>The models and results are all saved in a folder called &ldquo;<em>structured_data_regressor<\/em>&rdquo; in your current working directory.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n[Trial summary]\r\n|-Trial ID: ea28b767d13e958c3ace7e54e7cb5a14\r\n|-Score: 108.62509155273438\r\n|-Best step: 0\r\n&gt; Hyperparameters:\r\n|-optimizer: adam\r\n|-regression_head_1\/dropout_rate: 0\r\n|-structured_data_block_1\/dense_block_1\/dropout_rate: 0.0\r\n|-structured_data_block_1\/dense_block_1\/num_layers: 2\r\n|-structured_data_block_1\/dense_block_1\/units_0: 16\r\n|-structured_data_block_1\/dense_block_1\/units_1: 1024\r\n|-structured_data_block_1\/dense_block_1\/units_2: 128\r\n|-structured_data_block_1\/dense_block_1\/use_batchnorm: True\r\n|-structured_data_block_1\/dense_block_2\/dropout_rate: 0.5\r\n|-structured_data_block_1\/dense_block_2\/num_layers: 2\r\n|-structured_data_block_1\/dense_block_2\/units_0: 256\r\n|-structured_data_block_1\/dense_block_2\/units_1: 64\r\n|-structured_data_block_1\/dense_block_2\/units_2: 1024\r\n|-structured_data_block_1\/dense_block_2\/use_batchnorm: True<\/pre>\n<p>The best-performing model is then evaluated on the hold-out test dataset.<\/p>\n<p><strong>Note<\/strong>: Your <a href=\"https:\/\/machinelearningmastery.com\/different-results-each-time-in-machine-learning\/\">results may vary<\/a> given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.<\/p>\n<p>In this case, we can see that the model achieved a MAE of about 24.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">MAE: 24.916<\/pre>\n<p>Next, the architecture of the best-performing model is reported.<\/p>\n<p>We can see a model with two hidden layers with ReLU activation.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">Model: \"model\"\r\n_________________________________________________________________\r\nLayer (type)                 Output Shape              Param #\r\n=================================================================\r\ninput_1 (InputLayer)         [(None, 1)]               0\r\n_________________________________________________________________\r\ncategorical_encoding (Catego (None, 1)                 0\r\n_________________________________________________________________\r\ndense (Dense)                (None, 64)                128\r\n_________________________________________________________________\r\nre_lu (ReLU)                 (None, 64)                0\r\n_________________________________________________________________\r\ndense_1 (Dense)              (None, 512)               33280\r\n_________________________________________________________________\r\nre_lu_1 (ReLU)               (None, 512)               0\r\n_________________________________________________________________\r\ndense_2 (Dense)              (None, 128)               65664\r\n_________________________________________________________________\r\nre_lu_2 (ReLU)               (None, 128)               0\r\n_________________________________________________________________\r\nregression_head_1 (Dense)    (None, 1)                 129\r\n=================================================================\r\nTotal params: 99,201\r\nTrainable params: 99,201\r\nNon-trainable params: 0\r\n_________________________________________________________________<\/pre>\n<\/p>\n<h2>Further Reading<\/h2>\n<p>This section provides more resources on the topic if you are looking to go deeper.<\/p>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Automated_machine_learning\">Automated machine learning, Wikipedia<\/a>.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Neural_architecture_search\">Neural architecture search, Wikipedia<\/a>.<\/li>\n<li><a href=\"https:\/\/autokeras.com\/\">AutoKeras Homepage<\/a>.<\/li>\n<li><a href=\"https:\/\/github.com\/keras-team\/autokeras\">AutoKeras GitHub Project<\/a>.<\/li>\n<li><a href=\"https:\/\/www.kdd.org\/kdd2019\/accepted-papers\/view\/auto-keras-an-efficient-neural-architecture-search-system\">Auto-keras: An efficient neural architecture search system<\/a>, 2019.<\/li>\n<li><a href=\"https:\/\/machinelearningmastery.com\/results-for-standard-classification-and-regression-machine-learning-datasets\/\">Results for Standard Classification and Regression Machine Learning Datasets<\/a><\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to use AutoKeras to find good neural network models for classification and regression tasks.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>AutoKeras is an implementation of AutoML for deep learning that uses neural architecture search.<\/li>\n<li>How to use AutoKeras to find a top-performing model for a binary classification dataset.<\/li>\n<li>How to use AutoKeras to find a top-performing model for a regression dataset.<\/li>\n<\/ul>\n<p><strong>Do you have any questions?<\/strong><br \/>\nAsk your questions in the comments below and I will do my best to answer.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/autokeras-for-classification-and-regression\/\">How to Use AutoKeras for Classification and Regression<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/autokeras-for-classification-and-regression\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee AutoML refers to techniques for automatically discovering the best-performing model for a given dataset. When applied to neural networks, this involves both [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2020\/09\/01\/how-to-use-autokeras-for-classification-and-regression\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":3828,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3827"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=3827"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3827\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/3828"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=3827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=3827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=3827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}