《手动学习深度学习》3.2和3.3的代码对比

3.2 线性回归的从零开始

这是我的第一个代码,也算是属于自己的hello world了,特此纪念,希望继续努力。
代码中引入了3.1中的计时模块,用来对比训练时间。

  import random
  import torch
  from d2l import torch as d2l
  import  sys
  sys.path.append("..")
  from timer import Timer

  # 定时器计时
  timer = Timer()

  # 生成数据集
  def synt_data(w, b, num):
      X = torch.normal(0, 1, (num, len(w)))
      y = torch.matmul(X, w) + b
      y += torch.normal(0, 0.01, y.shape)
      return X, y.reshape((-1, 1))

  true_w = torch.tensor([2, -3.4])
  true_b = 4.2
  features, labels = synt_data(true_w, true_b, 1000)

  # d2l.set_figsize()
  # d2l.plt.scatter(features[: , 0].detach().numpy(), labels.detach().numpy(), 1)
  # d2l.plt.show()

  # 读取数据集
  def data_iter(batch_size, features, labels):
      num = len(features)
      # 打乱下标
      indices  = list(range(num))
      random.shuffle(indices)
      for i in range(0, num, batch_size):
          # 每次获取10个数据作为一个batch
          batch_indices = torch.tensor(indices[i: min(i + batch_size, num)])
          # 获取数据
          yield features[batch_indices], labels[batch_indices]

  batch_size = 10

  # for X, y in data_iter(batch_size, features, labels):
  #     print(X, '\n', y)
  #     break

  # 初始化模型参数
  w = torch.normal(0, 0.01, (2,1), requires_grad=True)
  b = torch.zeros(1, requires_grad=True)

  # 定义模型
  def linreg(X, w, b):
      return torch.matmul(X, w) + b

  #损失函数
  def squ_loss(y_hat, y):
      return (y_hat - y.reshape(y_hat.shape))**2 / 2

  # 定义优化算法,简单梯度下降
  def sgd(params, lr, batch_size):
      with torch.no_grad():
          for param in params:
              param -= lr * param.grad /batch_size
              param.grad.zero_()

  # 训练
  lr  = 0.03
  num_epochs = 3
  net = linreg
  loss = squ_loss

  for epoch in range(num_epochs):
      for X, y in data_iter(batch_size, features, labels):
          l = loss(net(X, w ,b), y)
          l.sum().backward()
          sgd([w,b], lr, batch_size)
      with torch.no_grad():
          train_l = loss(net(features, w, b), labels)
          print(f'epoch {epoch + 1 }, loss {float(train_l.mean()):f}')

  print(f'time {timer.stop(): .5f} sec')

3.3 线性回归的简单实现

这段代码敲的时候有几个有趣的发现:

  1. 手动写的梯度下降函数里面,有自动的梯度清零和参数更新,但是torch实现的SGD应该是没有的,所以才需要使用trainer.zero_grad()trainer.step()
  2. yeild的使用可以实现迭代器一样的效果
  import numpy as np
  import torch
  from torch.utils import data
  from torch import nn
  from d2l import torch as d2l
  import  sys
  sys.path.append("..")
  from timer import Timer

  # 定时器计时
  timer = Timer()

  # 生成数据集
  true_w = torch.tensor([2, -3.4])
  true_b = 4.2
  features, labels = d2l.synthetic_data(true_w, true_b, 1000)

  # 读取数据集
  def load_array(data_arrays, batch_size, is_train = True):
      # 构建一个迭代器
      dataset = data.TensorDataset(*data_arrays)
      return data.DataLoader(dataset, batch_size, shuffle=is_train)

  batch_size = 10
  data_iter = load_array((features, labels), batch_size)

  # 定义模型
  net = nn.Sequential(nn.Linear(2, 1))

  # 定义参数
  net[0].weight.data.normal_(0, 0.01)
  net[0].bias.data.fill_(0)

  # 定义损失函数
  loss  = nn.MSELoss()

  # 定义优化算法
  trainer = torch.optim.SGD(net.parameters(), lr = 0.03)

  # 训练
  num_epoch = 3
  for epoch in range(num_epoch):
      for X, y in data_iter:
          l = loss(net(X), y)
          trainer.zero_grad()
          l.backward()
          trainer.step()
      l = loss(net(features), labels)
      print(f'epoch {epoch + 1}, loss {l:f}')

  print(f'time {timer.stop(): .5f} sec')

对比

两个代码的运行结果分别为:

  (d2l) z**@e****:~/deeplearning/linear_regression$ ***/miniconda3/envs/d2l/bin/python ***/deeplearning/linear_regression/model_simple.py
  epoch 1, loss 0.000301
  epoch 2, loss 0.000114
  epoch 3, loss 0.000114
  time  0.16474 sec
  (d2l) z**@e****:~/deeplearning/linear_regression$ ***/miniconda3/envs/d2l/bin/python ***/deeplearning/linear_regression/model.py
  epoch 1, loss 0.028095
  epoch 2, loss 0.000099
  epoch 3, loss 0.000052
  time  0.13666 sec

可以看到,针对于简单的线性回归而言,手动写的代码无论是最终的精度还是时间上都是更优的,搜索到的可能的原因是:使用现有的机器学习框架可能会带来一些开销,例如框架本身的启动时间、内存占用等,手动编写的代码可以避免这些开销。

以上就是全部的内容了,由于作者刚开始学习,能力浅薄,待我学成归来,也许会有更深的了解。

posted @ 2024-03-02 11:30  ZCry  阅读(43)  评论(0)    收藏  举报