Dive into Deep Learning
Table Of Contents
Dive into Deep Learning
Table Of Contents

4.2. Implementation of Multilayer Perceptron from Scratch

Now that we know how multilayer perceptrons (MLPs) work in theory, let’s implement them. First, we import the required packages.

In [1]:
import sys
sys.path.insert(0, '..')

%matplotlib inline
import d2l
from mxnet import nd
from mxnet.gluon import loss as gloss

To compare against the results we previously achieved with vanilla softmax regression, we continue to use the Fashion-MNIST image classification dataset.

In [2]:
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

4.2.1. Initialize Model Parameters

Recall that this dataset contains 10 classes and that each image consists of a \(28 \times 28 = 784\) grid of pixel values. Since we’ll be discarding the spatial strucutre (for now), we can just think of this as a classifiation dataset with \(784\) input features and \(10\) classes. In particular we will implement our MLP with one hidden layer and \(256\) hidden units. Note that we can regard both of these choices as hyperparameters that could be set based on performance on validation data. Typically, we’ll choose layer widths as powers of \(2\) to make everything align nicely in memory.

Again, we will allocate several NDArrays to represent our parameters. Note that we now have one weight matrix and one bias vector per layer. As always, we must call attach_grad to allocate memory for the gradients with respect to these parameters.

In [3]:
num_inputs, num_outputs, num_hiddens = 784, 10, 256

W1 = nd.random.normal(scale=0.01, shape=(num_inputs, num_hiddens))
b1 = nd.zeros(num_hiddens)
W2 = nd.random.normal(scale=0.01, shape=(num_hiddens, num_outputs))
b2 = nd.zeros(num_outputs)
params = [W1, b1, W2, b2]

for param in params:

4.2.2. Activation Function

To make sure we know how everything works, we will use the maximum function to implement ReLU ourselves, instead of invoking nd.relu directly.

In [4]:
def relu(X):
    return nd.maximum(X, 0)

4.2.3. The model

As in softmax regression, we will reshape each 2D image into a flat vector of length num_inputs. Finally, we cam implement our model with just a few lines of code.

In [5]:
def net(X):
    X = X.reshape((-1, num_inputs))
    H = relu(nd.dot(X, W1) + b1)
    return nd.dot(H, W2) + b2

4.2.4. The Loss Function

For better numerical stability and because we already know how to implement softmax regression completely from scratch, we will use Gluon’s integrated function for calculating the softmax and cross-entropy loss. Recall that we discussed some of these intricacies in the previous section. We encourage the interested reader to examing the source code for mxnet.gluon.loss.nnSoftmaxCrossEntropyLoss for more details.

In [6]:
loss = gloss.SoftmaxCrossEntropyLoss()

4.2.5. Training

Steps for training the MLP are no different than for softmax regression. In the d2l package, we directly call the train_ch3 function, whose implementation was introduced here. We set the number of epochs to \(10\) and the learning rate to \(0.5\).

In [7]:
num_epochs, lr = 10, 0.5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,
              params, lr)
epoch 1, loss 0.8199, train acc 0.697, test acc 0.809
epoch 2, loss 0.4874, train acc 0.820, test acc 0.843
epoch 3, loss 0.4299, train acc 0.840, test acc 0.865
epoch 4, loss 0.3983, train acc 0.853, test acc 0.865
epoch 5, loss 0.3716, train acc 0.863, test acc 0.864
epoch 6, loss 0.3588, train acc 0.867, test acc 0.875
epoch 7, loss 0.3440, train acc 0.872, test acc 0.875
epoch 8, loss 0.3296, train acc 0.880, test acc 0.875
epoch 9, loss 0.3168, train acc 0.882, test acc 0.883
epoch 10, loss 0.3083, train acc 0.885, test acc 0.881

To see how well we did, let’s apply the model to some test data. If you’re interested, compare the result to corresponding linear model.

In [8]:
for X, y in test_iter:

true_labels = d2l.get_fashion_mnist_labels(y.asnumpy())
pred_labels = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1).asnumpy())
titles = [truelabel + '\n' + predlabel
          for truelabel, predlabel in zip(true_labels, pred_labels)]

d2l.show_fashion_mnist(X[0:9], titles[0:9])

This looks a bit better than our previous result, a good sign that we’re on the right path.

4.2.6. Summary

We saw that implementing a simple MLP is easy, even when done manually. That said, with a large number of layers, this can get messy (e.g. naming and keeping track of the model parameters, etc).

4.2.7. Exercises

  1. Change the value of the hyper-parameter num_hiddens in order to see how this hyperparameter influences your results.
  2. Try adding a new hidden layer to see how it affects the results.
  3. How does changing the learning rate change the result.
  4. What is the best result you can get by optimizing over all the parameters (learning rate, iterations, number of hidden layers, number of hidden units per layer)?

4.2.8. Scan the QR Code to Discuss