Evaluation code for various unsupervised automated metrics for Natural Language Generation.
Evaluation code for various unsupervised automated metrics for Natural Language Generation.
Evaluation code for various unsupervised automated metrics for NLG (Natural Language Generation). It takes as input a hypothesis file, and one or more references files and outputs values of metrics. Rows across these files should correspond to the same example.
Install Java 1.8.0 (or higher).
Install the Python dependencies, run:
pip install git+https://github.com/Maluuba/nlg-eval.git@masterIf you are using macOS High Sierra or higher, then run this to allow multithreading:
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YESSimple setup (download required data (e.g. models, embeddings) and external code files), run:
nlg-eval --setupIf you're setting this up from the source code or you're on Windows and not using a Bash terminal, then you might get errors about nlg-eval not being found.
You will need to find the nlg-eval script.
See here for details.
# If you don't like the default path (~/.cache/nlgeval) for the downloaded data,
# then specify a path where you want the files to be downloaded.
# The value for the data path is stored in ~/.config/nlgeval/rc.json and can be overwritten by
# setting the NLGEVAL_DATA environment variable.
nlg-eval --setup ${data_path}(These examples were made with Git Bash on Windows)
All of the data files should have been downloaded, you should see sizes like:
$ ls -l ~/.cache/nlgeval/
total 6003048
-rw-r--r-- 1 ... 289340074 Sep 12 2018 bi_skip.npz
-rw-r--r-- 1 ... 689 Sep 12 2018 bi_skip.npz.pkl
-rw-r--r-- 1 ... 2342138474 Sep 12 2018 btable.npy
-rw-r--r-- 1 ... 7996547 Sep 12 2018 dictionary.txt
-rw-r--r-- 1 ... 21494787 Jan 22 2019 glove.6B.300d.model.bin
-rw-r--r-- 1 ... 480000128 Jan 22 2019 glove.6B.300d.model.bin.vectors.npy
-rw-r--r-- 1 ... 663989216 Sep 12 2018 uni_skip.npz
-rw-r--r-- 1 ... 693 Sep 12 2018 uni_skip.npz.pkl
-rw-r--r-- 1 ... 2342138474 Sep 12 2018 utable.npyYou can also verify some checksums:
…If you're ensure that the setup was successful, you can run the tests:
pip install pytest
pytestIt might take a few minutes and you might see warnings but they should pass.
Once setup has completed, the metrics can be evaluated with a Python API or in the command line.
Examples of the Python API can be found in test_nlgeval.py.
nlg-eval --hypothesis=examples/hyp.txt --references=examples/ref1.txt --references=examples/ref2.txtwhere each line in the hypothesis file is a generated sentence and the corresponding lines across the reference files are ground truth reference sentences for the corresponding hypothesis.
from nlgeval import compute_metrics
metrics_dict = compute_metrics(hypothesis='examples/hyp.txt',
references=['examples/ref1.txt', 'examples/ref2.txt'])from nlgeval import compute_individual_metrics
metrics_dict = compute_individual_metrics(references, hypothesis)where references is a list of ground truth reference text strings and
hypothesis is the hypothesis text string.
from nlgeval import NLGEval
nlgeval = NLGEval() # loads the models
metrics_dict = nlgeval.compute_individual_metrics(references, hypothesis)where references is a list of ground truth reference text strings and
hypothesis is the hypothesis text string.
from nlgeval import NLGEval
nlgeval = NLGEval() # loads the models
metrics_dict = nlgeval.compute_metrics(references, hypothesis)where references is a list of lists of ground truth reference text strings and
hypothesis is a list of hypothesis text strings. Each inner list in references
is one set of references for the hypothesis (a list of single reference strings for
each sentence in hypothesis in the same order).
If you use this code as part of any published research, please cite the following paper:
Shikhar Sharma, Layla El Asri, Hannes Schulz, and Jeremie Zumer. "Relevance of Unsupervised Metrics in Task-Oriented Dialogue for Evaluating Natural Language Generation" arXiv preprint arXiv:1706.09799 (2017)
@article{sharma2017nlgeval,
author = {Sharma, Shikhar and El Asri, Layla and Schulz, Hannes and Zumer, Jeremie},
title = {Relevance of Unsupervised Metrics in Task-Oriented Dialogue for Evaluating Natural Language Generation},
journal = {CoRR},
volume = {abs/1706.09799},
year = {2017},
url = {http://arxiv.org/abs/1706.09799}
}Running
nlg-eval --hypothesis=examples/hyp.txt --references=examples/ref1.txt --references=examples/ref2.txtgives
Bleu_1: 0.550000
Bleu_2: 0.428174
Bleu_3: 0.284043
Bleu_4: 0.201143
METEOR: 0.295797
ROUGE_L: 0.522104
CIDEr: 1.242192
SPICE: 0.312331
SkipThoughtsCosineSimilarity: 0.626149
EmbeddingAverageCosineSimilarity: 0.884690
VectorExtremaCosineSimilarity: 0.568696
GreedyMatchingScore: 0.784205If you have issues with Meteor then you can try lowering the mem variable in meteor.py
CIDEr by default (with idf parameter set to "corpus" mode) computes IDF values using the reference sentences provided. Thus, CIDEr score for a reference dataset with only 1 image (or example for NLG) will be zero. When evaluating using one (or few) images, set idf to "coco-val-df" instead, which uses IDF from the MSCOCO Vaildation Dataset for reliable results. This has not been adapted in this code. For this use-case, apply patches from vrama91/coco-caption.
To mount an already prepared data directory to a Docker container or share it between
users, you can set the NLGEVAL_DATA environment variable to let nlg-eval know
where to find its models and data. E.g.
NLGEVAL_DATA=~/workspace/nlg-eval/nlgeval/dataThis variable overrides the value provided during setup (stored in ~/.config/nlgeval/rc.json)
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
See LICENSE.md.
No open issues yet, or sync has not completed.