Skip to content

Commit 565dcf2

Browse files
Merge pull request #1 from clab/master
update from origin
2 parents a8831ec + 340ce47 commit 565dcf2

File tree

172 files changed

+294155
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+294155
-19
lines changed

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
project(cnn)
2+
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
3+
4+
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
5+
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -O3 -g")
6+
7+
enable_testing()
8+
9+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
10+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/cnn)
11+
set(WITH_EIGEN_BACKEND 1)
12+
13+
# look for Boost
14+
set(Boost_REALPATH ON)
15+
find_package(Boost COMPONENTS program_options iostreams serialization REQUIRED)
16+
include_directories(${Boost_INCLUDE_DIR})
17+
set(LIBS ${LIBS} ${Boost_LIBRARIES})
18+
19+
# look for Eigen
20+
find_package(Eigen3 REQUIRED)
21+
include_directories(${EIGEN3_INCLUDE_DIR})
22+
23+
#configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
24+
25+
add_subdirectory(cnn/cnn)
26+
add_subdirectory(nt-parser)
27+
# add_subdirectory(cnn/examples)
28+

README.md

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,116 @@
1-
# rnng
2-
Recurrent neural network grammars
1+
# Recurrent Neural Network Grammars
2+
Code for the [Recurrent Neural Network Grammars](https://arxiv.org/abs/1602.07776) paper (NAACL 2016), by Chris Dyer, Adhiguna Kuncoro, Miguel Ballesteros, and Noah A. Smith, after the Corrigendum (last two pages on the ArXiv version of the paper). The code is written in C++.
3+
4+
# Citation
5+
@inproceedings{dyer-rnng:16,
6+
author = {Chris Dyer and Adhiguna Kuncoro and Miguel Ballesteros and Noah A. Smith},
7+
title = {Recurrent Neural Network Grammars},
8+
booktitle = {Proc. of NAACL},
9+
year = {2016},
10+
}
11+
12+
# Prerequisites
13+
* A C++ compiler supporting the [C++11 language standard](https://en.wikipedia.org/wiki/C%2B%2B11)
14+
* [Boost](http://www.boost.org/) libraries
15+
* [Eigen](http://eigen.tuxfamily.org) (latest development release)
16+
* [CMake](http://www.cmake.org/)
17+
* [EVALB](http://nlp.cs.nyu.edu/evalb/) (latest version. IMPORTANT: please put the EVALB folder on the same directory as `get_oracle.py` and `sample_input_chinese.txt` to ensure compatibility)
18+
19+
# Build instructions
20+
Assuming the latest development version of Eigen is stored at: /opt/tools/eigen-dev
21+
22+
mkdir build
23+
cd build
24+
cmake -DEIGEN3_INCLUDE_DIR=/opt/tools/eigen-dev ..
25+
make -j2
26+
27+
28+
# Sample input format:
29+
`sample_input_english.txt` (English PTB) and `sample_input_chinese.txt` (Chinese CTB)
30+
31+
# Oracles
32+
The oracle converts the bracketed phrase-structure tree into a sequence of actions.
33+
The script to obtain the oracle also converts singletons in the training set and unknown words in the dev and test set into a fine-grained set of 'UNK' symbols
34+
35+
### Obtaining the oracle for the discriminative model
36+
python get_oracle.py [training file] [training file] > train.oracle
37+
python get_oracle.py [training file] [dev file] > dev.oracle
38+
python get_oracle.py [training file] [test file] > test.oracle
39+
40+
### Obtaining the oracle for the generative model
41+
python get_oracle_gen.py [training file] [training file] > train_gen.oracle
42+
python get_oracle_gen.py [training file] [dev file] > dev_gen.oracle
43+
python get_oracle_gen.py [training file] [test file] > test_gen.oracle
44+
45+
# Discriminative Model
46+
The discriminative variant of the RNNG is used as a proposal distribution for decoding the generative model (although it can also be used for decoding on its own). To save time we recommend training both models in parallel.
47+
48+
On the English PTB dataset the discriminative model typically converges after about 3 days on a single-core CPU device.
49+
50+
### Training the discriminative model
51+
52+
nohup build/nt-parser/nt-parser --cnn-mem 1700 -x -T [training_oracle_file] -d [dev_oracle_file] -C [original_dev_file (PTB bracketed format, see sample_input_english.txt)] -P -t --pretrained_dim [dimension of pre-trained word embedding] -w [pre-trained word embedding] --lstm_input_dim 128 --hidden_dim 128 -D 0.2 > log.txt
53+
54+
IMPORTANT: please run the command at the same folder where `remove_dev_unk.py` is located.
55+
56+
If not using pre-trained word embedding, then remove the `--pretrained_dim` and `-w` flags.
57+
58+
The training log is printed to `log.txt` (including information on where the parameter file for the model is saved to, which is used for decoding under the -m option below)
59+
60+
### Decoding with discriminative model
61+
62+
build/nt-parser/nt-parser --cnn-mem 1700 -x -T [training_oracle_file] -p [test_oracle_file] -C [original_test_file (PTB bracketed format, see sample_input_english.txt)] -P --pretrained_dim [dimension of pre-trained word embedding] -w [pre-trained word embedding] --lstm_input_dim 128 --hidden_dim 128 -m [parameter file] > output.txt
63+
64+
Note: the output will be stored in `/tmp/parse/parser_test_eval.xxxx.txt` and the parser will output F1 score calculated with EVALB with COLLINS.prm option. The parameter file (following the -m in the command above) can be obtained from `log.txt`.
65+
66+
If training was done using pre-trained word embedding (by specifying the -w and --pretrained\_dim options) or POS tags (-P option), then decoding must alo use the exact same options used for training.
67+
68+
# Generative Model
69+
The generative model achieved state of the art results, and decoding is done using sampled trees from the trained discriminative model
70+
For the best results the generative model takes about 7 days to converge.
71+
72+
### Training the generative model
73+
nohup build/nt-parser/nt-parser-gen -x -T [training_oracle_generative] -d [dev_oracle_generative] -t --clusters clusters-train-berk.txt --input_dim 256 --lstm_input_dim 256 --hidden_dim 256 -D 0.3 > log_gen.txt
74+
75+
The training log is printed to `log_gen.txt`, including information on where the parameters of the model is saved to, which is used for decoding later.
76+
77+
# Decoding with the generative model
78+
Decoding with the generative model requires sample trees from the trained discriminative model
79+
80+
### Sampling trees from the discriminative model
81+
build/nt-parser/nt-parser --cnn-mem 1700 -x -T [training_oracle_file] -p [test_oracle_file] -C [original_test_file (PTB bracketed format, see sample_input_english.txt)] -P --pretrained_dim [dimension of pre-trained word embedding] -w [pre-trained word embedding] --lstm_input_dim 128 --hidden_dim 128 -m [parameter file of trained discriminative model] --alpha 0.8 -s 100 > test-samples.props
82+
83+
important parameters
84+
85+
* s = # of samples (all reported results used 100)
86+
* alpha = posterior scaling (since this is a proposal, a higher entropy distribution is probably good, so a value < 1 is sensible. All reported results used 0.8)
87+
88+
### Prepare samples for likelihood evaluation
89+
90+
utils/cut-corpus.pl 3 test-samples.props > test-samples.trees
91+
92+
### Evaluate joint likelihood under generative model
93+
94+
build/nt-parser/nt-parser-gen -x -T [training_oracle_generative] --clusters clusters-train-berk.txt --input_dim 256 --lstm_input_dim 256 --hidden_dim 256 -p test-samples.trees -m [parameters file from the trained generative model, see log_gen.txt] > test-samples.likelihoods
95+
96+
### Estimate marginal likelihood (final step to get language modeling ppl)
97+
98+
utils/is-estimate-marginal-llh.pl 2416 100 test-samples.props test-samples.likelihoods > llh.txt 2> rescored.trees
99+
100+
* 100 = # of samples
101+
* 2416 = # of sentences in test set
102+
* `rescored.trees` will contain the samples reranked by p(x,y)
103+
104+
The file `llh.txt` would contain the final language modeling perplexity after marginalization (see the last lines of the file)
105+
106+
### Compute generative model parsing accuracy (final step to get parsing accuracy from the generative model)
107+
108+
utils/add-fake-preterms-for-eval.pl rescored.trees > rescored.preterm.trees
109+
utils/replace-unks-in-trees.pl [Discriminative oracle for the test file] rescored.preterm.trees > hyp.trees
110+
utils/remove_dev_unk.py [gold trees on the test set (same format as sample_input_english.txt)] hyp.trees > hyp_final.trees
111+
EVALB/evalb -p COLLINS.prm [gold trees on the test set (same format as sample_input_english.txt)] hyp_final.trees > parsing_result.txt
112+
113+
The file `parsing_result.txt` contains the final parsing accuracy using EVALB
114+
115+
# Contact
116+
If there are any issues, please let us know at adhiguna.kuncoro [ AT SYMBOL ] gmail [ DOT ] com

0 commit comments

Comments
 (0)