在Ubuntu 16.04上安装Torch7和OpenNMT

参考资料

  1. OpenNMT: Open-Source Neural Machine Translation GitHub网址
  2. Installing Torch7 and OpenNMT On Ubuntu 14.04
  3. Getting started with Torch
  4. Self-contained Torch installation
  5. 我爱自然语言处理

本机环境

Ubuntu 16,04 | 无GPU
注:如果有GPU时,驱动安装请选择cuda 8.x,不要选择9版本;同时gcc需要降级,如果没报gcc错误,就不用降级,否则按照提示降级或者安装低版本的gcc并做软连接替换,具体方法请自行google。

开始安装 Torch7和 Lua5.2

Install Torch7 with Lua 5.2
运行命令将Torch安装到文件夹~/torch中
注意请不要用sudo执行命令,仅仅安装在当前用户环境下

# 从GitHub里保存Torch
$ git clone https://github.com/torch/distro.git ~/torch --recursive
$ cd ~/torch

运行两个脚本

# 安装LuaJIT和Torch所需要的基本软件包依赖项
$ bash install-deps
# 安装LuaJIT和LuaRocks(一款lua包管理工具,类似python中的pip)
# 然后使用LuaRocks来安装核心包,比如说torch,nn和paths,还有其他包
# https://github.com/torch/distro : set env to use lua
# 默认时 执行 ./install.sh 会安装LuaJIT2.1
# 当前如果你要安装Lua5.1 可以使用代码 TORCH_LUA_VERSION = LUA51 ./install.sh
# 这里我们安装了Lua5.2 
# 如果安装了不同的版本,请使用./clean.sh清除它 
$ TORCH_LUA_VERSION=LUA52 ./install.sh

将torch添加到PATH变量中(根据自己系统情况选择)

# 将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

安装完成后,可以查看luarocks安装的软件包

$ luarocks list

学习和试验Torch的最简单方法是启动交互式会话(也称为火炬读取 - 评估 - 打印循环或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]

要退出交互式会话,请键入Ctrl+c两次 - 控制键以及c键,两次或键入os.exit()。

要以非交互方式在文件中运行代码,可以将其作为th命令的第一个参数:

$ th file.lu

有多种方法可以运行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

测试
可以通过运行以下方法测试是否正确安装了所有库,输出会有点多

#使用./test.sh >> test_log.txt 保存到文件中
$ ./test.sh

更新
要将已安装的发行版更新到火炬/发行版的最新主分支,只需运行即可

$ ./update.sh

清理
删除临时编译的文件

$ ./clean.sh

要删除安装(没用过这个)

# Warning: this will remove your current installation
$ rm -rf ./install

从你的启动文件(~/.bashrc or ~/.profile)中移除torch-acticate

. /home/XXXXXX/torch/install/bin/torch-activate

如果需要卸载火炬,只需运行命令:(使用过)

$:rm -rf ~/torch

如果你卸载后重新安装,请执行下面命令,清理以前旧设置
如果安装了不同的版本,请使用./clean.sh清除它
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

安装OpenNMT

$ git clone https://github.com/opennmt/opennmt OpenNMT
$ luarocks install tds

运行它

$ cd ~/OpenNMT

1.预处理数据

th preprocess.lua -train_src data/src-train.txt -train_tgt data/tgt-train.txt -valid_src data/src-val.txt -valid_tgt data/tgt-val.txt -save_data data/demo

2.训练模型

th train.lua -data data/demo-train.t7 -save_model model

训练模式开始
这一步会训练出13个模型
训练结果
3.翻译句子
将model_final.t7 改为训练出来的13个模型中的任意一个,比如说model_epoch1_3918_85.t7

th translate.lua -model model_final.t7 -src data/src-test.txt -output pred.txt

翻译结束后会给出PPL得分
翻译结果

记录运行命令时出的错

如果安装了cuda可能出现

/home/XXXXXX/torch/extra/cutorch/lib/THC/generic/THCTensorMathPairwise.cu(66): error: more than one operator "!=" matches these operands:
            function "operator!=(const __half &, const __half &)"
            function "operator!=(half, half)"
            operand types are: half != half

原因是cuda和torch的头文件都提供了相同的重载运算符,编译器不知道用哪一个。输入下面shell命令禁止使用cuda的头文件编译torch即可:

export TORCH_NVCC_FLAGS="-D__CUDA_NO_HALF_OPERATORS__"

重新运行./install.sh重新开始编译就OK了。
安装完成后提示,输入yes回车就好

Do you want to automatically prepend the Torch install location
to PATH and LD_LIBRARY_PATH in your /home/zzunlp/.bashrc? (yes/no)
[yes] >>> 
yes

安装bit32,一个lua包

$ wget https://luarocks.org/manifests/siffiejoe/bit32-5.3.0-1.src.rock luarocks 
$ luarocks unpack bit32-5.3.0-1.src.rock 
$ cd bit32-5.3.0-1/lua-compat-5.2 
$ luarocks make bit32-5.3.0-1.rockspec

解决安装lua包报错,报错内容如下:

$ luarocks install nn(任意lua包)
Output:
Warning: Failed searching manifest: Failed fetching manifest for https://raw.githubusercontent.com/torch/rocks/master - Failed downloading https://raw.githubusercontent.com/torch/rocks/master/manifest
Warning: Failed searching manifest: Failed fetching manifest for https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master - Failed downloading https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/manifest

解决办法:

$ sudo rm -rf ~/.cache/luarocks
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值