/
test.py
executable file
·91 lines (66 loc) · 3.12 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import sys
import time
import argparse
import dlm.utils as U
import dlm.io.logging as L
###############
## Arguments
#
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--test-file", dest="test_path", required=True, help="The evaluation file (memory-mapped, nbest list or text file)")
parser.add_argument("-f", "--format", dest="format", required=True, help="The evaluation file format (mmap|nbest|text)")
parser.add_argument("-v", "--vocab-file", dest="vocab_path", help="The vocabulary file that was used in training")
parser.add_argument("-m", "--model-file", dest="model_path", required=True, help="Input CoreLM model file")
parser.add_argument("-ulp", "--unnormalized-log-prob-file", dest="ulp_path", help="Output file for sentence-level UNNORMALIZED log-probabilities")
parser.add_argument("-nlp", "--normalized-log-prob-file", dest="nlp_path", help="Output file for sentence-level NORMALIZED log-probabilities")
parser.add_argument("-ppl", "--perplexity", action='store_true', help="Compute and output normalized perplexity")
parser.add_argument("-un", "--unnormalized", action='store_true', help="Compute and output unnormalized perplexity")
parser.add_argument("-d", "--device", dest="device", default="gpu", help="The computing device (cpu or gpu)")
args = parser.parse_args()
U.set_theano_device(args.device, 1)
from dlm.models.mlp import MLP
from dlm import eval
import theano
import theano.tensor as T
#########################
## Loading model
#
classifier = MLP(model_path=args.model_path)
#########################
## Loading dataset
#
U.xassert(args.format == "mmap" or args.format == "nbest" or args.format == "text", "Invalid file format given: " + args.format)
U.xassert(args.perplexity or args.nlp_path or args.ulp_path, "You should use one of (or more) -ppl, -nlp or -ulp")
if args.format == "mmap":
U.xassert((args.nlp_path is None) and (args.ulp_path is None), "Cannot compute log-probabilities for an mmap file")
from dlm.io.mmapReader import MemMapReader
testset = MemMapReader(dataset_path=args.test_path, batch_size=500)
else:
U.xassert(args.vocab_path, "Vocab file is required for non-mmap file formats")
from dlm.io.textReader import TextReader
is_nbest = False
if args.format == "nbest":
is_nbest = True
testset = TextReader(dataset_path=args.test_path, is_nbest=is_nbest, ngram_size=classifier.ngram_size, vocab_path=args.vocab_path)
#########################
## Compiling theano function
#
evaluator = eval.Evaluator(testset, classifier)
#########################
## Testing
#
start_time = time.time()
if args.perplexity:
L.info("Perplexity: %f" % (evaluator.perplexity()))
if args.unnormalized:
L.info("Unnormalized Perplexity: %f" % (evaluator.unnormalized_perplexity()))
if args.nlp_path:
with open(args.nlp_path, 'w') as output:
for i in xrange(testset.get_num_sentences()):
output.write(str(evaluator.get_sequence_log_prob(i)) + '\n')
if args.ulp_path:
with open(args.ulp_path, 'w') as output:
for i in xrange(testset.get_num_sentences()):
output.write(str(evaluator.get_unnormalized_sequence_log_prob(i)) + '\n')
L.info("Ran for %.2fs" % (time.time() - start_time))