- 本文转自我亲爱的队友@如若明镜
Torch简介
主要参考于官方文档:http://torch.ch/
什么是Torch?
Torch是一个广泛支持机器学习算法的科学计算框架,由于采用简单而快速的脚本语言LuaJIT和一个底层的C/CUDA实现,Torch易于使用且高效。
核心特性:
- 一个强大的N-维数组(a powerful N-dimensional array)
- 大量索引、切片、置换……例程(lots of routines for indexing, slicing, transposing, …)
- 惊人的通过LuaJIT到C的接口(amazing interface to C, via LuaJIT)
- 线性代数例程(linear algebra routines)
- 神经网络和基于能量的模型(neural network, and energy-based models)
- 数值优化例程(numeric optimization routines)
- 快速高效的GPU支持(Fast and efficient GPU support)
- 可嵌入的,提供到iOS,Android和FPGA后台的端口(Embeddable, with ports to iOS, Android and FPGA backends)
为什么选择Torch
Torch的目标是在建立科学算法的同时,要有最大的灵活性和速度,而这一过程非常简单。Torch拥有一个大社区驱动包的生态系统,涉及机器学习、计算机视觉、信号处理、并行处理、图像、视频、音频和网络等,并建立在Lua社区基础之上。
Torch的核心是流行的神经网络和优化库,它们易于使用,同时在实现复杂的神经网络拓扑结构时具有最大的灵活性。你可以建立任意的神经网络图,并在CPUs和GPUs上有效地并行化。
获得与使用Torch
你可以查看入门指南 Getting Started ,开始下载并尝试使用Torch。Torch是开源的,源码可以在GitHub repo上获得。
Torch是不断变化的:它已经用在Facebook, Google, Twitter, NYU, IDIAP, Purdue和其他几家公司和研究实验室。
Torch安装
Torch7
curl是利用URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在Unix、多种Linux发行版中,并且有DOS和Win32、Win64下的移植版本。获得页面使用命令:curl http://curl.haxx.se 这是最简单的使用方法。用这个命令获得了http://curl.haxx.se指向的页面,同样,如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地。可以使用 -v 命令看curl是怎样工作的.
Torch的安装相当简便,依次输入如下命令,即可完成安装,假设安装目录为:~/sfw/Torch
,请更改为自己的目录。
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 1.Install curl</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get install curl <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 2.Install the basic package dependencies that LuaJIT and Torch require.</span> curl <span class="hljs-operator" style="box-sizing: border-box;">-s</span> https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 3.Clone Torch to ~/sfw/torch, this will installs LuaJIT, LuaRocks, and then uses LuaRocks (the lua package manager) to install core packages like torch, nn and paths, as well as a few other packages.</span> git clone https://github.com/torch/distro.git ~/sfw/torch --recursive <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 4.Install Torch</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> ~/sfw/torch; ./install.sh <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 5.Refresh your env variables</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">source</span> ~/.bashrc</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
注意:
-
在执行第4步时,安装进入尾声时,终端会提示是否自动添加环境变量,选择是,即输入
y
或yes
,如果没有到这一步而退出,说明程序安装不正确,但即使到了这一步,也不能说明安装没问题,有可能有些依赖库未安装成功,具体看终端输出的信息:
-
第4步结束后,可以输入
gedit ~/.bashrc
打开该文件,看到文件末尾有这样一行代码:
. /home/liu/sfw/torch/install/bin/torch-activate
,说明环境变量添加成功,然后执行第5步,更新环境变量。
查看了“install-deps”文件,可见安装了OpenBLAS、build-essential、gcc、g++、curl、cmake、libreadline-dev、git-core、libqt4-core、libqt4-gui、libqt4-dev、libjpeg-dev、libpng-dev、ncurses-dev、imagemagick、libzmq3-dev、gfortran、unzip、gnuplot、gnuplot-x11、ipython依赖包。
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Install dependencies for Torch:</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [[ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$distribution</span> == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'ubuntu'</span> ]]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">declare</span> <span class="hljs-operator" style="box-sizing: border-box;">-a</span> target_pkgs target_pkgs=( build-essential gcc g++ curl \ cmake libreadline-dev git-core libqt4-core libqt4-gui \ libqt4-dev libjpeg-dev libpng-dev ncurses-dev \ imagemagick libzmq3-dev gfortran unzip gnuplot \ gnuplot-x11 ipython ) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get update <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># python-software-properties is required for apt-add-repository</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get install -y python-software-properties <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [[ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ubuntu_major_version</span> == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'14'</span> ]]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'==> Found Ubuntu version 14.xx, installing dependencies'</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get install -y software-properties-common \ libgraphicsmagick1-dev nodejs npm libfftw3-dev sox libsox-dev \ libsox-fmt-all <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> add-apt-repository -y ppa:jtaylor/ipython <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> add-apt-repository -y ppa:chris-lea/zeromq <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> add-apt-repository -y ppa:chris-lea/node.js <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get update <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get install -y <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${target_pkgs[@]}</span>"</span> install_openblas</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>
注意:对于依赖库ipython,torch默认安装2.3.0,如下图:
如果你之前已经安装了ipython并且版本为4(可以使用ipython --version
查询),可能在后面运行itorch notebook
后,在网页中打开的是“jupyter notebook”页面,而不是“itorch notebook”,而且无法新建notebook;不仅如此,你在安装上述依赖库时,会提示:“WARNING: Your ipython version is too old. Type “ipython –version” to see this. Should be at least version 2”,而实际上你的版本是4>2,原因可以从上述依赖库安装脚本文件中看出,如下。
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">ipython_exists=$(command -v ipython) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [[ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ipython_exists</span> ]]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> { ipython_version=$(ipython --version|cut <span class="hljs-operator" style="box-sizing: border-box;">-f</span>1 <span class="hljs-operator" style="box-sizing: border-box;">-d</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'.'</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [[ <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ipython_version</span> != <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> && <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ipython_version</span> != <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> ]]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'WARNING: Your ipython version is too old. Type "ipython --version" to see this. Should be at least version 2'</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
比如我先安装了caffe,其中的“requirements”文件中也有ipython,也需要ipython,caffe默认安装最新的4.0(目前为止),导致出现上述问题,后来输入sudo apt-get remove ipython
想卸载ipython,无果,最后傻傻地想卸了python2.7.6重装,谁知卸载后,系统就出问题了(Ubuntu 14.04 系统自带Python,且好多应用依赖于它),无奈只能重装。
其它包
Torch是一个工具包相当丰富的工具,安装Torch时会安装一部分扩展包,在”/torch/extra/”下可以看到,如果想安装其它工具包,可以通过Luarocks在终端以命令行形式安装新的包:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">luarocks install image luarocks list</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
加速
如果你想使用CUDA、cuDNN加速,你还需要安装CUDA和cuDNN,可参见本人博客NVIDIA DIGITS 学习笔记(NVIDIA DIGITS-2.0 + Ubuntu 14.04 + CUDA 7.0 + cuDNN 7.0 + Caffe 0.13.0)。
Torch卸载
如果想卸载Torch,使用如下命令,注意更改目录:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">rm -rf ~/sfw/torch</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
Torch使用
概览Torch
所有关于Torch的手册资料清单见Cheatsheet,也可以在安装后的文件夹中找到,为“.md”格式的文件。
Torch中包含了很多工具包,有22类,如数学(Core Math)、机器学习(Machine Learning)、自然语言处理(NLP)、CUDA、音频、视频、图像等等,如下表。
大部分都在如下的“extra”文件夹中:
初探Torch
解释器启动Torch(LuaJIT)
Torch7 依赖于 LuaJIT 解释器, 因而启动Torch的最方便的方式是启动一个解释器,输入luajit
:
如果想给一个变量赋值,并显示该变量的值,输入:a = 10
,回车,=a
,注意到a前面的=
了吗?
是不是很不方便,太不和蔼了!原始的 LuaJIT 解释器有点简单 (no completion, no help, nothing preloaded…),下面要提及的TREPL没有上述缺点,更为人性化,启动Torch也只需输入:th
。
交互式会话环境(TREPL)
-
启动
在终端输入th
即可启动运行Torch,看到Torch的标志了吗?学习Torch的最简单的方法就是通过这个交互式会话(interactive session),也被称作TREPL( torch read-eval-print loop1)与Torch互动。如当你输入a=1
后回车再输入a
回车即可查看变量a的值;当输入?
时,Torch会输出帮助内容。
-
退出
输入os.exit()
或按两次Ctrl+C
键退出Torch。 -
运行文件
如果要计算源文件“file.lua”中的表达式,启动Torch后输入:dofile "file.lua"
;如果以非交互式的方式运行,可以输入:th file.lua
。比如,我在”~/work/torch”目录下新建了”file.lua”文件,并输入print "Hello World!"
保存,然后以上述两种方式运行:
-
有各种各样的方式来运行Lua代码,这与perl 和 ruby 类似:
-
TREPL 便利特性:
Tab-completion on nested namespaces(嵌套命名空间的Tab-补全)
Tab-completion on disk files (when opening a string)
History (preserved between sessions)
Pretty print (table introspection and coloring)
Auto-print after eval (can be stopped with ;)
Each command is profiled, timing is reported
No need for ‘=’ to print
Easy help with: ? funcname(? 函数名
查看帮助)
Self help: ?
Shell commands with: cmd(example: ls)(Shell 命令:$ cmd
,如$ ls -a
)
机器学习与Torch7
本人仅对机器学习感兴趣,发现了这个教程Machine Learning with Torch7,包含如下子教程,应该按顺序阅读:
- Tutorial 1: Setup / Basics / Getting Started
- Tutorial 2: Supervised Learning
- Tutorial 3: Unsupervised Learning
- Tutorial 4: Graphical Models
- Tutorial 5: Creating New Modules
- Tutorial 6: Using CUDA
这个教程包含代码,可以从GitHub上下载,或者如果系统装有git的话输入如下命令直接下载到当前目录下:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">git clone https://github.com/torch/tutorials.git</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
默认情况下,解释器仅仅预加载 torch,其它的诸如image和nn的库需要手动加载:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Loads nn and print details</span> require <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'nn'</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Loads image and doesn't print details</span> require <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'image'</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
就到这吧,你可以访问Machine Learning with Torch7学习,或者查看我的对应学习笔记:Machine Learning with Torch7学习笔记,下面是相应子教程链接:
英文教程 | 中文笔记 |
---|---|
Machine Learning with Torch7 | Machine Learning with Torch7学习笔记 |
Tutorial 1: Setup / Basics / Getting Started | ML&Torch教程1:安装/基础/开始 |
Tutorial 2: Supervised Learning | ML&Torch教程2:有监督学习 |
Tutorial 3: Unsupervised Learning | ML&Torch教程3:无监督学习 |
Tutorial 4: Graphical Models | ML&Torch教程4:图模型 |
Tutorial 5: Creating New Modules | ML&Torch教程5:创建新模块 |
Tutorial 6: Using CUDA | ML&Torch教程6:使用CUDA |
这个教程的代码的运行依赖于“dp”包,需要安装此包才能运行示例,可点此下载安装
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">git clone https://github.com/nicholas-leonard/dp.git ~/sfw/torch/extra/dp <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> ~/sfw/torch/extra/dp <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> luarocks make rocks/dp-scm-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>.rockspec</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
也可以直接输入如下命令安装:
<code class="language-bash hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">luarocks install dp</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
此外,Cheatsheet里有大量的Tutorials, Demos,涵盖图像、语音、视频、语音等方面的应用,自行查阅。
一个实例教程
参考:Deep Learning with Torch, a 60 minute tutorial,教程目的:
- 理解torch和神经网络工具包;
- 在CPU和GPU上训练一个小的神经网络。
此教程教我们在数据集CIFAR-10数据集上,构建一个卷积神经网络,并用于分类,包含如下几个步骤:
- 加载和归一化数据;
- 定义神经网络;
- 定义损失函数;
- 在训练数据集上训练网络;
- 在测试集上测试网络。
注:下面的代码,需要在“itorch notebook”中输入运行。使用itorch notebook
命令打开itorch notebook,并新建notebook。
加载与归一化数据集
CIFAR-10数据集包含10类物体,共50,000个训练样本,10,000个测试样本,为减小测试时间,这里使用一个小点的数据集,包含10,000个训练样本和10,000个测试样本。
使用如下代码下载(取消注释)并加载数据集:
<code class="language-itorch hljs lua has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip')</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- os.execute('unzip cifar10torchsmall.zip')</span> trainset = torch.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">load</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'yourdatadirectory/cifar10/cifar10-train.t7'</span>) testset = torch.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">load</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'yourdatadirectory/cifar10/cifar10-test.t7'</span>) classes = {<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'airplane'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'automobile'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'bird'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'cat'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'deer'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dog'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'frog'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'horse'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'ship'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'truck'</span>} <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">print</span>(trainset) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">print</span>(#trainset.data)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
使用如下代码显示数据集:
<code class="language-itorch hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box;">itorch</span>.image(trainset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{{1,36}</span>,<span class="hljs-container" style="box-sizing: border-box;">{}</span>,<span class="hljs-container" style="box-sizing: border-box;">{}</span>,<span class="hljs-container" style="box-sizing: border-box;">{}</span>}]) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- display the 1~36-th image in dataset</span></span> <span class="hljs-title" style="box-sizing: border-box;">for</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">36</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> print(classes[trainset.label[i]]) <span class="hljs-title" style="box-sizing: border-box;">end</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
数据标准化(均值为零,方差为1):
<code class="language-itorch hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box;">mean</span> = {} <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- store the mean, to normalize the test set in the future</span> <span class="hljs-title" style="box-sizing: border-box;">stdv</span> = {} <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- store the standard-deviation for the future</span> <span class="hljs-title" style="box-sizing: border-box;">for</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- over each image channel</span> mean[i] = trainset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{ {}</span>, <span class="hljs-container" style="box-sizing: border-box;">{<span class="hljs-title" style="box-sizing: border-box;">i</span>}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span> }]:mean<span class="hljs-container" style="box-sizing: border-box;">()</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- mean estimation</span></span> print('<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">Channel</span> ' .. i .. ', <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">Mean</span>: ' .. mean[i]) trainset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{ {}</span>, <span class="hljs-container" style="box-sizing: border-box;">{<span class="hljs-title" style="box-sizing: border-box;">i</span>}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span> }]:add<span class="hljs-container" style="box-sizing: border-box;">(-<span class="hljs-title" style="box-sizing: border-box;">mean</span>[<span class="hljs-title" style="box-sizing: border-box;">i</span>])</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- mean subtraction</span></span> stdv[i] = trainset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{ {}</span>, <span class="hljs-container" style="box-sizing: border-box;">{<span class="hljs-title" style="box-sizing: border-box;">i</span>}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span> }]:std<span class="hljs-container" style="box-sizing: border-box;">()</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- std estimation</span></span> print('<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">Channel</span> ' .. i .. ', <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">Standard</span> <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">Deviation</span>: ' .. stdv[i]) trainset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{ {}</span>, <span class="hljs-container" style="box-sizing: border-box;">{<span class="hljs-title" style="box-sizing: border-box;">i</span>}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span> }]:div<span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-title" style="box-sizing: border-box;">stdv</span>[<span class="hljs-title" style="box-sizing: border-box;">i</span>])</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- std scaling</span></span> <span class="hljs-title" style="box-sizing: border-box;">end</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
定义神经网络
“nn”是一个神经网络工具包,使用如下代码包含它:
<code class="language-itorch hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'nn'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
模块(Modules)是用于构建神经网络的积木,它们也是神经网络,通过容器(containers)组合其它网络形成复杂的神经网络。下面是一个用于数字图像分类的网络,它是一个简单的前馈神经网络,依次包含如下层:输入层、卷积层、池化层、卷积层、池化层、全连接层、全连接层(线性层)、高斯连接层,构成一个有序的网络容器:
我们使用的网络模型配置代码(--
是注释):
<code class="language-itorch hljs oxygene has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'nn'</span>; net = nn.Sequential() net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.SpatialConvolution(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)) -- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> input image channel, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> output channels, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>x5 convolution kernel net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.SpatialMaxPooling(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)) -- A max-pooling operation that looks at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>x2 windows <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> finds the max. net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.SpatialConvolution(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)) net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.SpatialMaxPooling(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)) net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.View(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)) -- reshapes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> a <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>D tensor <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>x5x5 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">into</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>D tensor <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.Linear(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>)) -- fully connected layer (matrix multiplication between input <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> weights) net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.Linear(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">84</span>)) net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.Linear(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">84</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)) -- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> the number <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> outputs <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> the network (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> this <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> digits) net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span>(nn.LogSoftMax()) -- converts the output <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> a log-probability. Useful <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> classification problems print(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Lenet5\n'</span> .. net:__tostring());</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
执行后输出:
<code class="language-itorch hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">Lenet5 nn.Sequential { [input<span class="hljs-function" style="box-sizing: border-box;"> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>)</span> -></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>)</span> -></span> output] (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>): nn.SpatialConvolution(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-function" style="box-sizing: border-box;"> -></span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>x5) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>): nn.SpatialMaxPooling(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>): nn.SpatialConvolution(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span><span class="hljs-function" style="box-sizing: border-box;"> -></span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>x5) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>): nn.SpatialMaxPooling(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>): nn.View (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>): nn.Linear(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">400</span><span class="hljs-function" style="box-sizing: border-box;"> -></span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>): nn.Linear(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span><span class="hljs-function" style="box-sizing: border-box;"> -></span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">84</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>): nn.Linear(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">84</span><span class="hljs-function" style="box-sizing: border-box;"> -></span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>) (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>): nn.LogSoftMax }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
nn中的其它容器结构:
简单测试一下网络:产生一个1×32×32的随机张量,进行一次前向传递和反向传播:
<code class="language-itorch hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">input = torch.rand(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">32</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">32</span>) -- pass a random tensor <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">as</span> input to the network output = net:forward(input) print(output) net:zeroGradParameters() -- zero the <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">internal</span> gradient buffers of the <span class="hljs-title" style="box-sizing: border-box;">network</span> (will come to <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span> later) gradInput = net:backward(input, torch.rand(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)) print(<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#gradInput) -- #a 求a的长度</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
为trainset添加size()函数和索引操作:
<code class="language-itorch hljs lua has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- ignore setmetatable for now, it is a feature beyond the scope of this tutorial. It sets the index operator.</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">setmetatable</span>(trainset, {__index = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(t, i)</span></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> {t.data[i], t.label[i]} <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span>} ); trainset.data = trainset.data:double() <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- convert the data from a ByteTensor to a DoubleTensor.</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">trainset:size</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> self.data:size(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">print</span>(trainset:size()) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- just to test</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">print</span>(trainset[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33</span>]) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- load sample number 33.</span> itorch.image(trainset[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>])</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>
定义一个损失函数
对数似然函数常用于分类:
<code class="language-itorch hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">criterion </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> nn.ClassNLLCriterion()</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
训练神经网络
定义一个统计梯度下降对象nn.StochasticGradient
,把训练数据送入其训练函数train
<code class="language-itorch hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">trainer = nn<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.StochasticGradient</span>(net, criterion) trainer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.learningRate</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.001</span> trainer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.maxIteration</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> -- just do <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> epochs of training. <span class="hljs-label" style="box-sizing: border-box;">trainer:</span>train(trainset) -- 训练网络</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
训练开始后,输出如下信息:
<code class="language-itorch hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># StochasticGradient: training </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.9760690803213 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.7046550614051 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.5611723361837 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.4702427591021 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.4034469173707 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># StochasticGradient: you have reached the maximum number of iterations </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># training error = 1.4034469173707</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
测试网络
显示测试样本:
<code class="language-itorch hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">print(classes[testset<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.label</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>]]) itorch<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.image</span>(testset<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>])</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
对测试数据集进行相同的标准化操作:
<code class="language-itorch hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box;">testset</span>.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span> = testset.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>:double<span class="hljs-container" style="box-sizing: border-box;">()</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- convert from Byte tensor to Double tensor</span></span> <span class="hljs-title" style="box-sizing: border-box;">for</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- over each image channel</span> testset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{ {}</span>, <span class="hljs-container" style="box-sizing: border-box;">{<span class="hljs-title" style="box-sizing: border-box;">i</span>}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span> }]:add<span class="hljs-container" style="box-sizing: border-box;">(-<span class="hljs-title" style="box-sizing: border-box;">mean</span>[<span class="hljs-title" style="box-sizing: border-box;">i</span>])</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- mean subtraction </span></span> testset.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>[<span class="hljs-container" style="box-sizing: border-box;">{ {}</span>, <span class="hljs-container" style="box-sizing: border-box;">{<span class="hljs-title" style="box-sizing: border-box;">i</span>}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span>, <span class="hljs-container" style="box-sizing: border-box;">{}</span> }]:div<span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-title" style="box-sizing: border-box;">stdv</span>[<span class="hljs-title" style="box-sizing: border-box;">i</span>])</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- std scaling</span></span> <span class="hljs-title" style="box-sizing: border-box;">end</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
对第100幅图像进行类别预测:
<code class="language-itorch hljs oxygene has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">print(classes[testset.label[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>]]) itorch.image(testset.data[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>]) predicted = net:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">forward</span>(testset.data[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>]) -- the output <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> the network <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> Log-Probabilities. <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">To</span> convert them <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> probabilities, you have <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">take</span> e^x print(predicted:exp())</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
计算正确率:
<code class="language-itorch hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">correct = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">do</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> groundtruth = testset.label[i] <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> prediction = net:forward(testset.data[i]) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> confidences, indices = torch.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sort</span>(prediction, <span class="hljs-constant" style="box-sizing: border-box;">true</span>) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- true means sort in descending order</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> groundtruth == indices[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> correct = correct + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span></span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">end</span></span> print(correct, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>*correct/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10000</span> .. <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">' % '</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
cunn: 使用cuda加速神经网络
使用cuda加速神经网络需要包含“cunn”库,如下代码显示如何使用cuda加速上述网络:
<code class="language-itorch hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box;">require</span> 'cunn'; <span class="hljs-title" style="box-sizing: border-box;">net</span> = net:cuda() <span class="hljs-title" style="box-sizing: border-box;">criterion</span> = criterion:cuda() <span class="hljs-title" style="box-sizing: border-box;">trainset</span>.<span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span> = trainset.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>:cuda<span class="hljs-container" style="box-sizing: border-box;">()</span></span> <span class="hljs-title" style="box-sizing: border-box;">trainer</span> = nn.<span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">StochasticGradient</span>(net, criterion) <span class="hljs-title" style="box-sizing: border-box;">trainer</span>.learningRate = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.001</span> <span class="hljs-title" style="box-sizing: border-box;">trainer</span>.maxIteration = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">-- just do 5 epochs of training.</span> <span class="hljs-title" style="box-sizing: border-box;">trainer</span>:train(trainset)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
看到如下输出,说明正使用CUDA进行加速:
<code class="language-itorch hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># StochasticGradient: training </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.3521909718752 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.3052758892536 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.2673446760654 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.2342329573035 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># current error = 1.2047961887121 </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># StochasticGradient: you have reached the maximum number of iterations </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># training error = 1.2047961887121</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
总结
本文记录了本人配置Ubuntu 14.04 LTS + Torch7 + CUDA环境的过程,以及遇到的一些问题,简要介绍了Torch的使用。
- What’s a REPL?
A read–eval–print loop (REPL), also known as an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise. The term is most usually used to refer to programming interfaces similar to the classic Lisp machine interactive environment. Common examples include command line shells and similar environments for programming languages, and is particularly characteristic of scripting languages.