当前位置: 首页 > 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.xdnf.cn/news/871669.html

相关文章:

  • Delphi中实现批量插入数据
  • 5分钟了解,Mysql事务事务隔离级别
  • tensorflow image_dataset_from_directory 训练数据集构建
  • 使用 Python 的 psutil 库进行系统资源监控
  • Webpack搭建本地服务器
  • Unity3D 逻辑代码性能优化策略
  • Linux kill 暂停命令
  • 跟着deepseek浅学分布式事务(2) - 两阶段提交(2PC)
  • 人工智能:网络安全的“智能守护者”
  • 录制mp4
  • Kali Linux 安全工具解析
  • Ros(控制机器人运动)
  • .NET 原生驾驭 AI 新基建实战系列(四):Qdrant ── 实时高效的向量搜索利器
  • JVMTI 在安卓逆向工程中的应用
  • 【Oracle】存储过程
  • 纹理压缩格式优化
  • OpenCV 自带颜色表实现各种滤镜
  • 【Netty源码分析总结】
  • 使用 Unstructured 开源库快速入门指南
  • 机器学习:聚类算法
  • Python爬虫:trafilatura 的详细使用(快速提取正文和评论以及结构,转换为 TXT、CSV 和 XML)
  • Day44打卡 @浙大疏锦行
  • 01-Redis介绍与安装
  • pyqt5 安装失败
  • 查看服务应用是否有跑起来命令
  • 机器学习算法分类
  • 3D旋转动态爱心 - Python创意代码
  • MySQL ACID 面试深度解析:原理、实现与面试实战
  • Pytest+Selenium UI自动化测试实战实例
  • MCP:AI应用的通用接口,如何重塑大模型与外部系统的连接?