Neural network language models wrapped with a common API for prediction, encoding, etc.
3.3K

This folder contains scripts for obtaining surprisals from the following pre-trained language models:

The models use the following tokenizers:
| Model | Tokenizer |
|---|---|
| GRNN | TreeTagger |
| JRNN | WMT11 tokenizer |
| RNNG | PTB tokenizer |
| Tiny | PTB tokenizer |
| Trans | Moses (one implementation) |
| ngram | TreeTagger |
The parameters are taken from the standard published version of each model unless stated otherwise.
Surprisals can be obtained from each model using the script eval_<MODEL>.sh in the scripts folder.
Each script expects two arguments: $1 is the input file containing the sentences, and
$2 is the output file to save the surprisals.
The input file should have each sentence on a new line, and each sentence should be tokenized.
There are also some model-specific constraints, although I may try to streamline these later:
<eos> token.unks. I am working on adding a script to do this.unkified. An unkify function is provided in
rnng-incremental/get_raw.py, which can be used in the following way:python2 get_raw.py train.02-21 \
RAW.txt > UNKIFIED.txt
The output file will have the following format:
token1 0.0
token2 ...
. ...
<eos> 0.0
where the second column (separated by \t) gives the surprisal in bits of the token.
When you run eval_ngram.sh, you will also get an extra .raw output file that has the raw SRILM output with
details about word probabilities and backoff.
The GRNN, JRNN, Transformer-XL, and Tiny LSTM models require pytorch and other dependencies that can be found
in their source folders. If you don't feel like creating your own environments, feel free to "steal" mine:
/om2/user/jennhu/conda/envs/neural-nlp (credit to Martin Schrimpf) works for GRNN, JRNN, and Tiny LSTM, and
/om2/user/jennhu/conda/envs/transXL was custom-built for Transformer-XL.
The dependencies for RNNG should already be set in the source code. If problems arise, I may make a Singularity image available with the relevant C++ libraries.
The dependencies for n-gram (SRILM) are also set in a Singularity image called in the script.
However, by default, you will also need numpy to convert the raw SRILM output to the standard
format. If you don't already have an active conda environment (which has numpy), simply use the command
module add openmind/anaconda before running the n-gram script.
Note that I did not add this line to the top of the eval_ngram.sh file because users may want
to run the n-gram model in their own preferred environments.
When submitting jobs to SLURM, keep in mind that different models have different memory/time requirements. The following settings have worked for me in the past:
| Model | Suggested memory | Speed | GPU |
|---|---|---|---|
| GRNN | 5G | Medium | Yes |
| JRNN | 20G | Medium | No |
| RNNG | 12G | Slow | No |
| Tiny | 5G | Fast | No |
| Trans | 5G | Fast | Yes |
| ngram | 5G | Fast | No |
The speed is relative to the other models; for reference, Tiny LSTM takes under 1 minute to calculate surprisal for 900 simple sentences (~7 words each), while RNNG takes several hours.
If using GPU, remember to request the appropriate resources in your sbatch call.
I may also add sample SLURM scripts if that would be helpful.
Content type
Image
Digest
Size
1 GB
Last updated
about 5 years ago
docker pull cpllab/language-models:gpt2