当前位置:   article > 正文

写给程序员的机器学习入门 (二) - pytorch 与矩阵计算入门

写给程序员的机器学习入门 (二) - pytorch 与矩阵计算入门

人工智能学习离不开实践的验证,推荐大家可以多在FlyAI-AI竞赛服务平台多参加训练和竞赛,以此来提升自己的能力。FlyAI是为AI开发者提供数据竞赛并支持GPU离线训练的一站式服务平台。每周免费提供项目开源算法样例,支持算法能力变现以及快速的迭代算法模型。

pytorch 简介

pytorch 是目前世界上最流行的两个机器学习框架的其中之一,与 tensoflow 并峙双雄。它提供了很多方便的功能,例如根据损失自动微分计算应该怎样调整参数,提供了一系列的数学函数封装,还提供了一系列现成的模型,以及把模型组合起来进行训练的框架。pytorch 的前身是 torch,基于 lua,而 pytorch 基于 python,虽然它基于 python 但底层完全由 c++ 编写,支持自动并列化计算和使用 GPU 加速运算,所以它的性能非常好。

传统的机器学习有的会像前一节的例子中全部手写,或者利用 numpy 类库减少一部分工作量,也有人会利用 scikit-learn (基于 numpy) 类库封装好的各种经典算法。pytorch 与 tensorflow 和传统机器学习不一样的是,它们把重点放在了组建类似人脑的神经元网络 (Neural Network),所以能实现传统机器学习无法做到的非常复杂的判断,例如判断图片中的物体类型,自动驾驶等。不过,它们组建的神经元网络工作方式是不是真的和人脑类似仍然有很多争议,目前已经有人开始着手组建原理上更接近人脑的 GNN (Graph Neural Network) 网络,但仍未实用化,所以我们这个系列还是会着重讲解当前已经实用化并广泛应用在各个行业的网络模型。

学 pytorch 还是学 tensorflow 好?

对初学者来说一个很常见的问题是,学 pytorch 还是学 tensorflow 好?按目前的统计数据来说,公司更多使用 tensorflow,而研究人员更多使用 pytorch,pytorch 的增长速度非常快,有超越 tensorflow 的趋势。我的意见是学哪个都无所谓,如果你熟悉 pytorch,学 tensorflow 也就一两天的事情,反过来也一样,并且 pytorch 和 tensorflow 的项目可以互相移植,选一个觉得好学的就可以了。因为我觉得 pytorch 更好学 (封装非常直观,使用 Dynamic Graph 使得调试非常容易),所以这个系列会基于 pytorch 来讲。

Dynamic Graph 与 Static Graph

机器学习框架按运算的流程是否需要预先固定可以分为 Dynamic Graph 和 Static Graph,Dynamic Graph 不需要预先固定运算流程,而 Static Graph 需要。举例来说,对同一个公式 wx + b = y,Dynamic Graph 型的框架可以把 wx+b 分开写并且逐步计算,计算的过程中随时都可以用 print 等指令输出途中的结果,或者把途中的结果发送到其他地方记录起来;而 Static Graph 型的框架必须预先定好整个计算流程,你只能传入 wxb 给计算器,然后让计算器输出 y,中途计算的结果只能使用专门的调试器来查看。

一般的来说 Static Graph 性能会比 Dynamic Graph 好,Tensorflow (老版本) 使用的是 Static Graph,而 pytorch 使用的是 Dynamic Graph,但两者实际性能相差很小,因为消耗资源的大部分都是矩阵运算,使用批次训练可以很大程度减少它们的差距。顺带一提,Tensorflow 1.7 开始支持了 Dynamic Graph,并且在 2.0 默认开启,但大部分人在使用 Tensorflow 的时候还是会用 Static Graph。

  1. # Dynamic Graph 的印象,运算的每一步都可以插入自定义代码
  2. def forward(w, x, b):
  3. wx = w * x
  4. print(wx)
  5. y = wx + b
  6. print(y)
  7. return y
  8. forward(w, x, b)
  9. # Static Graph 的印象,需要预先编译整个计算流程
  10. forward = compile("wx+b")
  11. forward(w, x, b)

安装 pytorch

假设你已经安装了 python3,执行以下命令即可安装 pytorch:

pip3 install pytorch

之后在 python 代码中使用 import torch 即可引用 pytorch 类库。

pytorch 的基本操作

接下来我们熟悉一下 pytorch 里面最基本的操作,pytorch 会用 torch.Tensor 类型来统一表现数值,向量 (一维数组) 或矩阵 (多维数组),模型的参数也会使用这个类型。(tensorflow 会根据用途分为好几个类型,这点 pytorch 更简洁明了)

torch.Tensor 类型可以使用 torch.tensor 函数构建,以下是一些简单的例子(运行在 python 的 REPL 中):

  1. # 引用 pytorch
  2. >>> import torch
  3. # 创建一个整数 tensor
  4. >>> torch.tensor(1)
  5. tensor(1)
  6. # 创建一个小数 tensor
  7. >>> torch.tensor(1.0)
  8. tensor(1.)
  9. # 单值 tensor 中的值可以用 item 函数取出
  10. >>> torch.tensor(1.0).item()
  11. 1.0
  12. # 使用一维数组创建一个向量 tensor
  13. >>> torch.tensor([1.0, 2.0, 3.0])
  14. tensor([1., 2., 3.])
  15. # 使用二维数组创建一个矩阵 tensor
  16. >>> torch.tensor([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]])
  17. tensor([[ 1., 2., 3.],
  18. [-1., -2., -3.]])

tensor 对象的数值类型可以看它的 dtype 成员:

  1. >>> torch.tensor(1).dtype
  2. torch.int64
  3. >>> torch.tensor(1.0).dtype
  4. torch.float32
  5. >>> torch.tensor([1.0, 2.0, 3.0]).dtype
  6. torch.float32
  7. >>> torch.tensor([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]).dtype
  8. torch.float32

pytorch 支持整数类型 torch.uint8torch.int8torch.int16torch.int32torch.int64 ,浮点数类型 torch.float16torch.float32torch.float64,还有布尔值类型 torch.bool。类型后的数字代表它的位数 (bit 数),而 uint8 前面的 u 代表它是无符号数 (unsigned)。实际绝大部分场景都只会使用 torch.float32,虽然精度没有 torch.float64 高但它占用内存小并且运算速度快。注意一个 tensor 对象里面只能保存一种类型的数值,不能混合存放。

创建 tensor 对象时可以通过 dtype 参数强制指定类型:

  1. >>> torch.tensor(1, dtype=torch.int32)
  2. tensor(1, dtype=torch.int32)
  3. >>> torch.tensor([1.1, 2.9, 3.5], dtype=torch.int32)
  4. tensor([1, 2, 3], dtype=torch.int32)
  5. >>> torch.tensor(1, dtype=torch.int64)
  6. tensor(1)
  7. >>> torch.tensor(1, dtype=torch.float32)
  8. tensor(1.)
  9. >>> torch.tensor(1, dtype=torch.float64)
  10. tensor(1., dtype=torch.float64)
  11. >>> torch.tensor([1, 2, 3], dtype=torch.float64)
  12. tensor([1., 2., 3.], dtype=torch.float64)
  13. >>> torch.tensor([1, 2, 0], dtype=torch.bool)
  14. tensor([ True, True, False])

tensor 对象的形状可以看它的 shape 成员:

  1. # 整数 tensor 的 shape 为空
  2. >>> torch.tensor(1).shape
  3. torch.Size([])
  4. >>> torch.tensor(1.0).shape
  5. torch.Size([])
  6. # 数组 tensor 的 shape 只有一个值,代表数组的长度
  7. >>> torch.tensor([1.0]).shape
  8. torch.Size([1])
  9. >>> torch.tensor([1.0, 2.0, 3.0]).shape
  10. torch.Size([3])
  11. # 矩阵 tensor 的 shape 根据它的维度而定,每个值代表各个维度的大小,这个例子代表矩阵有 23
  12. >>> torch.tensor([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]).shape
  13. torch.Size([2, 3])

tensor 对象与数值,tensor 对象与 tensor 对象之间可以进行运算:

  1. >>> torch.tensor(1.0) * 2
  2. tensor(2.)
  3. >>> torch.tensor(1.0) * torch.tensor(2.0)
  4. tensor(2.)
  5. >>> torch.tensor(3.0) * torch.tensor(2.0)
  6. tensor(6.)

向量和矩阵还可以批量进行运算(内部会并列化运算):

  1. # 向量和数值之间的运算
  2. >>> torch.tensor([1.0, 2.0, 3.0])
  3. tensor([1., 2., 3.])
  4. >>> torch.tensor([1.0, 2.0, 3.0]) * 3
  5. tensor([3., 6., 9.])
  6. >>> torch.tensor([1.0, 2.0, 3.0]) * 3 - 1
  7. tensor([2., 5., 8.])
  8. # 矩阵和单值 tensor 对象之间的运算
  9. >>> torch.tensor([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]])
  10. tensor([[ 1., 2., 3.],
  11. [-1., -2., -3.]])
  12. >>> torch.tensor([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]) / torch.tensor(2)
  13. tensor([[ 0.5000, 1.0000, 1.5000],
  14. [-0.5000, -1.0000, -1.5000]])
  15. # 矩阵和与矩阵最后一个维度相同长度向量之间的运算
  16. >>> torch.tensor([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]) * torch.tensor([1.0, 1.5, 2.0])
  17. tensor([[ 1., 3., 6.],
  18. [-1., -3., -6.]])

tensor 对象之间的运算一般都会生成一个新的 tensor 对象,如果你想避免生成新对象 (提高性能),可以使用 _ 结尾的函数,它们会修改原有的对象:

  1. # 生成新对象,原有对象不变,add+ 意义相同
  2. >>> a = torch.tensor([1,2,3])
  3. >>> b = torch.tensor([7,8,9])
  4. >>> a.add(b)
  5. tensor([ 8, 10, 12])
  6. >>> a
  7. tensor([1, 2, 3])
  8. # 在原有对象上执行操作,避免生成新对象
  9. >>> a.add_(b)
  10. tensor([ 8, 10, 12])
  11. >>> a
  12. tensor([ 8, 10, 12])

pytorch 还提供了一系列方便的函数求最大值,最小值,平均值,标准差等:

  1. >>> torch.tensor([1.0, 2.0, 3.0])
  2. tensor([1., 2., 3.])
  3. >>> torch.tensor([1.0, 2.0, 3.0]).min()
  4. tensor(1.)
  5. >>> torch.tensor([1.0, 2.0, 3.0]).max()
  6. tensor(3.)
  7. >>> torch.tensor([1.0, 2.0, 3.0]).mean()
  8. tensor(2.)
  9. >>> torch.tensor([1.0, 2.0, 3.0]).std()
  10. tensor(1.)

pytorch 还支持比较 tensor 对象来生成布尔值类型的 tensor:

  1. # tensor 对象与数值比较
  2. >>> torch.tensor([1.0, 2.0, 3.0]) > 1.0
  3. tensor([False, True, True])
  4. >>> torch.tensor([1.0, 2.0, 3.0]) <= 2.0
  5. tensor([ True, True, False])
  6. # tensor 对象与 tensor 对象比较
  7. >>> torch.tensor([1.0, 2.0, 3.0]) > torch.tensor([1.1, 1.9, 3.0])
  8. tensor([False, True, False])
  9. >>> torch.tensor([1.0, 2.0, 3.0]) <= torch.tensor([1.1, 1.9, 3.0])
  10. tensor([ True, False, True])

pytorch 还支持生成指定形状的 tensor 对象:

  1. # 生成 23 列的矩阵 tensor,值全部为 0
  2. >>> torch.zeros(2, 3)
  3. tensor([[0., 0., 0.],
  4. [0., 0., 0.]])
  5. # 生成 32 列的矩阵 tensor,值全部为 1
  6. torch.ones(3, 2)
  7. >>> torch.ones(3, 2)
  8. tensor([[1., 1.],
  9. [1., 1.],
  10. [1., 1.]])
  11. # 生成 32 列的矩阵 tensor,值全部为 100
  12. >>> torch.full((3, 2), 100)
  13. tensor([[100., 100.],
  14. [100., 100.],
  15. [100., 100.]])
  16. # 生成 33 列的矩阵 tensor,值为范围 [0, 1) 的随机浮点数
  17. >>> torch.rand(3, 3)
  18. tensor([[0.4012, 0.2412, 0.1532],
  19. [0.1178, 0.2319, 0.4056],
  20. [0.7879, 0.8318, 0.7452]])
  21. # 生成 33 列的矩阵 tensor,值为范围 [1, 10] 的随机整数
  22. >>> (torch.rand(3, 3) * 10 + 1).long()
  23. tensor([[ 8, 1, 5],
  24. [ 8, 6, 5],
  25. [ 1, 6, 10]])
  26. # 和上面的写法效果一样
  27. >>> torch.randint(1, 11, (3, 3))
  28. tensor([[7, 1, 3],
  29. [7, 9, 8],
  30. [4, 7, 3]])

这里提到的操作只是常用的一部分,如果你想了解更多 tensor 对象支持的操作,可以参考以下文档:

pytorch 保存 tensor 使用的数据结构

为了减少内存占用与提升访问速度,pytorch 会使用一块连续的储存空间 (不管是在系统内存还是在 GPU 内存中) 保存 tensor,不管 tensor 是数值,向量还是矩阵。

我们可以使用 storage 查看 tensor 对象使用的储存空间:

  1. # 数值的储存空间长度是 1
  2. >>> torch.tensor(1).storage()
  3. 1
  4. [torch.LongStorage of size 1]
  5. # 向量的储存空间长度等于向量的长度
  6. >>> torch.tensor([1, 2, 3], dtype=torch.float32).storage()
  7. 1.0
  8. 2.0
  9. 3.0
  10. [torch.FloatStorage of size 3]
  11. # 矩阵的储存空间长度等于所有维度相乘的结果,这里是 23 列总共 6 个元素
  12. >>> torch.tensor([[1, 2, 3], [-1, -2, -3]], dtype=torch.float64).storage()
  13. 1.0
  14. 2.0
  15. 3.0
  16. -1.0
  17. -2.0
  18. -3.0
  19. [torch.DoubleStorage of size 6]

pytorch 会使用 stride 来确定一个 tensor 对象的维度:

  1. # 储存空间有 6 个元素
  2. >>> torch.tensor([[1, 2, 3], [-1, -2, -3]]).storage()
  3. 1
  4. 2
  5. 3
  6. -1
  7. -2
  8. -3
  9. [torch.LongStorage of size 6]
  10. # 第一个维度是 2,第二个维度是 3 (23 列)
  11. >>> torch.tensor([[1, 2, 3], [-1, -2, -3]]).shape
  12. torch.Size([2, 3])
  13. # stride 的意义是表示每个维度之间元素的距离
  14. # 第一个维度会按 3 个元素来切分 (6 个元素可以切分成 2 组),第二个维度会按 1 个元素来切分 (3 个元素)
  15. >>> torch.tensor([[1, 2, 3], [-1, -2, -3]])
  16. tensor([[ 1, 2, 3],
  17. [-1, -2, -3]])
  18. >>> torch.tensor([[1, 2, 3], [-1, -2, -3]]).stride()
  19. (3, 1)

pytorch 的一个很强大的地方是,通过 view 函数可以修改 tensor 对象的维度 (内部改变了 stride),但是不需要创建新的储存空间并复制元素:

  1. # 创建一个 23 列的矩阵
  2. >>> a = torch.tensor([[1, 2, 3], [-1, -2, -3]])
  3. >>> a
  4. tensor([[ 1, 2, 3],
  5. [-1, -2, -3]])
  6. >>> a.shape
  7. torch.Size([2, 3])
  8. >>> a.stride()
  9. (3, 1)
  10. # 把维度改为 32
  11. >>> b = a.view(3, 2)
  12. >>> b
  13. tensor([[ 1, 2],
  14. [ 3, -1],
  15. [-2, -3]])
  16. >>> b.shape
  17. torch.Size([3, 2])
  18. >>> b.stride()
  19. (2, 1)
  20. # 转换为向量
  21. >>> c = b.view(6)
  22. >>> c
  23. tensor([ 1, 2, 3, -1, -2, -3])
  24. >>> c.shape
  25. torch.Size([6])
  26. >>> c.stride()
  27. (1,)
  28. # 它们的储存空间是一样的
  29. >>> a.storage()
  30. 1
  31. 2
  32. 3
  33. -1
  34. -2
  35. -3
  36. [torch.LongStorage of size 6]
  37. >>> b.storage()
  38. 1
  39. 2
  40. 3
  41. -1
  42. -2
  43. -3
  44. [torch.LongStorage of size 6]
  45. >>> c.storage()
  46. 1
  47. 2
  48. 3
  49. -1
  50. -2
  51. -3
  52. [torch.LongStorage of size 6]

使用 stride 确定维度的另一个意义是它可以支持共用同一个空间实现转置 (Transpose) 操作:

  1. # 创建一个 23 列的矩阵
  2. >>> a = torch.tensor([[1, 2, 3], [-1, -2, -3]])
  3. >>> a
  4. tensor([[ 1, 2, 3],
  5. [-1, -2, -3]])
  6. >>> a.shape
  7. torch.Size([2, 3])
  8. >>> a.stride()
  9. (3, 1)
  10. # 使用转置操作交换维度 (行转列)
  11. >>> b = a.transpose(0, 1)
  12. >>> b
  13. tensor([[ 1, -1],
  14. [ 2, -2],
  15. [ 3, -3]])
  16. >>> b.shape
  17. torch.Size([3, 2])
  18. >>> b.stride()
  19. (1, 3)
  20. # 它们的储存空间是一样的
  21. >>> a.storage()
  22. 1
  23. 2
  24. 3
  25. -1
  26. -2
  27. -3
  28. [torch.LongStorage of size 6]
  29. >>> b.storage()
  30. 1
  31. 2
  32. 3
  33. -1
  34. -2
  35. -3
  36. [torch.LongStorage of size 6]

转置操作内部就是交换了指定维度在 stride 中对应的值,你可以根据前面的描述想想对象在转置后的矩阵中会如何划分。

现在再想想,如果把转置后的矩阵用 view 函数专为向量会变为什么?会变为 [1, -1, 2, -2, 3, -3] 吗?

实际上这样的操作会导致出错

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/356132
推荐阅读
相关标签