机器学习开源框架系列:Torch:1:简介与安装

机器学习开源框架系列:Torch:1:简介与安装_第1张图片
Torch是一个基于BSD License的开源的机器学习的框架,早在2002年就发布了Torch的初版。Torch一直聚焦于大规模的机器学习应用,尤其是图像或者视频应用等领域,在超过5万次的下载,在学校和企业中也有着不小的影响力,而Torch7希望能够做到像Matlab那样,为机器学习提供环境。

基本信息

项目 说明
官方网站 http://torch.ch/
开源/闭源 开源
License类别 BSD License
代码管理地址 https://github.com/torch/torch7
开发语言 C,Lua
支持平台 由于使用C和Lua,Torch可以支持多种操作系统诸如Linux/MacOSX/iOS/Android等
当前版本 7.0 (2017/02/27)

功能特色

Torch的目标在保证使用的方式非常简单的基础上最大化地保证算法的灵活性和速度。Torch的核心是流行的神经网络和简单易用的优化库,使用Torch能在实现复杂的神经网络拓扑结构的时候保持最大的灵活性,同时可以使用并行的方式对CPU和GPU进行更有效率的操作。在LuaJIT/C/CUDA等基础之上,Torch的主要特性如下:

  • 强大的N维数组操作的支持
  • 提供很多对于索引/切片等的常用操作
  • 常见线性代数计算的支持
  • 神经网络和基于能量的模型
  • 支持GPU计算
  • 可嵌入,可移植到iOS或者Android

Torch与Lua

Lua在Torch中扮演了一个重要角色,Torch认为LuaJIT是那个“合适的脚本语言”,LuaJIT的优势如下

  • 快速,并且拥有一个透明的JIT编译器
  • 像Python一样简单而且可读
  • 和C语言的接口非常干净和简单
  • 可嵌入到任何环境中,比如iphone的引用,视频游戏或者web后端等

Lua vs Python

为什么不使用Python而使用LuaJIT,Torch给出的理由是:

  • 纠结于速度和效率,而超级轻量级的LuaJIT则不会在这方面碍手碍脚
  • 希望整个Torch框架是自包含的
  • 希望能更容易地将代码移植到任何平台,比如移植到iphone上而不需要修改已经写好的脚本
  • 希望有更容易的扩展性,LuaJIT的FFI接口是最容易学习的方式之一。

整体来说,这些点LuaJIT都更好地适应Torch,所以选择了LuaJIT

关于如何快速上手Lua,可以参看如下文章,快速Catchup

LUA快速使用指南 https://blog.csdn.net/liumiaocn/article/details/80342596

安装部署

下载install-all脚本

[root@devops ~]# wget  https://raw.githubusercontent.com/torch/ezinstall/master/install-all
...省略
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2096 (2.0K) [text/plain]
Saving to: ‘install-all’
...省略
[root@devops ~]#

使用脚本进行安装

无需其他,只需保证网络能够连接正常,等待结束即可完成安装

[root@devops ~]# sh install-all 
INFO: Preparing to install dependencies...
...省略
Total download size: 104 M
...省略
  Verifying  : libstdc++-4.8.5-16.el7.x86_64                                                                                    122/122 
Installed:
...省略
  Verifying  : libdv-1.0.0-17.el7.x86_64                                                                                          49/49 

Installed:
  python-ipython.noarch 0:3.2.1-1.el7                                                                                                   
...省略
Cloning into 'OpenBLAS'...
...省略
make -j 2 -C test all
...省略
TEST 22/22 potrf:bug_695 [OK]
RESULTS: 22 tests (22 ok, 0 failed, 0 skipped) ran in 0 ms
...省略
 OpenBLAS build complete. (BLAS CBLAS LAPACK LAPACKE)
  OS               ... Linux             
  Architecture     ... x86_64               
  BINARY           ... 64bit                 
  C compiler       ... GCC  (command line : cc)
  Fortran compiler ... GFORTRAN  (command line : gfortran)
  Library Name     ... libopenblas_haswellp-r0.3.0.dev.a (Multi threaded; Max num-threads is 2)
...省略
==> Torch7's dependencies have been installed
INFO: Dependencies installed successfully.
INFO: Preparing to install torch...
Installing Torch into: /usr/local
Cloning into 'luajit-rocks'...
...省略
Already on 'master'
Already up-to-date.
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
...省略
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/include/luaconf.h
-- Installing: /usr/local/include/lua.h
-- Installing: /usr/local/include/lauxlib.h
-- Installing: /usr/local/include/lualib.h
-- Installing: /usr/local/include/lua.hpp
-- Installing: /usr/local/include/luajit.h
-- Installing: /usr/local/lib/libluajit.so
-- Installing: /usr/local/bin/luajit
...省略
-- Installing: /usr/local/etc/luarocks/config.lua
-- Installing: /usr/local/bin/luarocks
-- Installing: /usr/local/bin/luarocks-admin
...省略
Cloning into 'sundown-ffi'...
...省略
Cloning into 'cwrap'...
r...省略
Cloning into 'paths'...
...省略
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/luarocks/rocks/paths/scm-1/lua/paths/init.lua
...省略
Cloning into 'torch7'...
...省略
[100%] Built target torch
cd build && make install
[ 55%] Built target TH
[ 58%] Built target luaT
[100%] Built target torch
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/share/cmake/torch/TorchExports.cmake
...省略
-- Installing: /usr/local/lib/libTH.so.0
-- Installing: /usr/local/lib/libTH.so
...省略
-- Installing: /usr/local/include/TH/THTensor.h
-- Installing: /usr/local/include/TH/THTensorApply.h
-- Installing: /usr/local/include/TH/THTensorDimApply.h
-- Installing: /usr/local/include/TH/THTensorMacros.h
-- Installing: /usr/local/include/TH/THVector.h
...省略
Cloning into 'luaffifb'...
...省略
Cloning into 'nn'...
...省略
[100%] Building C object lib/THNN/CMakeFiles/THNN.dir/init.c.o
Linking C shared module libTHNN.so
[100%] Built target THNN
cd build && make install
[100%] Built target THNN
Install the project...
-- Install configuration: "Release"
...省略
-- Installing: /usr/local/lib/luarocks/rocks/nn/scm-1/lib/libTHNN.so
-- Set runtime path of "/usr/local/lib/luarocks/rocks/nn/scm-1/lib/libTHNN.so" to "$ORIGIN/../lib:/usr/local/lib:/opt/OpenBLAS/lib"
-- Installing: /usr/local/include/THNN/THNN.h
-- Installing: /usr/local/include/THNN/generic/THNN.h
...省略
Cloning into 'dok'...
...省略
Cloning into 'gnuplot'...
...省略
Cloning into 'qtlua'...
...省略
Cloning into 'qttorch'...
...省略
Cloning into 'sys'...
...省略
Cloning into 'image'...
...省略
Cloning into 'optim'...
...省略
Cloning into 'trepl'...
...省略
=> Torch7 has been installed successfully

  + Extra packages have been installed as well:
     $ luarocks list

  + To install more packages, do:
     $ luarocks search --all
     $ luarocks install PKG_NAME
...省略
  + packages installed:
    - sundown   :  ok
    - cwrap     :  ok
    - paths     :  ok
    - torch     :  ok
    - nn        :  ok
    - dok       :  ok
    - gnuplot   :  ok
    - qtlua     :  ok
    - qttorch   :  ok
    - lfs       :  ok
    - penlight  :  ok
    - sys       :  ok
    - xlua      :  ok
    - image     :  ok
    - optim     :  ok
    - cjson     :  ok
    - trepl     :  ok

INFO: Torch installed successfully.
[root@devops ~]#

删除脚本

https://raw.github.com/torch/ezinstall/master/clean-old.sh

安装确认

确认luagit

Torch7希望自己是自包含的,这里面luagit起到了一个重要作用,luajit会同时一同被安装,所以需要进行确认

[root@devops ~]# which luajit
/usr/local/bin/luajit
[root@devops ~]# luajit -v
LuaJIT 2.0.4 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/

 _____              _     
|_   _|            | |    
  | | ___  _ __ ___| |__  
  | |/ _ \| '__/ __| '_ \ 
  | | (_) | | | (__| | | |
  \_/\___/|_|  \___|_| |_|

[root@devops ~]# 

使用此版本的luajit进行动作确认

[root@devops ~]# luajit
LuaJIT 2.0.4 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/

 _____              _     
|_   _|            | |    
  | | ___  _ __ ___| |__  
  | |/ _ \| '__/ __| '_ \ 
  | | (_) | | | (__| | | |
  \_/\___/|_|  \___|_| |_|

JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> print "Hello World"
Hello World
> 

可以看到Torch7下的luajit确实很小

[root@devops ~]# du -k /usr/local/bin/luajit
648 /usr/local/bin/luajit
[root@devops ~]#

确认luarocks

除此之外,luarocks作为Lua的包管理的工具也非常重要,确认其版本以及是否能够进行正常操作,后续会通过luarocks install来安装所需的依赖

[root@devops ~]# luarocks
LuaRocks 2.3.0, a module deployment system for Lua
...省略
CONFIGURATION
    Lua version: 5.1
    Configuration files:
        System: /usr/local/etc/luarocks/config.lua (ok)
        User  : /root/.luarocks/config-5.1.lua (not found)
...省略
[root@devops ~]# luarocks list  nn
...省略
nn
   scm-1 (installed) - /usr/local/lib/luarocks/rocks
[root@devops ~]# 

确认th

Torch可以通过交互式的th进行操作,确认其是否能够正常进入

[root@devops ~]# th

  ______             __   |  Torch7 
 /_  __/__  ________/ /   |  Scientific computing for Lua. 
  / / / _ \/ __/ __/ _ \  |  Type ? for help 
 /_/  \___/_/  \__/_//_/  |  https://github.com/torch 
                          |  http://torch.ch 

th>

总结

这篇文章介绍了Torch的概要信息,以及如何在centos7上安装最新版本的torch7.

你可能感兴趣的:(人工智能,TensorFlow入门教程)