Predict fuel efficiency using an Artificial Neural Network in TensorFlow 2.x
By: Jan Bollenbacher
Tags: TensorFlow, Tutorial, Regression, Neural Networks, Machine Learning

After we started using just one linear neuron, we continue and build our first Artificial Neural Network (ANN). This tutorial is built upon the regression tutorial by Tensorflow.

In this tutorial, we are using a dataset describing cars. We are provided with data as the number of cylinders, horsepower, weight, etc. We need to predict the miles per gallon (MPG). Therefore we use the labeled training data and implement our first ANN.

In the last section of this tutorial are a few ideas that you could try to optimize further.

Setting up the dataset

As last time we start by setting up the dataset. The dataset we are using is the Auto MPG dataset available from the UCI Machine Learning Repository.

We do this by adding a new Python file named ‘auto_mpg_dataset.py’ into our datasets folder and receive the raw dataset.

This should output something like this:

A glimpse into our dataset

There are a few problems:

If you run print(dataset.isna().sum()) you can see that there are rows with unknown values; we need to drop this using dataset = dataset.dropna() The origin is really categorical, not numeric. This introduces information into our dataset, which we don’t want. The column is encoded like this: 1-USA, 2-Europe, 3-Japan. Higher numbers are interpreted better, but we don’t want to rate any state of origin, so we need to encode this column one-hot.

dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})

dataset = pd.get_dummies(dataset, prefix='', prefix_sep='')

This leaves us with the following output:

We dropped rows with unknown values, and one-hot encoded the origin column.

We need to split the dataset into train- and test-set; we do this 80% trainset and 20% test set. The feature we want to predict is ‘MPG’ so we need to separate the target value, the “label”, from the features:

Split the dataset, separate the label and return everything in order

Implementing the Model

We can implement the model now. Therefore we open a file regression.py in folder ‘models’.

The model's structure in the last tutorial is the same; we only inherit from tf.keras.Model:

Normalization

If we look at the data in the table, we can see that there are columns with different scales. To treat each column the same way, we need to normalize the columns.

We can do this in two ways:

Use NumPy to normalize the dataset or use a normalize-layer in our ANN.

In this tutorial, we implement the latter.

To do that, we fit a normalize-layer to our feature data. We can do this in our __init__() method.

self.normalizer = preprocessing.Normalization()

self.normalizer.adapt(np.array(train_features))

Layers

In the init() function, we also need to define the dense layers. In a dense layer or feed-forward layer, every neuron of the layer is connected with every neuron of the following layer. The connections between neurons are the weights.

The function call() defines in which order we call the layers.

Our first model will use two dense layers with 64 neurons each and ReLU as non-linearity. The output layer is just one neuron that uses the activation function linear and outputs the predicted MPG. In the get_model() function, we feed the train_features to initialize the normalization-layer.

Training the Model

We need to connect our prepared dataset with our model and train it accordingly. Therefore we write a new trainer called trainer_regression.py and place it in the ‘trainer’ folder.

As you can see, we basically do the same steps as in the tutorial before:

get model get dataset train the model evaluate the model.

Changes in main.py

There are a few changes in main.py. We need:

parameters val_split and train_split add the trainer regression

That’s it!