多层感知机¶
Note
多层感知机(Multilayer Perceptron,MLP)就是多个全连接层堆叠起来的神经网络
定义模型¶
import torch
from torch import nn
import d2l
# 双层神经网络
net = nn.Sequential(nn.Flatten(),
nn.Linear(784, 256),
nn.ReLU(),
nn.Linear(256, 10))
def init_weights(m):
"""initialize at random"""
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)
# 参数初始化
net.apply(init_weights)
Sequential(
(0): Flatten(start_dim=1, end_dim=-1)
(1): Linear(in_features=784, out_features=256, bias=True)
(2): ReLU()
(3): Linear(in_features=256, out_features=10, bias=True)
)
训练¶
# 获取数据
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)
# 训练
lr, num_epochs = 0.01, 10
d2l.train_image_classifier(net, train_iter, test_iter, lr, num_epochs)
loss 0.272, train acc 0.897333, test acc 0.866800