安装Torch


官方提供在Mac OS X和Ubuntu 12+上安装Torch:

你可以通过如下三个命令把Torch安装到你的home文件夹中,注意:包比较大,需要耐心等待:

#在终端运行命令没有sudo
git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; bash install-deps;
./install.sh

第一个脚本安装基本的软件包的依赖关系,要求LuaJITTorch。第二脚本安装LuaJITluarocks,然后使用luarocks(Lua包管理器)安装核心包像TorchNN和路径,以及一些其他的包。 该脚本添加到您的路径变量Torch。你只需要一次就可以刷新你的环境变量。安装脚本将检测您当前的shell,并在正确的配置文件中修改路径。

# On Linux with bash
source ~/.bashrc
# On Linux with zsh
source ~/.zshrc
# On OSX or in Linux with none of the above.
source ~/.profile

如果你需要卸载Torch,只需运行命令:

rm -rf ~/torch

如果你想安装Torch的lua 5.2代替LuaJIT,简单地运行:

git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch

# clean old torch installation
./clean.sh
# optional clean command (for older torch versions)
# curl -s https://raw.githubusercontent.com/torch/ezinstall/master/clean-old.sh | bash

# https://github.com/torch/distro : set env to use lua
TORCH_LUA_VERSION=LUA52 ./install.sh

新的软件包,可以使用命令行luarocks安装:

# run luarocks WITHOUT sudo
$ luarocks install image
$ luarocks list

一旦安装好,你就可以用你的命令运行Torch了! 在学习和试验Torch的最简单的方法是启动一个交互式会话(也被称为TorchRead-Eval-Print-Loop或trepl):

$ th

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

th> torch.Tensor{1,2,3}
 1
 2
 3
[torch.DoubleTensor of dimension 3]

th>

退出交互会话,型^C两控制键和C键,两次,或类型的操作系统。exit()。一旦用户输入了一个完整的表达式,如1 + 2,并点击进入,交互会话将计算表达式并显示其值。 评价写在一个源文件file.lua表达式,写dofile”文件。lua”。 要在非交互式的文件中运行代码,可以将它作为第一个参数传递给TH命令::

$ th file.lua

有不同的方法来运行lua代码和提供选项,类似于那些用于Perl和Ruby程序:

$ th -h
Usage: th [options] [script.lua [arguments]]

Options:
  -l name            load library name
  -e statement       execute statement
  -h,--help          print this help
  -a,--async         preload async (libuv) and start async repl (BETA)
  -g,--globals       monitor global variables (print a warning on creation/access)
  -gg,--gglobals     monitor global variables (throw an error on creation/access)
  -x,--gfx           start gfx server and load gfx env
  -i,--interactive   enter the REPL after executing a script

TREPL充满了方便的特性:

  1. tab补齐在嵌套名称空间
  2. 打开磁盘文件(打开字符串时)
  3. 历史(会话之间保存)
  4. 漂亮的打印(表内省和着色)
  5. eval(可以停止)自动打印(可以停止)
  6. 每个命令都被提交,时间被报告
  7. 不需要“=”打印
  8. 简单的帮助:?funcname
  9. 自助:?
  10. Shell命令:$ cmd(示例:$ ls)

接下来的步骤 除了这本手册,还有各种其他的资源,可以帮助新用户开始使用Torch,都总结在本列表 该控件提供的教程,演示,包总结和大量有用的信息。 如果你有问题,请加入Torch手用户名单。