Participate in the 13th day of The November Gwen Challenge, see the details of the event: 2021 last Gwen Challenge

It’s easy enough to implement a multilayer perceptron manually, but now let’s see how to do it in a framework. Again, implement a two-tier model:

Some of you might wonder why this is a two-tier model, but I’ve seen that there are several layers of weights that are the base model, and I’ve heard a lot of lectures calling this a two-tier model, but don’t worry, if you want to call it a three-tier model, call it a three-tier model. But in order for me to be consistent with the class I’m listening to I’m going to call it the two-tier model.

import torch
from torch import nn
from d2l import torch as d2l
Copy the code
net = nn.Sequential(nn.Flatten(),
                    nn.Linear(784.256),
                    nn.ReLU(),
                    nn.Linear(256.10))

def init_weights(m) :
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)

net.apply(init_weights);
Copy the code
  • net =
    • To set the model, expand the input first and transform it into a vector
    • The input layer to the hidden layer is 784→256
    • Set ReLU layer activation function
    • Set the hidden layer to the output layer
  • init_weightsInitialize weights for each layer
  • willinit_weightsApply to NET.
batch_size, lr, num_epochs = 256.0.1.10
loss = nn.CrossEntropyLoss()
trainer = torch.optim.SGD(net.parameters(), lr=lr)
Copy the code
  • Here, batch_size is set to 256 for mini-batch, LR to 0.1 for learning-rate, and num_epochs to 10 for iteration
  • The training process also uses SGD directly from the framework
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
Copy the code

The user warning mentioned in manually implementing SoftMax regression appears in this code. I can just ignore it. This problem has been with me since the manual implementation of SoftMax section, but I just ignored it. Would like to know how to solve warning and we can see here: torchvision. Transforms. ToTensor explanation. | use transforms ToTensor () a user warning – the nuggets (juejin. Cn)

The training process was implemented exactly the same as when we implemented SoftMax regression, and this modular design allowed us to isolate the content related to the model architecture.

Read more here: Hands-on Deep Learning – LolitaAnn’s column – Nuggets (juejin. Cn)