# 多层感知机的简洁实现

I think the semicolon in the last line of 4.3.1 Model is unnecessary.

Tanh:

Relu：

Sigmoid

Sigmoid看上去更好，波动小，准确率高。

1 Like

https://pytorch.org/docs/stable/generated/torch.nn.Linear.html#torch.nn.Linear

1 Like

loss=nn.CrossEntropyLoss(reduction=‘none’)

1 Like

``````import torch
from torch import nn
from d2l import torch as d2l
def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater):
"""Train a model (defined in Chapter 3).

Defined in :numref:`sec_softmax_scratch`"""
animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1.0],
legend=['train loss', 'train acc', 'test acc'])
for epoch in range(num_epochs):
train_metrics = d2l.train_epoch_ch3(net, train_iter, loss, updater)
test_acc = d2l.evaluate_accuracy(net, test_iter)
animator.add(epoch + 1, train_metrics + (test_acc,))
train_loss, train_acc = train_metrics
assert train_loss < 0.5, train_loss
assert train_acc <= 1 and train_acc > 0.7, train_acc
assert test_acc <= 1 and test_acc > 0.7, test_acc

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

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

net.apply(init_weights);
batch_size, lr, num_epochs = 256, 0.1, 10

loss = nn.CrossEntropyLoss(reduction='none')

trainer = torch.optim.SGD(net.parameters(), lr)

train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

``````

``````def init_weights(m):
if type(m) == nn.Linear:
nn.init.uniform_(m.weight,-1.0,1.0)
nn.init.zeros_(m.bias)
#nn.init.normal_(m.weight, std=0.01)
``````

batch_size, lr, num_epochs = 256, 0.2, 40

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

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

net.apply(init_weights)
batch_size, lr, num_epochs = 256, 0.2, 40
loss = nn.CrossEntropyLoss(reduction='none')
trainer = torch.optim.SGD(net.parameters(), lr=lr)