import torch
from torch import nn
from d2l import torch as d2lbatch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
3.7.1 初始化模型参数
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);
3.7.2 重新审视Softmax的实现
loss = nn.CrossEntropyLoss(reduction='none')
3.7.3 优化算法
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
3.7.4 训练
num_epochs = 10
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

3.7.5 预测
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)def predict_ch3(net, test_iter, n=6): """Predict labels (defined in Chapter 3)."""for X, y in test_iter: breaktrues = d2l.get_fashion_mnist_labels(y) preds = d2l.get_fashion_mnist_labels(d2l.argmax(net(X), axis=1)) titles = [true +'\n' + pred for true, pred in zip(trues, preds)] d2l.show_images(d2l.reshape(X[0:n], (n, 28, 28)), 1, n, titles=titles[0:n]) predict_ch3(net, test_iter)
