zoukankan      html  css  js  c++  java
  • 【李沐】动手学深度学习-pytorch 2021版 从零开始实现softmax回归

    一、从零开始实现

    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 + '
    ' + 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)

  • 相关阅读:
    关于重构之Switch的处理【二】
    转,有用
    C#枚举总结和其扩展用法(通过枚举描设置枚举值)
    DataGridView编辑状态和CurrentCellDirtyStateChanged
    C#获取DataGirdView选定子项子控件类型,is,as的用法
    VS2013添加作者注释(C#工程)
    开通博客了 zkh
    CentOS7下在线安装mysql zkh
    设计模式之简单工厂,工厂方法,抽象工厂模式(二) zkh
    asp.net mvc中捕获异常和使用log4net日志记录组件 zkh
  • 原文地址:https://www.cnblogs.com/blzm742624643/p/15079086.html
Copyright © 2011-2022 走看看