本方法参考自《动手学深度学习》(Pytorch版)github项目
从零搭建 Softmax 回归有三个关键点
Softmax 方法实现
def softmax(x):
x_exp = x.exp() # m * n
partition = x_exp.sum(dim=1, keepdim=True) # 按列累加, m * 1
return x_exp / partition # 广播机制, [m * n] / [m * 1] = [m * n]
交叉熵实现
def cross_entropy(y_hat, y):
return - torch.log(y_hat.gather(1, y.view(-1, 1)))
精度评估
def accuracy(y_hat, y):
return (y_hat.argmax(dim=1) == y).float().mean().item()
完整代码
import torch
import torchvision
import numpy as np
import d2lzh_pytorch as d2l
batch_size = 256
# 数据获取
print('load fashion-mnist start...')
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# 模型参数初始化
num_inputs = 28 * 28 # 784
num_outputs = 10
w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_outputs)), dtype=torch.float32)
b = torch.zeros(num_outputs, dtype=torch.float32)
w.requires_grad_(True)
b.requires_grad_(True)
# 定义模型
def softmax(X):
X_exp = X.exp()
partition = X_exp.sum(dim=1, keepdim=True)
return X_exp / partition
def net(X):
return softmax(torch.mm(X.view(-1, num_inputs), w) + b)
# 定义损失函数
def cross_entropy(y_hat, y):
return - torch.log(y_hat.gather(1, y.view(-1, 1)))
# 定义精度评估函数
def accuracy(y_hat, y):
return (y_hat.argmax(dim=1) == y).float().mean().item()
def evaluate_accuracy(data_iter, net):
acc_sum, n = 0.0, 0
for X, y in data_iter:
y_hat = net(X)
acc = (y_hat.argmax(dim=1) == y).float().sum().item()
acc_sum += acc
n += y.shape[0]
return acc_sum / n
# 训练模型
num_epochs = 5
lr = 0.1
def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params=None, lr=None, optimizer=None):
for epo in range(1, num_epochs + 1):
train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
for X, y in train_iter:
y_hat = net(X)
l = loss(y_hat, y).sum()
# 梯度清零
if optimizer is not None:
optimizer.zero_grad()
elif params is not None and params[0].grad is not None:
for param in params:
param.grad.data.zero_()
l.backward()
# 梯度下降
if optimizer is not None:
optimizer.step()
else:
d2l.sgd(params, lr, batch_size)
train_l_sum += l.item()
train_acc_sum += (y_hat.argmax(dim=1) == y).float().sum().item()
n += y.shape[0]
test_acc = evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'
% (epo, train_l_sum / n, train_acc_sum / n, test_acc))
train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, [w, b], lr)
import torch
import torch.nn as nn
import torch.nn.init as init
import d2lzh_pytorch as d2l
from collections import OrderedDict
import torch.optim as optim
# 构建数据
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# 构建网络
class FlattenLayer(nn.Module):
def __init__(self):
super(FlattenLayer, self).__init__()
def forward(self, x): # transform (batch, c, h, w) to (batch, c * h * w)
return x.view(x.shape[0], -1)
class LinearNet(nn.Module):
def __init__(self, n_input, n_output):
super(LinearNet, self).__init__()
self.flatten = FlattenLayer()
self.linear = nn.Linear(n_input, n_output)
def forward(self, x):
return self.linear(self.flatten(x))
num_features = 28 * 28 # 784
num_labels = 10
net = LinearNet(num_features, num_labels)
# 等价于用 Sequential 创建网络
net2 = nn.Sequential(
OrderedDict([
('flatten', FlattenLayer()),
('linear', nn.Linear(num_features, num_labels))
])
)
print(net)
print(net2)
# 参数初始化
init.normal_(net.linear.weight, mean=0, std=0.01)
init.constant_(net.linear.bias, val=0)
# 损失函数
loss = nn.CrossEntropyLoss()
# 下降器
optimizer = optim.SGD(net.parameters(), lr=0.1)
# 模型训练
num_epochs = 5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, optimizer=optimizer)