-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify.py
executable file
·72 lines (54 loc) · 2.09 KB
/
classify.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
#!/usr/bin/env python
import sys
import time
import argparse
import dlm.utils as U
import dlm.io.logging as L
import numpy
###############
## Arguments
#
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-file", dest="input_path", required=True, help="Input feature file")
parser.add_argument("-v", "--vocab-file", dest="vocab_path", required=True, help="The vocabulary file for the model")
parser.add_argument("-rv", "--restricted-vocab-file", dest="restricted_vocab_path", help="Restricted vocab file to predict the word")
parser.add_argument("-m", "--model-file", dest="model_path", required=True, help="Input PrimeLM model file")
parser.add_argument("-o", "--output-file",dest="output_path", required=False, help="Output File path.")
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
#
from dlm.io.ngramsReader import NgramsReader
from dlm.io.vocabReader import VocabManager
testset = NgramsReader(dataset_path=args.input_path, ngram_size=classifier.ngram_size, vocab_path=args.vocab_path)
vocab = VocabManager(args.vocab_path)
## Loading restricted vocab
restricted_ids = []
restricted_vocab = []
if args.restricted_vocab_path:
with open(args.restricted_vocab_path) as restricted_vocab_file:
for line in restricted_vocab_file:
restricted_vocab.append(line.strip())
restricted_ids = vocab.get_ids_given_word_list(restricted_vocab)
#########################
## Compiling theano function
#
evaluator = eval.Evaluator(testset, classifier)
print evaluator.perplexity()
''' Uncomment to write the output class labels to a file
if args.output_path:
with open(args.output_path, 'w') as output:
for i in xrange(testset._get_num_samples()):
out = evaluator.get_class(i, restricted_ids)
output.write(vocab.get_word_given_id(out)+'\n')
'''