Table of Contents
1. Linear Regression Implementation from Scratch
Now that you understand the key ideas behind linear regression, we can begin to work through a hands-on implementation in code. In this section, we will implement the entire method from scratch, including the data pipeline, the model, the loss function, and the minibatch stochastic gradient descent optimizer. While modern deep learning frameworks can automate nearly all of this work, implementing things from scratch is the only way to make sure that you really know what you are doing. Moreover, when it comes time to customize models, defining our own layers or loss functions, understanding how things work under the hood will prove handy. In this section, we will rely only on tensors and auto differentiation. Afterwards, we will introduce a more concise implementation, taking advantage of bells and whistles of deep learning frameworks.
(ns clj-d2l.linreg (:require [clj-djl.ndarray :as nd] [clj-djl.device :as device] [clj-djl.engine :as engine] [clj-djl.training :as t] [clj-djl.training.dataset :as ds] [clj-d2l.core :as d2l]))
1.1. Generating the Dataset
To keep things simple, we will construct an artificial dataset according to a linear model with additive noise. Our task will be to recover this model’s parameters using the finite set of examples contained in our dataset. We will keep the data low-dimensional so we can visualize it easily. In the following code snippet, we generate a dataset containing 1000 examples, each consisting of 2 features sampled from a standard normal distribution. Thus our synthetic dataset will be a matrix \(\mathbf{X}\in \mathbb{R}^{1000 \times 2}\).
The true parameters generating our dataset will be \(\mathbf{w} = [2, -3.4]^\top\) and \(b = 4.2\), and our synthetic labels will be assigned according to the following linear model with the noise term \(\epsilon\):
\begin{equation} \mathbf{y}= \mathbf{X} \mathbf{w} + b + \mathbf\epsilon. \end{equation}You could think of \(\epsilon\) as capturing potential measurement errors on the features and labels. We will assume that the standard assumptions hold and thus that \(\epsilon\) obeys a normal distribution with mean of 0. To make our problem easy, we will set its standard deviation to 0.01. The following code generates our synthetic dataset.
(defn synthetic-data [ndm w b num] (let [X (nd/random-normal ndm [num (nd/size w)]) y (nd/+ (nd/dot X w) b) noise (nd/random-normal ndm 0 0.01 (nd/shape y) :float32)] [X (nd/+ y noise)])) (def ndm (nd/new-base-manager)) (def true-w (nd/create ndm (float-array [2 -3.4]))) (def true-b 4.2) (def dp (synthetic-data ndm true-w true-b 1000)) (def features (get dp 0)) (def labels (get dp 1))
Note that each row in features consists of a 2-dimensional data example and that each row in labels consists of a 1-dimensional label value (a scalar).
(str "features(0): " (nd/to-vec (nd/get features [0])) "\nlabels(0): " (nd/get-element labels [0]))
features(0): [1.1630785 2.2122061] labels(0): -1.0015316
By generating a scatter plot using the second feature and labels
, we
can clearly observe the linear correlation between the two.
(let [x (nd/to-vec (nd/get features ":, 1")) y (nd/to-vec labels)] (d2l/plot-scatter "notes/figures/synthetic_data.svg" "data" x y))
1.2. Reading the Dataset
Recall that training models consists of making multiple passes over the dataset, grabbing one minibatch of examples at a time, and using them to update our model. Since this process is so fundamental to training machine learning algorithms, it is worth defining a utility function to shuffle the dataset and access it in minibatches.
In the following code, we define the data-iter
function to demonstrate
one possible implementation of this functionality. The function takes
a batch size, a matrix of features, and a vector of labels, yielding
minibatches of the size batch-size
. Each minibatch consists of a tuple
of features and labels.
(defn data-iter [batch-size features labels] (let [num-examples (nd/size features) indices (range num-examples) indices (shuffle indices)] (for [i (range 0 num-examples batch-size)] [(->> (range i (min (+ i batch-size) num-examples)) (map #(nd/get features [%])) (nd/stack)) (->> (range i (min (+ i batch-size) num-examples)) (map #(nd/get labels [%])) (nd/stack))])))
In general, note that we want to use reasonably sized minibatches to take advantage of the GPU hardware, which excels at parallelizing operations. Because each example can be fed through our models in parallel and the gradient of the loss function for each example can also be taken in parallel, GPUs allow us to process hundreds of examples in scarcely more time than it might take to process just a single example.
To build some intuition, let us read and print the first small batch
of data examples. The shape of the features in each minibatch tells us
both the minibatch size and the number of input features. Likewise,
our minibatch of labels will have a shape given by batch-size
.
(first (first (data-iter 10 features labels)))
ND: (10, 2) cpu() float32 [[ 1.1631, 2.2122], [ 0.4838, 0.774 ], [ 0.2996, 1.0434], [ 0.153 , 1.1839], [-1.1688, 1.8917], [ 1.5581, -1.2347], [-0.5459, -1.771 ], [-2.3556, -0.4514], [ 0.5414, 0.5794], [ 2.6785, -1.8561], ]
(second (first (data-iter 10 features labels)))
ND: (10) cpu() float32 [-1.0015, 2.5281, 1.2197, 0.4807, -4.5658, 11.5155, 9.1389, 1.0271, 3.3058, 15.8548]
As we run the iteration, we obtain distinct minibatches successively until the entire dataset has been exhausted (try this). While the iteration implemented above is good for didactic purposes, it is inefficient in ways that might get us in trouble on real problems. For example, it requires that we load all the data in memory and that we perform lots of random memory access. The built-in iterators implemented in a deep learning framework are considerably more efficient and they can deal with both data stored in files and data fed via data streams.
1.3. Initializing Model Parameters
Before we can begin optimizing our model’s parameters by minibatch stochastic gradient descent, we need to have some parameters in the first place. In the following code, we initialize weights by sampling random numbers from a normal distribution with mean 0 and a standard deviation of 0.01, and setting the bias to 0.
(def w (nd/random-normal ndm 0 0.01 [2 1] :float32)) (def b (nd/zeros ndm [1])) (nd/set-requires-gradient w true) (nd/set-requires-gradient b true) (println (nd/to-vec w)) (println (nd/to-vec b))
[0.0013263007 0.0072455113] [0.0]
After initializing our parameters, our next task is to update them until they fit our data sufficiently well. Each update requires taking the gradient of our loss function with respect to the parameters. Given this gradient, we can update each parameter in the direction that may reduce the loss.
Since nobody wants to compute gradients explicitly (this is tedious and error prone), we use automatic differentiation, as introduced in Section 2.5-automatic-differentiation.html#automatic_differentiation, to compute the gradient.
1.4. Defining the Model
Next, we must define our model, relating its inputs and parameters to its outputs. Recall that to calculate the output of the linear model, we simply take the matrix-vector dot product of the input features \(\mathbf{X}\) and the model weights \(\mathbf{w}\), and add the offset \(b\) to each example. Note that below \(\mathbf{Xw}\) is a vector and \(b\) is a scalar. Recall the broadcasting mechanism as described in Section 2.1-data-manipulation.html#9dcbe412-db7e-485a-bb3c-d7181f2f7f05. When we add a vector and a scalar, the scalar is added to each component of the vector.
(defn linreg "The linear regression model." [X w b] (nd/+ (nd/dot X w) b))
1.5. Defining the Loss Function
Since updating our model requires taking the gradient of our loss
function, we ought to define the loss function first. Here we will use
the squared loss function as described in Section
3.1-linear-regression.html#lin_reg. In the implementation,
we need to transform the true value y
into the predicted value’s shape
y-hat
. The result returned by the following function will also have
the same shape as y-hat
.
(defn squared-loss [y-hat y] (nd// (nd/* (nd/- y-hat (nd/reshape y (nd/shape y-hat))) (nd/- y-hat (nd/reshape y (nd/shape y-hat)))) 2))
1.6. Defining the Optimization Algorithm
As we discussed in Section 3.1-linear-regression.html#lin_reg, linear regression has a closed-form solution. However, this is not a book about linear regression: it is a book about deep learning. Since none of the other models that this book introduces can be solved analytically, we will take this opportunity to introduce your first working example of minibatch stochastic gradient descent.
At each step, using one minibatch randomly drawn from our dataset, we
will estimate the gradient of the loss with respect to our parameters.
Next, we will update our parameters in the direction that may reduce
the loss. The following code applies the minibatch stochastic gradient
descent update, given a set of parameters, a learning rate, and a
batch size. The size of the update step is determined by the learning
rate lr
. Because our loss is calculated as a sum over the minibatch of
examples, we normalize our step size by the batch size (batch-size
),
so that the magnitude of a typical step size does not depend heavily
on our choice of the batch size.
(defn sgd "Minibatch stochastic gradient descent." [params lr batch-size] (doseq [param params] ;; param = param - param.gradient * lr / batchSize (nd/-! param (nd// (nd/* (nd/get-gradient param) lr) batch-size))))
1.7. Training
Now that we have all of the parts in place, we are ready to implement the main training loop. It is crucial that you understand this code because you will see nearly identical training loops over and over again throughout your career in deep learning.
In each iteration, we will grab a minibatch of training examples, and
pass them through our model to obtain a set of predictions. After
calculating the loss, we initiate the backwards pass through the
network, storing the gradients with respect to each
parameter. Finally, we will call the optimization algorithm sgd
to
update the model parameters.
In summary, we will execute the following loop:
- Initialize parameters \((\mathbf{w}, b)\)
- Repeat until done
- Compute gradient \(\mathbf{g} \leftarrow \partial_{(\mathbf{w},b)} \frac{1}{|\mathcal{B}|} \sum_{i \in \mathcal{B}} l(\mathbf{x}^{(i)}, y^{(i)}, \mathbf{w}, b)\)
- Update parameters \((\mathbf{w}, b) \leftarrow (\mathbf{w}, b) - \eta \mathbf{g}\)
In each epoch, we will iterate through the entire dataset (using the
data-iter
function) once passing through every example in the training
dataset (assuming that the number of examples is divisible by the
batch size). The number of epochs num-epochs
and the learning rate lr
are both hyperparameters, which we set here to 3 and 0.03,
respectively. Unfortunately, setting hyperparameters is tricky and
requires some adjustment by trial and error. We elide these details
for now but revise them later in Section 11.
(def lr 0.03) (def num-epochs 3) (def batch-size 10) [lr num-epochs batch-size]
[0.03 3 10]
(def datasets (data-iter batch-size features labels))
#'clj-d2l.linreg/datasets
(doseq [epoch (range num-epochs)] (doseq [[X y] datasets] (with-open [gc (t/gradient-collector)] (let [l (-> (linreg X w b) (squared-loss y))] (t/backward gc l))) (sgd [w b] lr batch-size)) (println "epoch " epoch (-> (squared-loss (linreg features w b) labels) (nd/mean) (nd/get-element))))
epoch 0 3.3477103E-4 epoch 1 5.24727E-5 epoch 2 5.1000967E-5
In this case, because we synthesized the dataset ourselves, we know precisely what the true parameters are. Thus, we can evaluate our success in training by comparing the true parameters with those that we learned through our training loop. Indeed they turn out to be very close to each other.
(println (nd/to-vec w)) (println (nd/to-vec true-w)) (def w-error (nd/to-vec (nd/- true-w (nd/reshape w (nd/get-shape true-w))))) (println "Error in estimating w:" (vec w-error)) (println "Error in estimating w:" (- true-b (nd/get-element b)))
[1.999567 -3.400089] [2.0 -3.4] Error in estimating w: [4.3296814E-4 8.893013E-5] Error in estimating w: 5.704879760743964E-4
Note that we should not take it for granted that we are able to recover the parameters perfectly. However, in machine learning, we are typically less concerned with recovering true underlying parameters, and more concerned with parameters that lead to highly accurate prediction. Fortunately, even on difficult optimization problems, stochastic gradient descent can often find remarkably good solutions, owing partly to the fact that, for deep networks, there exist many configurations of the parameters that lead to highly accurate prediction.
1.8. Summary
- We saw how a deep network can be implemented and optimized from scratch, using just tensors and auto differentiation, without any need for defining layers or fancy optimizers.
- This section only scratches the surface of what is possible. In the following sections, we will describe additional models based on the concepts that we have just introduced and learn how to implement them more concisely.