【李沐】动手学习ai思路softmax回归实现

来源:https://www.cnblogs.com/blzm742624643/p/15079086.html

一、从零开始实现

1.1 首先引入Fashion-MNIST数据集

复制代码
1 import torch
2 from IPython import display
3 from d2l import torch as d2l
4 
5 batch_size = 256
6 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

1.2 初始化模型参数

原始图像中每个样本都是28*28的,所以要展平每个图像成长度为784的向量。

权重784*10,偏置1*10

复制代码
1 num_inputs = 784
2 num_outputs = 10
3 
4 W = torch.normal(0, 0.01, size=(num_inputs, num_outputs), requires_grad=True)
5 b = torch.zeros(num_outputs, requires_grad=True)

1.3 定义softmax操作

如果为0则留下一行,为1则留下一列

复制代码
X = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
X.sum(0, keepdim=True), X.sum(1, keepdim=True)
复制代码
1 def softmax(X):
2     X_exp = torch.exp(X)
3     partition = X_exp.sum(1, keepdim=True)
4     return X_exp / partition  # 这里应用了广播机制
复制代码
1 X = torch.normal(0, 1, (2, 5))
2 X_prob = softmax(X)
3 X_prob, X_prob.sum(1)

1.4 模型定义

-1 的地方为批次, W.shape[0]为输入的维度

复制代码
1 def net(X):
2     return softmax(torch.matmul(X.reshape((-1, W.shape[0])), W) + b)

1.5 损失函数

通过 y 来获取 y_hat 中的值

复制代码
1 y = torch.tensor([0, 2])
2 y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
3 y_hat[[0, 1], y]

学会了以上的操作我们就可以用一行来实现交叉熵损失函数

复制代码
def cross_entropy(y_hat, y):
    return -torch.log(y_hat[range(len(y_hat)), y])

cross_entropy(y_hat, y)

1.6 分类准确率

假设y_hat是一个矩阵,第二个维度存储每个类的预测分数。使用argmax获得每行中的最大元素。

复制代码
def accuracy(y_hat, y):  #@save
    """计算预测正确的数量。"""
    if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:
        y_hat = y_hat.argmax(axis=1)
    cmp = y_hat.type(y.dtype) == y
    return float(cmp.type(y.dtype).sum())

在评估模式的时候不计算梯度,只做前向传递

复制代码
1 def evaluate_accuracy(net, data_iter):  #@save
2     """计算在指定数据集上模型的精度。"""
3     if isinstance(net, torch.nn.Module):
4         net.eval()  # 将模型设置为评估模式
5     metric = Accumulator(2)  # 正确预测数、预测总数
6     for X, y in data_iter:
7         metric.add(accuracy(net(X), y), y.numel())
8     return metric[0] / metric[1]

关于用于对多个变量进行累加的Accumulator类的实现

复制代码
 1 class Accumulator:  #@save
 2     """在`n`个变量上累加。"""
 3     def __init__(self, n):
 4         self.data = [0.0] * n
 5 
 6     def add(self, *args):
 7         self.data = [a + float(b) for a, b in zip(self.data, args)]
 8 
 9     def reset(self):
10         self.data = [0.0] * len(self.data)
11 
12     def __getitem__(self, idx):
13         return self.data[idx]

由于随机权重初始化net模型,所以准确率近似于随机猜测

复制代码
1 evaluate_accuracy(net, test_iter)

1.7 训练

updater 是更新模型参数的常用函数,它接受批量大小作为参数。它可以是封装的d2l.sgd函数,也可以是框架的内置优化函数。

复制代码
def train_epoch_ch3(net, train_iter, loss, updater):  #@save
    """训练模型一个迭代周期(定义见第3章)。"""
    # 将模型设置为训练模式
    if isinstance(net, torch.nn.Module):
        net.train()
    # 训练损失总和、训练准确度总和、样本数
    metric = Accumulator(3)
    for X, y in train_iter:
        # 计算梯度并更新参数
        y_hat = net(X)
        l = loss(y_hat, y)
        if isinstance(updater, torch.optim.Optimizer):
            # 使用PyTorch内置的优化器和损失函数
            updater.zero_grad()
            # 计算梯度
            l.backward()  
            # 更新参数
            updater.step()
            metric.add(
                float(l) * len(y), accuracy(y_hat, y),
                y.size().numel())
        else:
            # 使用定制的优化器和损失函数
            l.sum().backward()
            updater(X.shape[0])
            metric.add(float(l.sum()), accuracy(y_hat, y), y.numel())
    # 返回训练损失和训练准确率
    return metric[0] / metric[2], metric[1] / metric[2]    

辅助函数

复制代码
class Animator:  #@save
    """在动画中绘制数据。"""
    def __init__(self, xlabel=None, ylabel=None, legend=None, xlim=None,
                 ylim=None, xscale='linear', yscale='linear',
                 fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1,
                 figsize=(3.5, 2.5)):
        # 增量地绘制多条线
        if legend is None:
            legend = []
        d2l.use_svg_display()
        self.fig, self.axes = d2l.plt.subplots(nrows, ncols, figsize=figsize)
        if nrows * ncols == 1:
            self.axes = [self.axes,]
        # 使用lambda函数捕获参数
        self.config_axes = lambda: d2l.set_axes(self.axes[
            0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
        self.X, self.Y, self.fmts = None, None, fmts

    def add(self, x, y):
        # 向图表中添加多个数据点
        if not hasattr(y, "__len__"):
            y = [y]
        n = len(y)
        if not hasattr(x, "__len__"):
            x = [x] * n
        if not self.X:
            self.X = [[] for _ in range(n)]
        if not self.Y:
            self.Y = [[] for _ in range(n)]
        for i, (a, b) in enumerate(zip(x, y)):
            if a is not None and b is not None:
                self.X[i].append(a)
                self.Y[i].append(b)
        self.axes[0].cla()
        for x, y, fmt in zip(self.X, self.Y, self.fmts):
            self.axes[0].plot(x, y, fmt)
        self.config_axes()
        display.display(self.fig)
        display.clear_output(wait=True)

进行num_epochs个迭代周期的训练,每个迭代周期结束利用test_iter访问到的测试数据集对模型进行评估。

复制代码
 1 def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater):  #@save
 2     """训练模型(定义见第3章)。"""
 3     animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
 4                         legend=['train loss', 'train acc', 'test acc'])
 5     for epoch in range(num_epochs):
 6         train_metrics = train_epoch_ch3(net, train_iter, loss, updater)
 7         test_acc = evaluate_accuracy(net, test_iter)
 8         animator.add(epoch + 1, train_metrics + (test_acc,))
 9     train_loss, train_acc = train_metrics
10     assert train_loss < 0.5, train_loss
11     assert train_acc <= 1 and train_acc > 0.7, train_acc
12     assert test_acc <= 1 and test_acc > 0.7, test_acc
复制代码
1 lr = 0.1
2 
3 def updater(batch_size):
4     return d2l.sgd([W, b], lr, batch_size)
5 
6 num_epochs = 10
7 train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, updater)

1.8 预测

复制代码
def predict_ch3(net, test_iter, n=6):  #@save
    """预测标签(定义见第3章)。"""
    # 拿出一个样本
    for X, y in test_iter:
        break
    trues = d2l.get_fashion_mnist_labels(y)
    preds = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1))
    titles = [true + '\n' + pred for true, pred in zip(trues, preds)]
    d2l.show_images(X[0:n].reshape((n, 28, 28)), 1, n, titles=titles[0:n])

predict_ch3(net, test_iter)
相关推荐
xingshanchang1 小时前
PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
人工智能·pytorch·python
昵称是6硬币3 小时前
YOLOv11: AN OVERVIEW OF THE KEY ARCHITECTURAL ENHANCEMENTS目标检测论文精读(逐段解析)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
费弗里4 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
学Linux的语莫9 天前
python基础语法
开发语言·python
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python
Ven%9 天前
PyTorch 张量(Tensors)全面指南:从基础到实战
人工智能·pytorch·python