Let us begin the tutorial with a classical problem called Linear Regression [1]. In this chapter, we will train a model from a realistic dataset to predict home prices. Some important concepts in Machine Learning will be covered through this example.
The source code for this tutorial lives on book/fit_a_line. For instructions on getting started with this book,see Running This Book.
Suppose we have a dataset of
Each home is associated with
In our problem setup, the attribute
where
We first show the result of our model. The dataset UCI Housing Data Set is used to train a linear model to predict the home prices in Boston. The figure below shows the predictions the model makes for some home prices. The

Figure 1. Predicted Value V.S. Actual Value
In the UCI Housing Data Set, there are 13 home attributes
where
Now we need an objective to optimize, so that the learned parameters can make
For Linear Regression, the most common loss function is Mean Square Error (MSE) which has the following form:
That is, for a dataset of size
After setting up our model, there are several major steps to go through to train it:
- Initialize the parameters including the weights
$\vec{\omega}$ and the bias$b$ . For example, we can set their mean values as $0$s, and their standard deviations as $1$s. - Feedforward. Evaluate the network output and compute the corresponding loss.
- Backpropagate the errors. The errors will be propagated from the output layer back to the input layer, during which the model parameters will be updated with the corresponding errors.
- Repeat steps 2~3, until the loss is below a predefined threshold or the maximum number of epochs is reached.
The UCI housing dataset has 506 instances. Each instance describes the attributes of a house in surburban Boston. The attributes are explained below:
| Attribute Name | Characteristic | Data Type |
|---|---|---|
| CRIM | per capita crime rate by town | Continuous |
| ZN | proportion of residential land zoned for lots over 25,000 sq.ft. | Continuous |
| INDUS | proportion of non-retail business acres per town | Continuous |
| CHAS | Charles River dummy variable | Discrete, 1 if tract bounds river; 0 otherwise |
| NOX | nitric oxides concentration (parts per 10 million) | Continuous |
| RM | average number of rooms per dwelling | Continuous |
| AGE | proportion of owner-occupied units built prior to 1940 | Continuous |
| DIS | weighted distances to five Boston employment centres | Continuous |
| RAD | index of accessibility to radial highways | Continuous |
| TAX | full-value property-tax rate per $10,000 | Continuous |
| PTRATIO | pupil-teacher ratio by town | Continuous |
| B | 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town | Continuous |
| LSTAT | % lower status of the population | Continuous |
| MEDV | Median value of owner-occupied homes in $1000's | Continuous |
The last entry is the median home price.
We define a feature vector of length 13 for each home, where each entry corresponds to an attribute. Our first observation is that, among the 13 dimensions, there are 12 continuous dimensions and 1 discrete dimension.
Note that although a discrete value is also written as numeric values such as 0, 1, or 2, its meaning differs from a continuous value drastically. The linear difference between two discrete values has no meaning. For example, suppose
We also observe a huge difference among the value ranges of the 13 features (Figure 2). For instance, the values of feature B fall in
There are at least three reasons for Feature Normalization (Feature Scaling):
- A value range that is too large or too small might cause floating number overflow or underflow during computation.
- Different value ranges might result in varying importances of different features to the model (at least in the beginning of the training process). This assumption about the data is often unreasonable, making the optimization difficult, which in turn results in increased training time.
- Many machine learning techniques or models (e.g., L1/L2 regularization and Vector Space Model) assumes that all the features have roughly zero means and their value ranges are similar.

Figure 2. The value ranges of the features
We split the dataset in two, one for adjusting the model parameters, namely, for training the model, and the other for testing. The model error on the former is called the training error, and the error on the latter is called the test error. Our goal in training a model is to find the statistical dependency between the outputs and the inputs, so that we can predict outputs given new inputs. As a result, the test error reflects the performance of the model better than the training error does. We consider two things when deciding the ratio of the training set to the test set: 1) More training data will decrease the variance of the parameter estimation, yielding more reliable models; 2) More test data will decrease the variance of the test error, yielding more reliable test errors. One standard split ratio is
When training complex models, we usually have one more split: the validation set. Complex models usually have Hyperparameters that need to be set before the training process, such as the number of layers in the network. Because hyperparameters are not part of the model parameters, they cannot be trained using the same loss function. Thus we will try several sets of hyperparameters to train several models and cross-validate them on the validation set to pick the best one; finally, the selected trained model is tested on the test set. Because our model is relatively simple, we will omit this validation process.
fit_a_line/trainer.py demonstrates the training using PaddlePaddle.
Our program starts with importing necessary packages:
import paddle import paddle.fluid as fluid import numpy from __future__ import print_function try: from paddle.fluid.contrib.trainer import * from paddle.fluid.contrib.inferencer import * except ImportError: print( "In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib", file=sys.stderr) from paddle.fluid.trainer import * from paddle.fluid.inferencer import *
We encapsulated the UCI Housing Data Set in our Python module uci_housing. This module can
- download the dataset to
~/.cache/paddle/dataset/uci_housing/housing.data, if you haven't yet, and - preprocess the dataset.
We define data feeders for test and train. The feeder reads a BATCH_SIZE of data each time and feed them to the training/testing process. If the user wants some randomness on the data order, she can define both a BATCH_SIZE and a buf_size. That way the datafeeder will yield the first BATCH_SIZE data out of a shuffle of the first buf_size data.
BATCH_SIZE = 20 train_reader = paddle.batch( paddle.reader.shuffle( paddle.dataset.uci_housing.train(), buf_size=500), batch_size=BATCH_SIZE) test_reader = paddle.batch( paddle.reader.shuffle( paddle.dataset.uci_housing.test(), buf_size=500), batch_size=BATCH_SIZE)
train_program sets up the network structure of this current training model. For linear regression, it is simply a fully connected layer from the input to the output. More complex structures like CNN and RNN will be introduced in later chapters. The train_program must return an avg_loss as its first returned parameter because it is needed in backpropagation.
def train_program(): y = fluid.layers.data(name='y', shape=[1], dtype='float32') # feature vector of length 13 x = fluid.layers.data(name='x', shape=[13], dtype='float32') y_predict = fluid.layers.fc(input=x, size=1, act=None) loss = fluid.layers.square_error_cost(input=y_predict, label=y) avg_loss = fluid.layers.mean(loss) return avg_loss
In the following SGD optimizer, learning_rate specifies the learning rate in the optimization procedure.
def optimizer_program(): return fluid.optimizer.SGD(learning_rate=0.001)
Specify your training environment, you should specify if the training is on CPU or GPU.
use_cuda = False place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
The trainer will take the train_program as input.
trainer = Trainer( train_func=train_program, place=place, optimizer_func=optimizer_program)
PaddlePaddle provides the reader mechanism for loading the training data. A reader may return multiple columns, and we need a Python dictionary to specify the mapping from column index to data layers.
feed_order=['x', 'y']
Moreover, an event handler is provided to print the training progress:
# Specify the directory to save the parameters params_dirname = "fit_a_line.inference.model" train_title = "Train cost" test_title = "Test cost" step = 0 # event_handler prints training and testing info def event_handler(event): global step if isinstance(event, EndStepEvent): if step % 10 == 0: # record a train cost every 10 batches print("%s, Step %d, Cost %f" % (train_title, step, event.metrics[0])) if step % 100 == 0: # record a test cost every 100 batches test_metrics = trainer.test( reader=test_reader, feed_order=feed_order) print("%s, Step %d, Cost %f" % (test_title, step, test_metrics[0])) if test_metrics[0] < 10.0: # If the accuracy is good enough, we can stop the training. print('loss is less than 10.0, stop') trainer.stop() step += 1 if isinstance(event, EndEpochEvent): if event.epoch % 10 == 0: # We can save the trained parameters for the inferences later if params_dirname is not None: trainer.save_params(params_dirname)
We now can start training by calling trainer.train().
%matplotlib inline # The training could take up to a few minutes. trainer.train( reader=train_reader, num_epochs=100, event_handler=event_handler, feed_order=feed_order)
Initialize the Inferencer with the inference_program and the params_dirname, which is where we saved our params
Similar to the trainer.train, the Inferencer needs to take an inference_program to do inference. Prune the train_program to only have the y_predict.
def inference_program(): x = fluid.layers.data(name='x', shape=[13], dtype='float32') y_predict = fluid.layers.fc(input=x, size=1, act=None) return y_predict
Inferencer will load the trained model from params_dirname and use it to infer the unseen data.
inferencer = Inferencer( infer_func=inference_program, param_path=params_dirname, place=place) batch_size = 10 test_reader = paddle.batch(paddle.dataset.uci_housing.test(),batch_size=batch_size) test_data = next(test_reader()) test_x = numpy.array([data[0] for data in test_data]).astype("float32") test_y = numpy.array([data[1] for data in test_data]).astype("float32") results = inferencer.infer({'x': test_x}) print("infer results: (House Price)") for idx, val in enumerate(results[0]): print("%d: %.2f" % (idx, val)) print("\nground truth:") for idx, val in enumerate(test_y): print("%d: %.2f" % (idx, val))
This chapter introduces Linear Regression and how to train and test this model with PaddlePaddle, using the UCI Housing Data Set. Because a large number of more complex models and techniques are derived from linear regression, it is important to understand its underlying theory and limitation.
- https://en.wikipedia.org/wiki/Linear_regression
- Friedman J, Hastie T, Tibshirani R. The elements of statistical learning[M]. Springer, Berlin: Springer series in statistics, 2001.
- Murphy K P. Machine learning: a probabilistic perspective[M]. MIT press, 2012.
- Bishop C M. Pattern recognition[J]. Machine Learning, 2006, 128.
This tutorial is contributed by PaddlePaddle, and licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
