当前位置: 首页 > news >正文

小白的进阶之路系列之十四----人工智能从初步到精通pytorch综合运用的讲解第七部分

通过示例学习PyTorch

本教程通过独立的示例介绍PyTorch的基本概念。

PyTorch的核心提供了两个主要特性:

  • 一个n维张量,类似于numpy,但可以在gpu上运行

  • 用于构建和训练神经网络的自动微分

我们将使用一个三阶多项式来拟合问题 y = s i n ( x ) y=sin(x) y=sin(x),作为我们的例子。网络将有四个参数,并将通过最小化网络输出与真实输出之间的欧几里得距离来训练梯度下降以拟合随机数据。

张量

热身:numpy

在介绍PyTorch之前,我们将首先使用numpy实现网络。

Numpy提供了一个n维数组对象,以及许多用于操作这些数组的函数。Numpy是科学计算的通用框架;它对计算图、深度学习或梯度一无所知。然而,我们可以很容易地使用numpy来拟合正弦函数的三阶多项式,通过使用numpy操作手动实现网络的向前和向后传递:

# -*- coding: utf-8 -*-
import numpy as np
import math# Create random input and output data
x = np.linspace(-math.pi, math.pi, 2000)
y = np.sin(x)# Randomly initialize weights
a = np.random.randn()
b = np.random.randn()
c = np.random.randn()
d = np.random.randn()learning_rate = 1e-6
for t in range(2000):# Forward pass: compute predicted y# y = a + b x + c x^2 + d x^3y_pred = a + b * x + c * x ** 2 + d * x ** 3# Compute and print lossloss = np.square(y_pred - y).sum()if t % 100 == 99:print(t, loss)# Backprop to compute gradients of a, b, c, d with respect to lossgrad_y_pred = 2.0 * (y_pred - y)grad_a = grad_y_pred.sum()grad_b = (grad_y_pred * x).sum()grad_c = (grad_y_pred * x ** 2).sum()grad_d = (grad_y_pred * x ** 3).sum()# Update weightsa -= learning_rate * grad_ab -= learning_rate * grad_bc -= learning_rate * grad_cd -= learning_rate * grad_dprint(f'Result: y = {a} + {b} x + {c} x^2 + {d} x^3')

输出为:

99 245.74528761103798
199 173.39611738368987
299 123.2440921752332
399 88.44419578924933
499 64.27442600584153
599 47.47246235496093
699 35.78209704113
799 27.641356008462985
899 21.967808007623955
999 18.010612761588764
1099 15.248450766474248
1199 13.319031005970338
1299 11.970356120179034
1399 11.026994992046731
1499 10.366717947337667
1599 9.904294863860954
1699 9.58024957389446
1799 9.353047171709575
1899 9.193661409658082
1999 9.081793826477744
Result: y = -0.016362745280289488 + 0.8518166048235671 x + 0.0028228458381066635 x^2 + -0.09262995903014938 x^3

PyTorch:张量

Numpy是一个很好的框架,但是它不能利用gpu来加速它的数值计算。对于现代深度神经网络,gpu通常提供50倍或更高的速度,因此不幸的是,numpy不足以用于现代深度学习。

这里我们介绍PyTorch最基本的概念:张量(Tensor)。PyTorch张量在概念上与numpy数组相同:张量是一个n维数组,PyTorch提供了许多函数来操作这些张量。在幕后,张量可以跟踪计算图和梯度,但它们作为科学计算的通用工具也很有用。

与numpy不同的是,PyTorch张量可以利用gpu来加速它们的数值计算。要在GPU上运行PyTorch Tensor,你只需要指定正确的设备。

这里我们使用PyTorch张量来拟合正弦函数的三阶多项式。像上面的numpy示例一样,我们需要手动实现网络中的正向和反向传递:

# -*- coding: utf-8 -*-import torch
import mathdtype = torch.float
# device = torch.device("cpu")
device = torch.device("cuda:0") # Uncomment this to run on GPU# Create random input and output data
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)# Randomly initialize weights
a = torch.randn((), device=device, dtype=dtype)
b = torch.randn((), device=device, dtype=dtype)
c = torch.randn((), device=device, dtype=dtype)
d = torch.randn((), device=device, dtype=dtype)learning_rate = 1e-6
for t in range(2000):# Forward pass: compute predicted yy_pred = a + b * x + c * x ** 2 + d * x ** 3# Compute and print lossloss = (y_pred - y).pow(2).sum().item()if t % 100 == 99:print(t, loss)# Backprop to compute gradients of a, b, c, d with respect to lossgrad_y_pred = 2.0 * (y_pred - y)grad_a = grad_y_pred.sum()grad_b = (grad_y_pred * x).sum()grad_c = (grad_y_pred * x ** 2).sum()grad_d = (grad_y_pred * x ** 3).sum()# Update weights using gradient descenta -= learning_rate * grad_ab -= learning_rate * grad_bc -= learning_rate * grad_cd -= learning_rate * grad_dprint(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')

输出为:

99 80.36772918701172
199 56.39781951904297
299 40.46946716308594
399 29.881393432617188
499 22.840898513793945
599 18.157623291015625
699 15.041072845458984
799 12.966400146484375
899 11.584661483764648
999 10.664037704467773
1099 10.050336837768555
1199 9.641047477722168
1299 9.367938041687012
1399 9.185595512390137
1499 9.063775062561035
1599 8.982349395751953
1699 8.92789077758789
1799 8.89144515991211
1899 8.867034912109375
1999 8.85067367553711
Result: y = 0.0030112862586975098 + 0.8616413474082947 x + -0.0005194980767555535 x^2 + -0.09402744472026825 x^3

Autograd

PyTorch:张量和自梯度

在上面的例子中,我们必须手动实现神经网络的向前和向后传递。对于一个小型的两层网络来说,手动实现向后传递并不是什么大问题,但对于大型复杂网络来说,可能很快就会变得非常棘手。

值得庆幸的是,我们可以使用自动微分来自动计算神经网络中的逆向传递。PyTorch中的autograd包提供了这个功能。当使用autograd时,网络的前向传递将定义一个计算图;图中的节点将是张量

http://www.lqws.cn/news/134641.html

相关文章:

  • 《动手深度学习》8.2文本预处理—代码分析
  • linux 故障处置通用流程-36计+1计
  • [C]extern声明变量报错:undefined reference终极解决方案
  • 图论水题2
  • 基于 qiankun + vite + vue3 构建微前端应用实践
  • 高防CDN有用吗?它的防护效果怎么样?
  • ComfyUI一键画风转换:爆火吉普力画风一键转绘
  • 区块链+AI融合实战:智能合约如何结合机器学习优化DeFi风控?
  • JavaWeb:前后端分离开发-部门管理
  • 如何搭建Z-Blog PHP版本:详细指南
  • 关于项目多语言化任务的概述
  • 一文读懂RAG流程中用到的请求参数与返回字段
  • 【Linux】Linux权限
  • matlab模糊控制实现路径规划
  • 函数调用(Function Calling)
  • Markdown基础(1.2w字)
  • 本地日记本,用于记录日常。
  • k8s热更新-subPath 不支持热更新
  • 损失函数L对全连接层W、X、b的梯度
  • 【机器人编程基础】循环语句for-while
  • 字符串Base64编码经历了什么
  • 压测软件-Jmeter
  • 【Pandas】pandas DataFrame sample
  • 机器学习的数学基础:假设检验
  • 从上下文学习和微调看语言模型的泛化:一项对照研究
  • Linux系统iptables防火墙实验拓补
  • WES7系统深度定制全流程详解(从界面剥离到工业部署)
  • 【python】运行python程序的方式
  • 数据湖是什么?数据湖和数据仓库的区别是什么?
  • 不同视角理解三维旋转