当前位置:网站首页>怎么搭建深度学习框架?
怎么搭建深度学习框架?
2022-06-21 08:06:00 【AI卡莎】
当前深度学习框架越来越成熟,对于使用者而言封装程度越来越高,好处就是现在可以非常快速地将这些框架作为工具使用,用非常少的代码就可以进行实验,坏处就是可能背后地实现都被隐藏起来了。在这篇文章里笔者将带大家一起从头设计和实现一个轻量级的(大约 200 行)、易于扩展的深度学习框架 tinynn,希望对大家了解深度学习框架的基本设计和实现有一定的帮助。
本文首先会从深度学习的流程开始分析,对神经网络中的关键组件抽象,确定基本框架;然后再对框架里各个组件进行代码实现;最后基于这个框架实现了一个 MNIST 分类的示例。
组件抽象
首先考虑神经网络运算的流程,神经网络运算主要包含训练 training 和预测 predict (或 inference) 两个阶段,
训练的基本流程是:输入数据 -> 网络层前向传播 -> 计算损失 -> 网络层反向传播梯度 -> 更新参数;
预测的基本流程是 输入数据 -> 网络层前向传播 -> 输出结果。

从运算的角度看,主要可以分为三种类型的计算:
数据在网络层直接的流动
前向传播和反向传播可以看做是张量 Tensor(多维数组)在网络层之间的流动(前向传播流动的是输入输出,反向传播流动的是梯度),每个网络层会进行一定的运算,然后将结果输入给下一层。
计算损失
衔接前向和反向传播的中间过程,定义了模型的输出与真实值之间的差异,用来后续提供反向传播所需的信息
参数更新
使用计算得到的梯度对网络参数进行更新的一类计算
基于这个三种类型,我们可以对网络的基本组件做一个抽象
tensor 张量,这个是神经网络中数据的基本单位
layer 网络层,负责接收上一层的输入,进行该层的运算,将结果输出给下一层,由于 tensor 的流动有前向和反向两个方向,因此对于每种类型网络层我们都需要同时实现 forward 和 backward 两种运算
loss 损失,在给定模型预测值与真实值之后,该组件输出损失值以及关于最后一层的梯度(用于梯度回传)
optimizer 优化器,负责使用梯度更新模型的参数
然后我们还需要一些组件把上面这个 4 种基本组件整合到一起,形成一个 pipeline
net 组件负责管理 tensor 在 layer 之间的前向和反向传播,同时能提供获取参数、设置参数、获取梯度的接口
model 组件负责整合所有组件,形成整个 pipeline。即 net 组件进行前向传播 -> loss 组件计算损失和梯度 -> net 组件将梯度反向传播 -> optimizer 组件将梯度更新到参数。
免费分享一些我整理的人工智能学习资料给大家,包括一些AI常用框架实战视频、图像识别、OpenCV、NLQ、机器学习、pytorch、计算机视觉、深度学习与神经网络等视频、课件源码、国内外知名精华资源、AI热门论文、行业报告等。
为了更好的系统学习AI,推荐大家收藏一份。
下面展示部分截图,点击此处免费下载文中资料。
一、人工智能课程及项目

二、国内外知名精华资源

三、人工智能论文合集

四、人工智能行业报告

学好人工智能,要多看书,多动手,多实践,要想提高自己的水平,一定要学会沉下心来慢慢的系统学习,最终才能有所收获。
有需要的小伙伴,点击此处免费下载文中资料。
边栏推荐
- 如何使用lerna进行多包(package)管理
- Yunkang group passed the hearing: the revenue exceeded 1billion in 8 months and the profit within the period was 270Million
- Practical application cases of digital Twins - coal mine
- 升级Jenkins步骤和遇到的问题
- 图解 Google V8 # 15:隐藏类:如何在内存中快速查找对象属性?
- (for line breaks) differences between gets and fgets, and between puts and fputs
- 2021-06-17 STM32F103 USART串口代码 使用固件库
- 1005 Spell It Right (20 分)(测试点3)
- How to view the MySQL installation path
- 33 Jenkins modify plug-in source
猜你喜欢

33 Jenkins modify plug-in source

Matlab 3D diagram (unconventional)

2021-06-18 STM32F103 DMA and DMA serial port code using firmware library

You can't use typescript generics after reading it. Come to me for yyds dry inventory

Rdkit | molecular similarity based on molecular fingerprint

JVM内存模型概念

How to exit quickly if the application is stuck?

2021-06-16 STM32F103 EXTI 中断识别 使用固件库

How to write the statement of executing stored procedure in MySQL

使用Lua+Redis+OpenResty实现电商首页并发优化
随机推荐
Blank screen of virtual machine browser
2022-2028 global internal gear motor industry research and trend analysis report
Scientific research information | national natural conclusion regulations: more than 50% of the fund balance or it will not be concluded
Construct URL and Base64 download in the form of binary stream for file download
使用Lua+Redis+OpenResty实现电商首页并发优化
/home/ljx/miniconda3/compiler_ compat/ld: cannot find crtbeginS. o: There is no such file or directory
Rdkit | molecular similarity based on molecular fingerprint
数字孪生实际应用案例-煤矿篇
文件下载 二进制流的形式构造url和base64下载
Haidilao is expected to have an annual net loss of 3.8 billion to 4.5 billion and close more than 300 stores
You can't use typescript generics after reading it. Come to me for yyds dry inventory
Yyds dry goods inventory junit5 learning 3: assertions class
2021-06-16 STM32F103 EXTI 中断识别 使用固件库
Detailed explanation of deep learning technology for building an image search engine that can find similar images
测试入门——软件测试模型
2022-2028 global after sales spark plug industry research and trend analysis report
Multiplication and addition of univariate polynomial (20 points)
2021-07-28 STM32F103配置信息
Global and Chinese market for online automatic optical inspection 2022-2028: Research Report on technology, participants, trends, market size and share
Three declaration methods of structure type