<jupyter_start><jupyter_text>Copyright 2018 The TensorFlow Authors.<jupyter_code>#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#@title MIT License
#
# Copyright (c) 2017 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.<jupyter_output><empty_output><jupyter_text>Basic regression: Predict fuel efficiency       View on TensorFlow.org        Run in Google Colab        View source on GitHub        Download notebook   In a *regression* problem, the aim is to predict the output of a continuous value, like a price or a probability. Contrast this with a *classification* problem, where the aim is to select a class from a list of classes (for example, where a picture contains an apple or an orange, recognizing which fruit is in the picture).This tutorial uses the classic [Auto MPG](https://archive.ics.uci.edu/ml/datasets/auto+mpg) dataset and demonstrates how to build models to predict the fuel efficiency of the late-1970s and early 1980s automobiles. To do this, you will provide the models with a description of many automobiles from that time period. This description includes attributes like cylinders, displacement, horsepower, and weight.This example uses the Keras API. (Visit the Keras [tutorials](https://www.tensorflow.org/tutorials/keras) and [guides](https://www.tensorflow.org/guide/keras) to learn more.)<jupyter_code># Use seaborn for pairplot.
!pip install -q seaborn
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

# Make NumPy printouts easier to read.
np.set_printoptions(precision=3, suppress=True)
import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers

print(tf.__version__)<jupyter_output>2.6.0<jupyter_text>The Auto MPG datasetThe dataset is available from the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/).  Get the dataFirst download and import the dataset using pandas:<jupyter_code>url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data'
column_names = ['MPG', 'Cylinders', 'Displacement', 'Horsepower', 'Weight',
                'Acceleration', 'Model Year', 'Origin']

raw_dataset = pd.read_csv(url, names=column_names,
                          na_values='?', comment='\t',
                          sep=' ', skipinitialspace=True)
dataset = raw_dataset.copy()
dataset.tail()<jupyter_output><empty_output><jupyter_text>Clean the dataThe dataset contains a few unknown values:<jupyter_code>dataset.isna().sum()<jupyter_output><empty_output><jupyter_text>Drop those rows to keep this initial tutorial simple:<jupyter_code>dataset = dataset.dropna()<jupyter_output><empty_output><jupyter_text>The `"Origin"` column is categorical, not numeric. So the next step is to one-hot encode the values in the column with [pd.get_dummies](https://pandas.pydata.org/docs/reference/api/pandas.get_dummies.html).Note: You can set up the `tf.keras.Model` to do this kind of transformation for you but that's beyond the scope of this tutorial. Check out the [Classify structured data using Keras preprocessing layers](../structured_data/preprocessing_layers.ipynb) or [Load CSV data](../load_data/csv.ipynb) tutorials for examples.<jupyter_code>dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})
dataset = pd.get_dummies(dataset, columns=['Origin'], prefix='', prefix_sep='')
dataset.tail()<jupyter_output><empty_output><jupyter_text>Split the data into training and test setsNow, split the dataset into a training set and a test set. You will use the test set in the final evaluation of your models.<jupyter_code>train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)<jupyter_output><empty_output><jupyter_text>Inspect the dataReview the joint distribution of a few pairs of columns from the training set.The top row suggests that the fuel efficiency (MPG) is a function of all the other parameters. The other rows indicate they are functions of each other.<jupyter_code>sns.pairplot(train_dataset[['MPG', 'Cylinders', 'Displacement', 'Weight']], diag_kind='kde')<jupyter_output><empty_output><jupyter_text>Let's also check the overall statistics. Note how each feature covers a very different range:<jupyter_code>train_dataset.describe().transpose()<jupyter_output><empty_output><jupyter_text>Split features from labelsSeparate the target value—the "label"—from the features. This label is the value that you will train the model to predict.<jupyter_code>train_features = train_dataset.copy()
test_features = test_dataset.copy()

train_labels = train_features.pop('MPG')
test_labels = test_features.pop('MPG')<jupyter_output><empty_output><jupyter_text>NormalizationIn the table of statistics it's easy to see how different the ranges of each feature are:<jupyter_code>train_dataset.describe().transpose()[['mean', 'std']]<jupyter_output><empty_output><jupyter_text>It is good practice to normalize features that use different scales and ranges.One reason this is important is because the features are multiplied by the model weights. So, the scale of the outputs and the scale of the gradients are affected by the scale of the inputs.Although a model *might* converge without feature normalization, normalization makes training much more stable.Note: There is no advantage to normalizing the one-hot features—it is done here for simplicity. For more details on how to use the preprocessing layers, refer to the [Working with preprocessing layers](../../guide/keras/preprocessing_layers.ipynb) guide and the [Classify structured data using Keras preprocessing layers](../structured_data/preprocessing_layers.ipynb) tutorial.  The Normalization layerThe `tf.keras.layers.Normalization` is a clean and simple way to add feature normalization into your model.The first step is to create the layer:<jupyter_code>normalizer = tf.keras.layers.Normalization(axis=-1)<jupyter_output><empty_output><jupyter_text>Then, fit the state of the preprocessing layer to the data by calling `Normalization.adapt`:<jupyter_code>normalizer.adapt(np.array(train_features))<jupyter_output><empty_output><jupyter_text>Calculate the mean and variance, and store them in the layer:<jupyter_code>print(normalizer.mean.numpy())<jupyter_output>[[   5.478  195.318  104.869 2990.252   15.559   75.898    0.178    0.197
     0.624]]<jupyter_text>When the layer is called, it returns the input data, with each feature independently normalized:<jupyter_code>first = np.array(train_features[:1])

with np.printoptions(precision=2, suppress=True):
  print('First example:', first)
  print()
  print('Normalized:', normalizer(first).numpy())<jupyter_output>First example: [[   4.    90.    75.  2125.    14.5   74.     0.     0.     1. ]]

Normalized: [[-0.87 -1.01 -0.79 -1.03 -0.38 -0.52 -0.47 -0.5   0.78]]<jupyter_text>Linear regressionBefore building a deep neural network model, start with linear regression using one and several variables.  Linear regression with one variableBegin with a single-variable linear regression to predict `'MPG'` from `'Horsepower'`.Training a model with `tf.keras` typically starts by defining the model architecture. Use a `tf.keras.Sequential` model, which [represents a sequence of steps](.././guide/keras/sequential_model.ipynb).There are two steps in your single-variable linear regression model:- Normalize the `'Horsepower'` input features using the `tf.keras.layers.Normalization` preprocessing layer.- Apply a linear transformation ($y = mx+b$) to produce 1 output using a linear layer (`tf.keras.layers.Dense`).The number of _inputs_ can either be set by the `input_shape` argument, or automatically when the model is run for the first time. First, create a NumPy array made of the `'Horsepower'` features. Then, instantiate the `tf.keras.layers.Normalization` and fit its state to the `horsepower` data:<jupyter_code>horsepower = np.array(train_features['Horsepower'])

horsepower_normalizer = layers.Normalization(input_shape=[1,], axis=None)
horsepower_normalizer.adapt(horsepower)<jupyter_output><empty_output><jupyter_text>Build the Keras Sequential model:<jupyter_code>horsepower_model = tf.keras.Sequential([
    horsepower_normalizer,
    layers.Dense(units=1)
])

horsepower_model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
normalization_1 (Normalizati (None, 1)                 3         
_________________________________________________________________
dense (Dense)                (None, 1)                 2         
=================================================================
Total params: 5
Trainable params: 2
Non-trainable params: 3
_________________________________________________________________<jupyter_text>This model will predict `'MPG'` from `'Horsepower'`.Run the untrained model on the first 10 'Horsepower' values. The output won't be good, but notice that it has the expected shape of `(10, 1)`:<jupyter_code>horsepower_model.predict(horsepower[:10])<jupyter_output><empty_output><jupyter_text>Once the model is built, configure the training procedure using the Keras `Model.compile` method. The most important arguments to compile are the `loss` and the `optimizer`, since these define what will be optimized (`mean_absolute_error`) and how (using the `tf.keras.optimizers.Adam`).<jupyter_code>horsepower_model.compile(
    optimizer=tf.optimizers.Adam(learning_rate=0.1),
    loss='mean_absolute_error')<jupyter_output><empty_output><jupyter_text>Having configured the training configured, use Keras `Model.fit` to execute the training for 100 epochs:<jupyter_code>%%time
history = horsepower_model.fit(
    train_features['Horsepower'],
    train_labels,
    epochs=100,
    # Suppress logging.
    verbose=0,
    # Calculate validation results on 20% of the training data.
    validation_split = 0.2)<jupyter_output>CPU times: user 1.89 s, sys: 173 ms, total: 2.07 s
Wall time: 1.51 s<jupyter_text>Visualize the model's training progress using the stats stored in the `history` object:<jupyter_code>hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()
def plot_loss(history):
  plt.plot(history.history['loss'], label='loss')
  plt.plot(history.history['val_loss'], label='val_loss')
  plt.ylim([0, 10])
  plt.xlabel('Epoch')
  plt.ylabel('Error [MPG]')
  plt.legend()
  plt.grid(True)
plot_loss(history)<jupyter_output><empty_output><jupyter_text>Collect the results on the test set for later:<jupyter_code>test_results = {}

test_results['horsepower_model'] = horsepower_model.evaluate(
    test_features['Horsepower'],
    test_labels, verbose=0)<jupyter_output><empty_output><jupyter_text>Since this is a single variable regression, it's easy to view the model's predictions as a function of the input:<jupyter_code>x = tf.linspace(0.0, 250, 251)
y = horsepower_model.predict(x)
def plot_horsepower(x, y):
  plt.scatter(train_features['Horsepower'], train_labels, label='Data')
  plt.plot(x, y, color='k', label='Predictions')
  plt.xlabel('Horsepower')
  plt.ylabel('MPG')
  plt.legend()
plot_horsepower(x,y)<jupyter_output><empty_output><jupyter_text>Linear regression with multiple inputs You can use an almost identical setup to make predictions based on multiple inputs. This model still does the same $y = mx+b$ except that $m$ is a matrix and $b$ is a vector.Create a two-step Keras Sequential model again with the first layer being `normalizer` (`tf.keras.layers.Normalization(axis=-1)`) you defined earlier and adapted to the whole dataset:<jupyter_code>linear_model = tf.keras.Sequential([
    normalizer,
    layers.Dense(units=1)
])<jupyter_output><empty_output><jupyter_text>When you call `Model.predict` on a batch of inputs, it produces `units=1` outputs for each example:<jupyter_code>linear_model.predict(train_features[:10])<jupyter_output><empty_output><jupyter_text>When you call the model, its weight matrices will be built—check that the `kernel` weights (the $m$ in $y=mx+b$) have a shape of `(9, 1)`:<jupyter_code>linear_model.layers[1].kernel<jupyter_output><empty_output><jupyter_text>Configure the model with Keras `Model.compile` and train with `Model.fit` for 100 epochs:<jupyter_code>linear_model.compile(
    optimizer=tf.optimizers.Adam(learning_rate=0.1),
    loss='mean_absolute_error')
%%time
history = linear_model.fit(
    train_features,
    train_labels,
    epochs=100,
    # Suppress logging.
    verbose=0,
    # Calculate validation results on 20% of the training data.
    validation_split = 0.2)<jupyter_output>CPU times: user 1.9 s, sys: 223 ms, total: 2.12 s
Wall time: 1.53 s<jupyter_text>Using all the inputs in this regression model achieves a much lower training and validation error than the `horsepower_model`, which had one input:<jupyter_code>plot_loss(history)<jupyter_output><empty_output><jupyter_text>Collect the results on the test set for later:<jupyter_code>test_results['linear_model'] = linear_model.evaluate(
    test_features, test_labels, verbose=0)<jupyter_output><empty_output><jupyter_text>Regression with a deep neural network (DNN) In the previous section, you implemented two linear models for single and multiple inputs.Here, you will implement single-input and multiple-input DNN models.The code is basically the same except the model is expanded to include some "hidden" non-linear layers. The name "hidden" here just means not directly connected to the inputs or outputs. These models will contain a few more layers than the linear model:* The normalization layer, as before (with `horsepower_normalizer` for a single-input model and `normalizer` for a multiple-input model).* Two hidden, non-linear, `Dense` layers with the ReLU (`relu`) activation function nonlinearity.* A linear `Dense` single-output layer.Both models will use the same training procedure so the `compile` method is included in the `build_and_compile_model` function below.<jupyter_code>def build_and_compile_model(norm):
  model = keras.Sequential([
      norm,
      layers.Dense(64, activation='relu'),
      layers.Dense(64, activation='relu'),
      layers.Dense(1)
  ])

  model.compile(loss='mean_absolute_error',
                optimizer=tf.keras.optimizers.Adam(0.001))
  return model<jupyter_output><empty_output><jupyter_text>Regression using a DNN and a single input Create a DNN model with only `'Horsepower'` as input and `horsepower_normalizer` (defined earlier) as the normalization layer:<jupyter_code>dnn_horsepower_model = build_and_compile_model(horsepower_normalizer)<jupyter_output><empty_output><jupyter_text>This model has quite a few more trainable parameters than the linear models:<jupyter_code>dnn_horsepower_model.summary()<jupyter_output>Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
normalization_1 (Normalizati (None, 1)                 3         
_________________________________________________________________
dense_2 (Dense)              (None, 64)                128       
_________________________________________________________________
dense_3 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 65        
=================================================================
Total params: 4,356
Trainable params: 4,353
Non-trainable params: 3
_________________________________________________________________<jupyter_text>Train the model with Keras `Model.fit`:<jupyter_code>%%time
history = dnn_horsepower_model.fit(
    train_features['Horsepower'],
    train_labels,
    validation_split=0.2,
    verbose=0, epochs=100)<jupyter_output>CPU times: user 2.03 s, sys: 168 ms, total: 2.2 s
Wall time: 1.61 s<jupyter_text>This model does slightly better than the linear single-input `horsepower_model`:<jupyter_code>plot_loss(history)<jupyter_output><empty_output><jupyter_text>If you plot the predictions as a function of `'Horsepower'`, you should notice how this model takes advantage of the nonlinearity provided by the hidden layers:<jupyter_code>x = tf.linspace(0.0, 250, 251)
y = dnn_horsepower_model.predict(x)
plot_horsepower(x, y)<jupyter_output><empty_output><jupyter_text>Collect the results on the test set for later:<jupyter_code>test_results['dnn_horsepower_model'] = dnn_horsepower_model.evaluate(
    test_features['Horsepower'], test_labels,
    verbose=0)<jupyter_output><empty_output><jupyter_text>Regression using a DNN and multiple inputs Repeat the previous process using all the inputs. The model's performance slightly improves on the validation dataset.<jupyter_code>dnn_model = build_and_compile_model(normalizer)
dnn_model.summary()
%%time
history = dnn_model.fit(
    train_features,
    train_labels,
    validation_split=0.2,
    verbose=0, epochs=100)
plot_loss(history)<jupyter_output><empty_output><jupyter_text>Collect the results on the test set:<jupyter_code>test_results['dnn_model'] = dnn_model.evaluate(test_features, test_labels, verbose=0)<jupyter_output><empty_output><jupyter_text>Performance Since all models have been trained, you can review their test set performance:<jupyter_code>pd.DataFrame(test_results, index=['Mean absolute error [MPG]']).T<jupyter_output><empty_output><jupyter_text>These results match the validation error observed during training.  Make predictionsYou can now make predictions with the `dnn_model` on the test set using Keras `Model.predict` and review the loss:<jupyter_code>test_predictions = dnn_model.predict(test_features).flatten()

a = plt.axes(aspect='equal')
plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [MPG]')
plt.ylabel('Predictions [MPG]')
lims = [0, 50]
plt.xlim(lims)
plt.ylim(lims)
_ = plt.plot(lims, lims)<jupyter_output><empty_output><jupyter_text>It appears that the model predicts reasonably well.Now, check the error distribution:<jupyter_code>error = test_predictions - test_labels
plt.hist(error, bins=25)
plt.xlabel('Prediction Error [MPG]')
_ = plt.ylabel('Count')<jupyter_output><empty_output><jupyter_text>If you're happy with the model, save it for later use with `Model.save`:<jupyter_code>dnn_model.save('dnn_model')<jupyter_output>INFO:tensorflow:Assets written to: dnn_model/assets<jupyter_text>If you reload the model, it gives identical output:<jupyter_code>reloaded = tf.keras.models.load_model('dnn_model')

test_results['reloaded'] = reloaded.evaluate(
    test_features, test_labels, verbose=0)
pd.DataFrame(test_results, index=['Mean absolute error [MPG]']).T<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10438074634937804, 'fraction_numerical': 0.014110821574315355, 'mean_word_length': 5.6754184443715125, 'pattern_counts': {'":': 0, '<': 165, '<?xml version=': 0, '>': 165, 'https://': 6, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '43', 'dir': 'jupyter-structured-clean-dedup', 'id': '30260', 'license': 'Apache-2.0', 'n_tokens_mistral': 6235, 'n_tokens_neox': 5868, 'n_words': 1910, 'path': 'site/en/tutorials/keras/regression_mine.ipynb', 'repo_name': 'kewlcoder/docs', 'size': '20349'}