安装 Torch
Jll580
・5 分钟阅读
Torch入门
安装 Torch
在 Mac OS X 和 Ubuntu 12 + 上,我们为Torch提供了一个简单的安装过程:
torch可以通过运行以下三个命令在~/torch中的主文件夹中安装:
# in a terminal, run the commands WITHOUT sudo
git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; bash install-deps;
./install.sh
第一个脚本安装LuaJIT和Torch所需的基本包依赖项, 第二个脚本安装 LuaJIT ,LuaRocks ,然后使用 LuaRocks (lua软件包管理器)安装核心包,比如,Torch 和paths,还有其他一些安装包。
脚本将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
如果你想用lua5.2,而且不是luajit安装torch,只需运行:
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
安装之后,你就可以使用命令th
运行Torch了 !
学习和使用Torch的最简单方法是启动交互式会话(也称为Torchread-eval-print循环或 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
两次:将控制键与键一起键入,两次或键入 os.exit()
, 一旦用户输入了完整的表达式,如 1 + 2
,并且点击 enter,交互式会话计算表达式,并且显示它值。
若要计算在源文件file.lua
中写入的表达式,请编写 dofile"file.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的功能非常方便:
- 嵌套命名空间上的制表符完成
- 磁盘文件上的选项卡完成(打开字符串时),
- 历史记录(在会话之间保留)
- 漂亮的打印(表格自省和着色)
- 评估后自动打印(可以停止使用)
- 每个命令都被配置,定时报告
- 不需要'='去打印
- 轻松帮助:
? funcname
- 自帮助:
?
- $ cmd(示例:
$ ls
)