開始使用 Torch 在 GitHub 上編輯
安裝 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、nn 和 paths,以及其他幾個套件。
指令碼會將 torch 加入到您的 PATH 變數中。只要執行一次指令碼,就能更新您的環境變數。安裝指令碼會偵測您目前的殼層,並修改正確的設定檔中的路徑。
# 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
如果您想用 Lua 5.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 的方法,就是開始一個互動式工作階段(也稱為 torch 讀取-評估-列印循環或 TREPL)
$ th
______ __ | Torch7
/_ __/__ ________/ / | Scientific computing for Lua.
/ / / _ \/ __/ __/ _ \ |
/_/ \___/_/ \__/_//_/ | https://github.com/torch
| https://torch.dev.org.tw
th> torch.Tensor{1,2,3}
1
2
3
[torch.DoubleTensor of dimension 3]
th>
若要離開互動式工作階段,請連續輸入 ^c
兩次,即同時按下控制鍵和 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 充滿以下便利功能
- 巢狀命名空間的 Tab 自動完成
- 磁碟檔案的 Tab 自動完成(開啟字串時)
- 記錄(能保存於多個工作階段中)
- 美化列印(表格內省和色彩)
- 評估後自動列印(可以使用 ; 停止)
- 每個指令都會描寫,計時都會回報
- 不需要 ‘=’ 來列印
- 使用以下方式來輕鬆取得說明:
? funcname
- 自行說明:
?
- 使用以下方式來使用殼辦指令:$ cmd(範例:
$ ls
)
後續步驟
除了本手冊,還有各類的其他資源,可能有助於新使用者開始使用 torch,所有都在此 秘笈 中加以摘要
秘笈提供前往教學課程、試用版、套件摘要和許多有用資訊的連結。
如果您有任何問題,請加入 Torch 使用者郵寄清單