{"id":4177,"date":"2020-12-08T18:00:02","date_gmt":"2020-12-08T18:00:02","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2020\/12\/08\/autoencoder-feature-extraction-for-regression\/"},"modified":"2020-12-08T18:00:02","modified_gmt":"2020-12-08T18:00:02","slug":"autoencoder-feature-extraction-for-regression","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2020\/12\/08\/autoencoder-feature-extraction-for-regression\/","title":{"rendered":"Autoencoder Feature Extraction for Regression"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>Autoencoder is a type of neural network that can be used to learn a compressed representation of raw data.<\/p>\n<p>An autoencoder is composed of encoder and a decoder sub-models. The encoder compresses the input and the decoder attempts to recreate the input from the compressed version provided by the encoder. After training, the encoder model is saved and the decoder is discarded.<\/p>\n<p>The encoder can then be used as a data preparation technique to perform feature extraction on raw data that can be used to train a different machine learning model.<\/p>\n<p>In this tutorial, you will discover how to develop and evaluate an autoencoder for regression predictive<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>An autoencoder is a neural network model that can be used to learn a compressed representation of raw data.<\/li>\n<li>How to train an autoencoder model on a training dataset and save just the encoder part of the model.<\/li>\n<li>How to use the encoder as a data preparation step when training a machine learning model.<\/li>\n<\/ul>\n<p>Let&rsquo;s get started.<\/p>\n<div id=\"attachment_11982\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-11982\" loading=\"lazy\" class=\"size-full wp-image-11982\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2021\/03\/Autoencoder-Feature-Extraction-for-Regression.jpg\" alt=\"Autoencoder Feature Extraction for Regression\" width=\"800\" height=\"580\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2021\/03\/Autoencoder-Feature-Extraction-for-Regression.jpg 800w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2021\/03\/Autoencoder-Feature-Extraction-for-Regression-300x218.jpg 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2021\/03\/Autoencoder-Feature-Extraction-for-Regression-768x557.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\"><\/p>\n<p id=\"caption-attachment-11982\" class=\"wp-caption-text\">Autoencoder Feature Extraction for Regression<br \/>Photo by <a href=\"https:\/\/www.flickr.com\/photos\/simonmatzinger\/16693660849\/\">Simon Matzinger<\/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>Autoencoders for Feature Extraction<\/li>\n<li>Autoencoder for Regression<\/li>\n<li>Autoencoder as Data Preparation<\/li>\n<\/ol>\n<h2>Autoencoders for Feature Extraction<\/h2>\n<p>An <a href=\"https:\/\/en.wikipedia.org\/wiki\/Autoencoder\">autoencoder<\/a> is a neural network model that seeks to learn a compressed representation of an input.<\/p>\n<blockquote>\n<p>An autoencoder is a neural network that is trained to attempt to copy its input to its output.<\/p>\n<\/blockquote>\n<p>&mdash; Page 502, <a href=\"https:\/\/amzn.to\/3kV7gdV\">Deep Learning<\/a>, 2016.<\/p>\n<p>They are an unsupervised learning method, although technically, they are trained using supervised learning methods, referred to as self-supervised. They are typically trained as part of a broader model that attempts to recreate the input.<\/p>\n<p>For example:<\/p>\n<ul>\n<li>X = model.predict(X)<\/li>\n<\/ul>\n<p>The design of the autoencoder model purposefully makes this challenging by restricting the architecture to a bottleneck at the midpoint of the model, from which the reconstruction of the input data is performed.<\/p>\n<p>There are many types of autoencoders, and their use varies, but perhaps the more common use is as a learned or automatic feature extraction model.<\/p>\n<p>In this case, once the model is fit, the reconstruction aspect of the model can be discarded and the model up to the point of the bottleneck can be used. The output of the model at the bottleneck is a fixed length vector that provides a compressed representation of the input data.<\/p>\n<blockquote>\n<p>Usually they are restricted in ways that allow them to copy only approximately, and to copy only input that resembles the training data. Because the model is forced to prioritize which aspects of the input should be copied, it often learns useful properties of the data.<\/p>\n<\/blockquote>\n<p>&mdash; Page 502, <a href=\"https:\/\/amzn.to\/3kV7gdV\">Deep Learning<\/a>, 2016.<\/p>\n<p>Input data from the domain can then be provided to the model and the output of the model at the bottleneck can be used as a feature vector in a supervised learning model, for visualization, or more generally for dimensionality reduction.<\/p>\n<p>Next, let&rsquo;s explore how we might develop an autoencoder for feature extraction on a regression predictive modeling problem.<\/p>\n<h2>Autoencoder for Regression<\/h2>\n<p>In this section, we will develop an autoencoder to learn a compressed representation of the input features for a regression predictive modeling problem.<\/p>\n<p>First, let&rsquo;s define a regression predictive modeling problem.<\/p>\n<p>We will use the <a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.datasets.make_regression.html\">make_regression() scikit-learn function<\/a> to define a synthetic regression task with 100 input features (columns) and 1,000 examples (rows). Importantly, we will define the problem in such a way that most of the input variables are redundant (90 of the 100 or 90 percent), allowing the autoencoder later to learn a useful compressed representation.<\/p>\n<p>The example below defines the dataset and summarizes its shape.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># synthetic regression dataset\r\nfrom sklearn.datasets import make_regression\r\n# define dataset\r\nX, y = make_regression(n_samples=1000, n_features=100, n_informative=10, noise=0.1, random_state=1)\r\n# summarize the dataset\r\nprint(X.shape, y.shape)<\/pre>\n<p>Running the example defines the dataset and prints the shape of the arrays, confirming the number of rows and columns.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(1000, 100) (1000,)<\/pre>\n<p>Next, we will develop a Multilayer Perceptron (MLP) autoencoder model.<\/p>\n<p>The model will take all of the input columns, then output the same values. It will learn to recreate the input pattern exactly.<\/p>\n<p>The autoencoder consists of two parts: the encoder and the decoder. The encoder learns how to interpret the input and compress it to an internal representation defined by the bottleneck layer. The decoder takes the output of the encoder (the bottleneck layer) and attempts to recreate the input.<\/p>\n<p>Once the autoencoder is trained, the decode is discarded and we only keep the encoder and use it to compress examples of input to vectors output by the bottleneck layer.<\/p>\n<p>In this first autoencoder, we won&rsquo;t compress the input at all and will use a bottleneck layer the same size as the input. This should be an easy problem that the model will learn nearly perfectly and is intended to confirm our model is implemented correctly.<\/p>\n<p>We will define the model using the functional API. If this is new to you, I recommend this tutorial:<\/p>\n<ul>\n<li><a href=\"https:\/\/machinelearningmastery.com\/keras-functional-api-deep-learning\/\">How to Use the Keras Functional API for Deep Learning<\/a><\/li>\n<\/ul>\n<p>Prior to defining and fitting the model, we will split the data into train and test sets and scale the input data by normalizing the values to the range 0-1, a good practice with MLPs.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# split into train test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\n# scale data\r\nt = MinMaxScaler()\r\nt.fit(X_train)\r\nX_train = t.transform(X_train)\r\nX_test = t.transform(X_test)<\/pre>\n<p>We will define the encoder to have one hidden layer with the same number of nodes as there are in the input data with batch normalization and <a href=\"https:\/\/machinelearningmastery.com\/rectified-linear-activation-function-for-deep-learning-neural-networks\/\">ReLU activation<\/a>.<\/p>\n<p>This is followed by a bottleneck layer with the same number of nodes as columns in the input data, e.g. no compression.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define encoder\r\nvisible = Input(shape=(n_inputs,))\r\ne = Dense(n_inputs*2)(visible)\r\ne = BatchNormalization()(e)\r\ne = ReLU()(e)\r\n# define bottleneck\r\nn_bottleneck = n_inputs\r\nbottleneck = Dense(n_bottleneck)(e)<\/pre>\n<p>The decoder will be defined with the same structure.<\/p>\n<p>It will have one hidden layer with batch normalization and ReLU activation. The output layer will have the same number of nodes as there are columns in the input data and will use a linear activation function to output numeric values.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define decoder\r\nd = Dense(n_inputs*2)(bottleneck)\r\nd = BatchNormalization()(d)\r\nd = ReLU()(d)\r\n# output layer\r\noutput = Dense(n_inputs, activation='linear')(d)\r\n# define autoencoder model\r\nmodel = Model(inputs=visible, outputs=output)\r\n# compile autoencoder model\r\nmodel.compile(optimizer='adam', loss='mse')<\/pre>\n<p>The model will be fit using the efficient Adam version of stochastic gradient descent and minimizes the mean squared error, given that reconstruction is a type of multi-output regression problem.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# compile autoencoder model\r\nmodel.compile(optimizer='adam', loss='mse')<\/pre>\n<p>We can plot the layers in the autoencoder model to get a feeling for how the data flows through the model.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# plot the autoencoder\r\nplot_model(model, 'autoencoder.png', show_shapes=True)<\/pre>\n<p>The image below shows a plot of the autoencoder.<\/p>\n<div id=\"attachment_11978\" style=\"width: 530px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-11978\" loading=\"lazy\" class=\"wp-image-11978 size-full\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/12\/Plot-of-Autoencoder-Model-for-Regression-with-No-Compression.png\" alt=\"Plot of the Autoencoder Model for Regression\" width=\"520\" height=\"959\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Plot-of-Autoencoder-Model-for-Regression-with-No-Compression.png 520w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Plot-of-Autoencoder-Model-for-Regression-with-No-Compression-163x300.png 163w\" sizes=\"(max-width: 520px) 100vw, 520px\"><\/p>\n<p id=\"caption-attachment-11978\" class=\"wp-caption-text\">Plot of the Autoencoder Model for Regression<\/p>\n<\/div>\n<p>Next, we can train the model to reproduce the input and keep track of the performance of the model on the holdout test set. The model is trained for 400 epochs and a batch size of 16 examples.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# fit the autoencoder model to reconstruct input\r\nhistory = model.fit(X_train, X_train, epochs=400, batch_size=16, verbose=2, validation_data=(X_test,X_test))<\/pre>\n<p>After training, we can plot the learning curves for the train and test sets to confirm the model learned the reconstruction problem well.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# plot loss\r\npyplot.plot(history.history['loss'], label='train')\r\npyplot.plot(history.history['val_loss'], label='test')\r\npyplot.legend()\r\npyplot.show()<\/pre>\n<p>Finally, we can save the encoder model for use later, if desired.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define an encoder model (without the decoder)\r\nencoder = Model(inputs=visible, outputs=bottleneck)\r\nplot_model(encoder, 'encoder.png', show_shapes=True)\r\n# save the encoder to file\r\nencoder.save('encoder.h5')<\/pre>\n<p>As part of saving the encoder, we will also plot the model to get a feeling for the shape of the output of the bottleneck layer, e.g. a 100-element vector.<\/p>\n<p>An example of this plot is provided below.<\/p>\n<div id=\"attachment_11979\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-11979\" loading=\"lazy\" class=\"size-full wp-image-11979\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/12\/Plot-of-Encoder-Model-for-Regression-with-No-Compression.png\" alt=\"Plot of Encoder Model for Regression With No Compression\" width=\"501\" height=\"516\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Plot-of-Encoder-Model-for-Regression-with-No-Compression.png 501w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Plot-of-Encoder-Model-for-Regression-with-No-Compression-291x300.png 291w\" sizes=\"(max-width: 501px) 100vw, 501px\"><\/p>\n<p id=\"caption-attachment-11979\" class=\"wp-caption-text\">Plot of Encoder Model for Regression With No Compression<\/p>\n<\/div>\n<p>Tying this all together, the complete example of an autoencoder for reconstructing the input data for a regression dataset without any compression in the bottleneck layer is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># train autoencoder for regression with no compression in the bottleneck layer\r\nfrom sklearn.datasets import make_regression\r\nfrom sklearn.preprocessing import MinMaxScaler\r\nfrom sklearn.model_selection import train_test_split\r\nfrom tensorflow.keras.models import Model\r\nfrom tensorflow.keras.layers import Input\r\nfrom tensorflow.keras.layers import Dense\r\nfrom tensorflow.keras.layers import ReLU\r\nfrom tensorflow.keras.layers import BatchNormalization\r\nfrom tensorflow.keras.utils import plot_model\r\nfrom matplotlib import pyplot\r\n# define dataset\r\nX, y = make_regression(n_samples=1000, n_features=100, n_informative=10, noise=0.1, random_state=1)\r\n# number of input columns\r\nn_inputs = X.shape[1]\r\n# split into train test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\n# scale data\r\nt = MinMaxScaler()\r\nt.fit(X_train)\r\nX_train = t.transform(X_train)\r\nX_test = t.transform(X_test)\r\n# define encoder\r\nvisible = Input(shape=(n_inputs,))\r\ne = Dense(n_inputs*2)(visible)\r\ne = BatchNormalization()(e)\r\ne = ReLU()(e)\r\n# define bottleneck\r\nn_bottleneck = n_inputs\r\nbottleneck = Dense(n_bottleneck)(e)\r\n# define decoder\r\nd = Dense(n_inputs*2)(bottleneck)\r\nd = BatchNormalization()(d)\r\nd = ReLU()(d)\r\n# output layer\r\noutput = Dense(n_inputs, activation='linear')(d)\r\n# define autoencoder model\r\nmodel = Model(inputs=visible, outputs=output)\r\n# compile autoencoder model\r\nmodel.compile(optimizer='adam', loss='mse')\r\n# plot the autoencoder\r\nplot_model(model, 'autoencoder.png', show_shapes=True)\r\n# fit the autoencoder model to reconstruct input\r\nhistory = model.fit(X_train, X_train, epochs=400, batch_size=16, verbose=2, validation_data=(X_test,X_test))\r\n# plot loss\r\npyplot.plot(history.history['loss'], label='train')\r\npyplot.plot(history.history['val_loss'], label='test')\r\npyplot.legend()\r\npyplot.show()\r\n# define an encoder model (without the decoder)\r\nencoder = Model(inputs=visible, outputs=bottleneck)\r\nplot_model(encoder, 'encoder.png', show_shapes=True)\r\n# save the encoder to file\r\nencoder.save('encoder.h5')<\/pre>\n<p>Running the example fits the model and reports loss on the train and test sets along the way.<\/p>\n<p><strong>Note<\/strong>: if you have problems creating the plots of the model, you can comment out the import and call the <em>plot_model()<\/em> function.<\/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 see that loss gets low but does not go to zero (as we might have expected) with no compression in the bottleneck layer. Perhaps further tuning the model architecture or learning hyperparameters is required.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\nEpoch 393\/400\r\n42\/42 - 0s - loss: 0.0025 - val_loss: 0.0024\r\nEpoch 394\/400\r\n42\/42 - 0s - loss: 0.0025 - val_loss: 0.0021\r\nEpoch 395\/400\r\n42\/42 - 0s - loss: 0.0023 - val_loss: 0.0021\r\nEpoch 396\/400\r\n42\/42 - 0s - loss: 0.0025 - val_loss: 0.0023\r\nEpoch 397\/400\r\n42\/42 - 0s - loss: 0.0024 - val_loss: 0.0022\r\nEpoch 398\/400\r\n42\/42 - 0s - loss: 0.0025 - val_loss: 0.0021\r\nEpoch 399\/400\r\n42\/42 - 0s - loss: 0.0026 - val_loss: 0.0022\r\nEpoch 400\/400\r\n42\/42 - 0s - loss: 0.0025 - val_loss: 0.0024<\/pre>\n<p>A plot of the learning curves is created showing that the model achieves a good fit in reconstructing the input, which holds steady throughout training, not overfitting.<\/p>\n<div id=\"attachment_11980\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-11980\" loading=\"lazy\" class=\"size-full wp-image-11980\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2020\/12\/Learning-Curves-of-Training-the-Autoencoder-Model-for-Regression-without-Compression.png\" alt=\"Learning Curves of Training the Autoencoder Model for Regression Without Compression\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Learning-Curves-of-Training-the-Autoencoder-Model-for-Regression-without-Compression.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Learning-Curves-of-Training-the-Autoencoder-Model-for-Regression-without-Compression-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Learning-Curves-of-Training-the-Autoencoder-Model-for-Regression-without-Compression-1024x768.png 1024w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2020\/12\/Learning-Curves-of-Training-the-Autoencoder-Model-for-Regression-without-Compression-768x576.png 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-11980\" class=\"wp-caption-text\">Learning Curves of Training the Autoencoder Model for Regression Without Compression<\/p>\n<\/div>\n<p>So far, so good. We know how to develop an autoencoder without compression.<\/p>\n<p>The trained encoder is saved to the file &ldquo;<em>encoder.h5<\/em>&rdquo; that we can load and use later.<\/p>\n<p>Next, let&rsquo;s explore how we might use the trained encoder model.<\/p>\n<h2>Autoencoder as Data Preparation<\/h2>\n<p>In this section, we will use the trained encoder model from the autoencoder model to compress input data and train a different predictive model.<\/p>\n<p>First, let&rsquo;s establish a baseline in performance on this problem. This is important as if the performance of a model is not improved by the compressed encoding, then the compressed encoding does not add value to the project and should not be used.<\/p>\n<p>We can train a support vector regression (SVR) model on the training dataset directly and evaluate the performance of the model on the holdout test set.<\/p>\n<p>As is good practice, we will scale both the input variables and target variable prior to fitting and evaluating the model.<\/p>\n<p>The complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># baseline in performance with support vector regression model\r\nfrom sklearn.datasets import make_regression\r\nfrom sklearn.preprocessing import MinMaxScaler\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.svm import SVR\r\nfrom sklearn.metrics import mean_absolute_error\r\n# define dataset\r\nX, y = make_regression(n_samples=1000, n_features=100, n_informative=10, noise=0.1, random_state=1)\r\n# split into train test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\n# reshape target variables so that we can transform them\r\ny_train = y_train.reshape((len(y_train), 1))\r\ny_test = y_test.reshape((len(y_test), 1))\r\n# scale input data\r\ntrans_in = MinMaxScaler()\r\ntrans_in.fit(X_train)\r\nX_train = trans_in.transform(X_train)\r\nX_test = trans_in.transform(X_test)\r\n# scale output data\r\ntrans_out = MinMaxScaler()\r\ntrans_out.fit(y_train)\r\ny_train = trans_out.transform(y_train)\r\ny_test = trans_out.transform(y_test)\r\n# define model\r\nmodel = SVR()\r\n# fit model on the training dataset\r\nmodel.fit(X_train, y_train)\r\n# make prediction on test set\r\nyhat = model.predict(X_test)\r\n# invert transforms so we can calculate errors\r\nyhat = yhat.reshape((len(yhat), 1))\r\nyhat = trans_out.inverse_transform(yhat)\r\ny_test = trans_out.inverse_transform(y_test)\r\n# calculate error\r\nscore = mean_absolute_error(y_test, yhat)\r\nprint(score)<\/pre>\n<p>Running the example fits an SVR model on the training dataset and evaluates it on the test set.<\/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 achieves a mean absolute error (MAE) of about 89.<\/p>\n<p>We would hope and expect that a SVR model fit on an encoded version of the input to achieve lower error for the encoding to be considered useful.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">89.51082036130629<\/pre>\n<p>We can update the example to first encode the data using the encoder model trained in the previous section.<\/p>\n<p>First, we can load the trained encoder model from the file.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# load the model from file\r\nencoder = load_model('encoder.h5')<\/pre>\n<p>We can then use the encoder to transform the raw input data (e.g. 100 columns) into bottleneck vectors (e.g. 100 element vectors).<\/p>\n<p>This process can be applied to the train and test datasets.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# encode the train data\r\nX_train_encode = encoder.predict(X_train)\r\n# encode the test data\r\nX_test_encode = encoder.predict(X_test)<\/pre>\n<p>We can then use this encoded data to train and evaluate the SVR model, as before.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n# define model\r\nmodel = SVR()\r\n# fit model on the training dataset\r\nmodel.fit(X_train_encode, y_train)\r\n# make prediction on test set\r\nyhat = model.predict(X_test_encode)<\/pre>\n<p>Tying this together, the complete example is listed below.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\"># support vector regression performance with encoded input\r\nfrom sklearn.datasets import make_regression\r\nfrom sklearn.preprocessing import MinMaxScaler\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.svm import SVR\r\nfrom sklearn.metrics import mean_absolute_error\r\nfrom tensorflow.keras.models import load_model\r\n# define dataset\r\nX, y = make_regression(n_samples=1000, n_features=100, n_informative=10, noise=0.1, random_state=1)\r\n# split into train test sets\r\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)\r\n# reshape target variables so that we can transform them\r\ny_train = y_train.reshape((len(y_train), 1))\r\ny_test = y_test.reshape((len(y_test), 1))\r\n# scale input data\r\ntrans_in = MinMaxScaler()\r\ntrans_in.fit(X_train)\r\nX_train = trans_in.transform(X_train)\r\nX_test = trans_in.transform(X_test)\r\n# scale output data\r\ntrans_out = MinMaxScaler()\r\ntrans_out.fit(y_train)\r\ny_train = trans_out.transform(y_train)\r\ny_test = trans_out.transform(y_test)\r\n# load the model from file\r\nencoder = load_model('encoder.h5')\r\n# encode the train data\r\nX_train_encode = encoder.predict(X_train)\r\n# encode the test data\r\nX_test_encode = encoder.predict(X_test)\r\n# define model\r\nmodel = SVR()\r\n# fit model on the training dataset\r\nmodel.fit(X_train_encode, y_train)\r\n# make prediction on test set\r\nyhat = model.predict(X_test_encode)\r\n# invert transforms so we can calculate errors\r\nyhat = yhat.reshape((len(yhat), 1))\r\nyhat = trans_out.inverse_transform(yhat)\r\ny_test = trans_out.inverse_transform(y_test)\r\n# calculate error\r\nscore = mean_absolute_error(y_test, yhat)\r\nprint(score)<\/pre>\n<p>Running the example first encodes the dataset using the encoder, then fits an SVR model on the training dataset and evaluates it on the test set.<\/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 achieves a MAE of about 69.<\/p>\n<p>This is a better MAE than the same model evaluated on the raw dataset, suggesting that the encoding is helpful for our chosen model and test harness.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">69.45890939600503<\/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<h3>Tutorials<\/h3>\n<ul>\n<li><a href=\"https:\/\/machinelearningmastery.com\/lstm-autoencoders\/\">A Gentle Introduction to LSTM Autoencoders<\/a><\/li>\n<li><a href=\"https:\/\/machinelearningmastery.com\/keras-functional-api-deep-learning\/\">How to Use the Keras Functional API for Deep Learning<\/a><\/li>\n<li><a href=\"https:\/\/machinelearningmastery.com\/tensorflow-tutorial-deep-learning-with-tf-keras\/\">TensorFlow 2 Tutorial: Get Started in Deep Learning With tf.keras<\/a><\/li>\n<\/ul>\n<h3>Books<\/h3>\n<ul>\n<li><a href=\"https:\/\/amzn.to\/3kV7gdV\">Deep Learning<\/a>, 2016.<\/li>\n<\/ul>\n<h3>APIs<\/h3>\n<ul>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.datasets.make_regression.html\">sklearn.datasets.make_regression API<\/a>.<\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.model_selection.train_test_split.html\">sklearn.model_selection.train_test_split API<\/a>.<\/li>\n<\/ul>\n<h3>Articles<\/h3>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Autoencoder\">Autoencoder, Wikipedia<\/a>.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered how to develop and evaluate an autoencoder for regression predictive modeling.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>An autoencoder is a neural network model that can be used to learn a compressed representation of raw data.<\/li>\n<li>How to train an autoencoder model on a training dataset and save just the encoder part of the model.<\/li>\n<li>How to use the encoder as a data preparation step when training a machine learning model.<\/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\/autoencoder-for-regression\/\">Autoencoder Feature Extraction for 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\/autoencoder-for-regression\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee Autoencoder is a type of neural network that can be used to learn a compressed representation of raw data. An autoencoder is [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2020\/12\/08\/autoencoder-feature-extraction-for-regression\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":4178,"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\/4177"}],"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=4177"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/4177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/4178"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=4177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=4177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=4177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}