.. _sec_softmax_concise: Concise Implementation of Softmax Regression ============================================ Just as high-level APIs of deep learning frameworks made it much easier to implement linear regression in :numref:`sec_linear_concise`, we will find it similarly (or possibly more) convenient for implementing classification models. Let us stick with the Fashion-MNIST dataset and keep the batch size at 256 as in :numref:`sec_softmax_scratch`. .. raw:: html
mxnetpytorchtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python from mxnet import gluon, init, npx from mxnet.gluon import nn from d2l import mxnet as d2l npx.set_np() batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import torch from torch import nn from d2l import torch as d2l batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import tensorflow as tf from d2l import tensorflow as d2l batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) .. raw:: html
.. raw:: html
Initializing Model Parameters ----------------------------- As mentioned in :numref:`sec_softmax`, the output layer of softmax regression is a fully-connected layer. Therefore, to implement our model, we just need to add one fully-connected layer with 10 outputs to our ``Sequential``. Again, here, the ``Sequential`` is not really necessary, but we might as well form the habit since it will be ubiquitous when implementing deep models. Again, we initialize the weights at random with zero mean and standard deviation 0.01. .. raw:: html
mxnetpytorchtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python net = nn.Sequential() net.add(nn.Dense(10)) net.initialize(init.Normal(sigma=0.01)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python # PyTorch does not implicitly reshape the inputs. Thus we define the flatten # layer to reshape the inputs before the linear layer in our network net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10)) def init_weights(m): if type(m) == nn.Linear: nn.init.normal_(m.weight, std=0.01) net.apply(init_weights); .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python net = tf.keras.models.Sequential() net.add(tf.keras.layers.Flatten(input_shape=(28, 28))) weight_initializer = tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.01) net.add(tf.keras.layers.Dense(10, kernel_initializer=weight_initializer)) .. raw:: html
.. raw:: html
.. _subsec_softmax-implementation-revisited: Softmax Implementation Revisited -------------------------------- In the previous example of :numref:`sec_softmax_scratch`, we calculated our model's output and then ran this output through the cross-entropy loss. Mathematically, that is a perfectly reasonable thing to do. However, from a computational perspective, exponentiation can be a source of numerical stability issues. Recall that the softmax function calculates :math:`\hat y_j = \frac{\exp(o_j)}{\sum_k \exp(o_k)}`, where :math:`\hat y_j` is the :math:`j^\mathrm{th}` element of the predicted probability distribution :math:`\hat{\mathbf{y}}` and :math:`o_j` is the :math:`j^\mathrm{th}` element of the logits :math:`\mathbf{o}`. If some of the :math:`o_k` are very large (i.e., very positive), then :math:`\exp(o_k)` might be larger than the largest number we can have for certain data types (i.e., *overflow*). This would make the denominator (and/or numerator) ``inf`` (infinity) and we wind up encountering either 0, ``inf``, or ``nan`` (not a number) for :math:`\hat y_j`. In these situations we do not get a well-defined return value for cross-entropy. One trick to get around this is to first subtract :math:`\max(o_k)` from all :math:`o_k` before proceeding with the softmax calculation. You can see that this shifting of each :math:`o_k` by constant factor does not change the return value of softmax: .. math:: \begin{aligned} \hat y_j & = \frac{\exp(o_j - \max(o_k))\exp(\max(o_k))}{\sum_k \exp(o_k - \max(o_k))\exp(\max(o_k))} \\ & = \frac{\exp(o_j - \max(o_k))}{\sum_k \exp(o_k - \max(o_k))}. \end{aligned} After the subtraction and normalization step, it might be possible that some :math:`o_j - \max(o_k)` have large negative values and thus that the corresponding :math:`\exp(o_j - \max(o_k))` will take values close to zero. These might be rounded to zero due to finite precision (i.e., *underflow*), making :math:`\hat y_j` zero and giving us ``-inf`` for :math:`\log(\hat y_j)`. A few steps down the road in backpropagation, we might find ourselves faced with a screenful of the dreaded ``nan`` results. Fortunately, we are saved by the fact that even though we are computing exponential functions, we ultimately intend to take their log (when calculating the cross-entropy loss). By combining these two operators softmax and cross-entropy together, we can escape the numerical stability issues that might otherwise plague us during backpropagation. As shown in the equation below, we avoid calculating :math:`\exp(o_j - \max(o_k))` and can use instead :math:`o_j - \max(o_k)` directly due to the canceling in :math:`\log(\exp(\cdot))`: .. math:: \begin{aligned} \log{(\hat y_j)} & = \log\left( \frac{\exp(o_j - \max(o_k))}{\sum_k \exp(o_k - \max(o_k))}\right) \\ & = \log{(\exp(o_j - \max(o_k)))}-\log{\left( \sum_k \exp(o_k - \max(o_k)) \right)} \\ & = o_j - \max(o_k) -\log{\left( \sum_k \exp(o_k - \max(o_k)) \right)}. \end{aligned} We will want to keep the conventional softmax function handy in case we ever want to evaluate the output probabilities by our model. But instead of passing softmax probabilities into our new loss function, we will just pass the logits and compute the softmax and its log all at once inside the cross-entropy loss function, which does smart things like the `"LogSumExp trick" `__. .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = gluon.loss.SoftmaxCrossEntropyLoss() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = nn.CrossEntropyLoss(reduction='none') .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) .. raw:: html
.. raw:: html
Optimization Algorithm ---------------------- Here, we use minibatch stochastic gradient descent with a learning rate of 0.1 as the optimization algorithm. Note that this is the same as we applied in the linear regression example and it illustrates the general applicability of the optimizers. .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1}) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trainer = torch.optim.SGD(net.parameters(), lr=0.1) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trainer = tf.keras.optimizers.SGD(learning_rate=.1) .. raw:: html
.. raw:: html
Training -------- Next we call the training function defined in :numref:`sec_softmax_scratch` to train the model. .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python num_epochs = 10 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer) .. figure:: output_softmax-regression-concise_75d138_51_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python num_epochs = 10 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer) .. figure:: output_softmax-regression-concise_75d138_54_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python num_epochs = 10 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer) .. figure:: output_softmax-regression-concise_75d138_57_0.svg .. raw:: html
.. raw:: html
As before, this algorithm converges to a solution that achieves a decent accuracy, albeit this time with fewer lines of code than before. Summary ------- - Using high-level APIs, we can implement softmax regression much more concisely. - From a computational perspective, implementing softmax regression has intricacies. Note that in many cases, a deep learning framework takes additional precautions beyond these most well-known tricks to ensure numerical stability, saving us from even more pitfalls that we would encounter if we tried to code all of our models from scratch in practice. Exercises --------- 1. Try adjusting the hyperparameters, such as the batch size, number of epochs, and learning rate, to see what the results are. 2. Increase the number of epochs for training. Why might the test accuracy decrease after a while? How could we fix this? .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html