Concise Implementation of Multilayer Perceptrons

:label:sec_mlp_concise

As you might expect, by relying on the high-level APIs, we can implement MLPs even more concisely.

```{.python .input} from d2l import mxnet as d2l from mxnet import gluon, init, npx from mxnet.gluon import nn npx.set_np()

  1. ```{.python .input}
  2. #@tab pytorch
  3. from d2l import torch as d2l
  4. import torch
  5. from torch import nn

```{.python .input}

@tab tensorflow

from d2l import tensorflow as d2l import tensorflow as tf

  1. ## Model
  2. As compared with our concise implementation
  3. of softmax regression implementation
  4. (:numref:`sec_softmax_concise`),
  5. the only difference is that we add
  6. *two* fully-connected layers
  7. (previously, we added *one*).
  8. The first is our hidden layer,
  9. which contains 256 hidden units
  10. and applies the ReLU activation function.
  11. The second is our output layer.
  12. ```{.python .input}
  13. net = nn.Sequential()
  14. net.add(nn.Dense(256, activation='relu'),
  15. nn.Dense(10))
  16. net.initialize(init.Normal(sigma=0.01))

```{.python .input}

@tab pytorch

net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(), nn.Linear(256, 10))

def initweights(m): if type(m) == nn.Linear: torch.nn.init.normal(m.weight, std=0.01)

net.apply(init_weights)

  1. ```{.python .input}
  2. #@tab tensorflow
  3. net = tf.keras.models.Sequential([
  4. tf.keras.layers.Flatten(),
  5. tf.keras.layers.Dense(256, activation='relu'),
  6. tf.keras.layers.Dense(10)])

The training loop is exactly the same as when we implemented softmax regression. This modularity enables us to separate matters concerning the model architecture from orthogonal considerations.

```{.python .input} batch_size, lr, num_epochs = 256, 0.1, 10 loss = gluon.loss.SoftmaxCrossEntropyLoss() trainer = gluon.Trainer(net.collect_params(), ‘sgd’, {‘learning_rate’: lr})

  1. ```{.python .input}
  2. #@tab pytorch
  3. batch_size, lr, num_epochs = 256, 0.1, 10
  4. loss = nn.CrossEntropyLoss()
  5. trainer = torch.optim.SGD(net.parameters(), lr=lr)

```{.python .input}

@tab tensorflow

batch_size, lr, num_epochs = 256, 0.1, 10 loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) trainer = tf.keras.optimizers.SGD(learning_rate=lr)

  1. ```{.python .input}
  2. #@tab all
  3. train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
  4. d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

Summary

  • Using high-level APIs, we can implement MLPs much more concisely.
  • For the same classification problem, the implementation of an MLP is the same as that of softmax regression except for additional hidden layers with activation functions.

Exercises

  1. Try adding different numbers of hidden layers (you may also modify the learning rate). What setting works best?
  2. Try out different activation functions. Which one works best?
  3. Try different schemes for initializing the weights. What method works best?

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab:

:begin_tab:tensorflow Discussions :end_tab: