多层感知机的简洁实现

通过高级API更简洁地实现多层感知机

In [1]:
import torch
from torch import nn
from d2l import torch as d2l

隐藏层 包含256个隐藏单元,并使用了ReLU激活函数

In [2]:
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);

训练过程

In [4]:
batch_size, lr, num_epochs = 256, 0.1, 10
loss = nn.CrossEntropyLoss()
trainer = torch.optim.SGD(net.parameters(), lr=lr)

train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
2021-05-05T19:29:27.051250 image/svg+xml Matplotlib v3.3.4, https://matplotlib.org/