Deep Semantic Role Labeling のソースコードを動かすまでの手順

nvidia-docker2でコンテナを作成。 —runtime=nvidiaを忘れないこと。

$ docker run -it --runtime=nvidia nvidia/cuda:7.5-cudnn5-devel-ubuntu14.04

ソースコードがtheano==0.9.0にしか対応しておらず、しかもtheano0.9.0はcuDNN5でしか動かない為、コンテナで新しい環境を作った。

作ったコンテナに入る。

$ docker start hoge
$ docker attach hoge

ここから先の手順はコンテナ内で実行。

コンテナに標準搭載されていなかった必要パッケージを入れる

# apt update
# apt install git
# apt install unzip

コンテナ内でのライブラリ管理が面倒なので、Anaconda2をインストールすることにした。

Anacondaに必要パッケージのインストール

# apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev libpng-dev

pyenvのインストール

# git clone git://github.com/yyuu/pyenv.git ~/.pyenv
# git clone https://github.com/yyuu/pyenv-pip-rehash.git ~/.pyenv/plugins/pyenv-pip-rehash
# echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
# echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
# echo 'eval "$(pyenv init -)"' >> ~/.bashrc
# source ~/.bashrc

まず、最新のAnaconda (Python2系)のバージョンを確認

# pyenv install -l | grep anaconda2
anaconda2-2.4.0
anaconda2-2.4.1
anaconda2-2.5.0
anaconda2-4.0.0
anaconda2-4.1.0
anaconda2-4.1.1
anaconda2-4.2.0
anaconda2-4.3.0
anaconda2-4.3.1
anaconda2-4.4.0
anaconda2-5.0.0
anaconda2-5.0.1
anaconda2-5.1.0
anaconda2-5.2.0

最新のAnaconda2 (ここではanaconda2-2.4.0) をインストールし、デフォルトとして設定。

# pyenv install anaconda2-2.4.0
# pyenv global anaconda2-2.4.0
# echo 'export PATH="$PYENV_ROOT/versions/anaconda2-2.4.0/bin:$PATH"' >> ~/.bashrc
# source ~/.bashrc

Pythonの環境を確認

# python --version
Python 2.7.10 :: Anaconda 2.4.0 (64-bit)

ここから、SRLのソースコードのインストールに移る コンテナ内の適当なディレクトリで、git clone

# git clone https://github.com/luheng/deep_srl.git

コードを動かすため、パッケージをインストール

# conda install theano==0.9.0 pygpu protobuf nltk
# apt install tcsh

Google ドライブのPretrainedファイル6つすべてダウンロードし、~/deep_srl/resources下に配置

https://drive.google.com/drive/folders/0B5zHXdvxrsjNZUx2YXJ5cEM0TW8?usp=sharing

先ほどダウンロードしたファイルの展開

# tar -zxvf conll05_ensemble.tar.gz
# tar -zxvf conll05_model.tar.gz
# tar -zxvf conll05_propid_model.tar.gz
# tar -zxvf conll2012_ensemble.tar.gz
# tar -zxvf conll2012_model.tar.gz
# tar -zxvf conll2012_propid_model.tar.gz

GloVe のインストール

./scripts/fetch_required_data.sh

以上でインストール終了。

サンプルコードを動かす前に、モデルのパスを指定

# vi run_end2end.sh
#!/bin/bash
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda:/usr/local/cuda/lib64:/opt/OpenBLAS/lib
MODEL_PATH="./resources/conll05_propid_model"
('lstm_1_rdrop', 0.1, True)
('lstm_2_rdrop', 0.1, True)
('lstm_3_rdrop', 0.1, True)
('lstm_4_rdrop', 0.1, True)
('lstm_5_rdrop', 0.1, True)
('lstm_6_rdrop', 0.1, True)
('lstm_7_rdrop', 0.1, True)
32072 2
Loaded model from: ./resources/conll05_ensemble/model4.npz
Model building and loading duration was 0:00:04.
Running model duration was 0:00:00.
Decoding duration was 0:00:00.
Accuracy: 38.750 (31.0/80.0)
Printing results to temp file: /root/script/deep_srl/python/neural_srl/shared/../../../temp/srl_pred_5859.tmp
Number of Sentences : 5
Number of Propositions : 8
Percentage of perfect props : 0.00
corr. excess missed prec. rec. F1
------------------------------------------------------------
Overall 0 19 0 0.00 0.00 0.00
----------
A0 0 6 0 0.00 0.00 0.00
A1 0 8 0 0.00 0.00 0.00
A2 0 2 0 0.00 0.00 0.00
AM-DIR 0 2 0 0.00 0.00 0.00
AM-TMP 0 1 0 0.00 0.00 0.00
------------------------------------------------------------
V 6 2 2 75.00 75.00 75.00
------------------------------------------------------------
Fscore=0.0
Writing to human-readable file: temp/sample.out

結果はテキストファイルとして、temp/sample.outに保存される

temp/sample.out

1 John told Pat to cut off the tree .
Predicate: told(2)
A0: 1 John
V: told
A2: Pat
A1: to cut off the tree
1 John told Pat to cut off the tree .
Predicate: cut(5)
A0: Pat
V: cut off
A1: the tree
4 John told Pat to cut off the tree .
Predicate: told(2)
A0: 4 John
V: told
A2: Pat
A1: to cut off the tree
4 John told Pat to cut off the tree .
Predicate: cut(5)
A0: Pat
V: cut off
A1: the tree
1 We saw the Grand Canyon flying to Chicago .
Predicate: saw(2)
A0: We
V: saw
A1: the Grand Canyon flying to Chicago
1 We saw the Grand Canyon flying to Chicago .
Predicate: flying(6)
A1: the Grand Canyon
V: flying
AM-DIR: to Chicago
5 We saw the Grand Canyon flying to Chicago .
Predicate: saw(2)
AM-TMP: 5
A0: We
V: saw
A1: the Grand Canyon flying to Chicago
5 We saw the Grand Canyon flying to Chicago .
Predicate: flying(6)
A1: the Grand Canyon
V: flying
AM-DIR: to Chicago

こんな感じで、動詞を中心とした係り受け解析ができる。