This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
forked from cookielee77/DAST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
executable file
·163 lines (134 loc) · 5.62 KB
/
evaluation.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import sys
import argparse
import glob
import tensorflow as tf
from nltk.translate.bleu_score import corpus_bleu, sentence_bleu, SmoothingFunction
smoothie = SmoothingFunction().method1
import network
from utils import *
from vocab import Vocabulary
from config import load_arguments
folder_path = 'samples/imdb_amazon/finetune/*.txt'
args = load_arguments()
args.batch_size = 50
# sometimes you need switch the label if the gt order is switched
pos_label = 1
neg_label = 0
# pos_label = 0
# neg_label = 1
def calculate_bleu(transfer_, ref_):
ref = []
hypo = []
for i in range(len(transfer_)):
hypo.append(transfer_[i].split())
ref.append([ref_[i].split()])
bleu = corpus_bleu(ref, hypo)
print("Bleu score on transfer sentences: %.4f" % bleu)
def make_batches(sents_):
sents = [x.split() for x in sents_]
lengths = [len(x) for x in sents]
assert len(sents) % args.batch_size == 0
begin = list(range(0, len(sents), args.batch_size))
end = begin[1:] + [len(sents)]
batches_text = []
batches_len = []
for i, j in zip(begin, end):
batches_text.append(sents[i : j])
batches_len.append(lengths[i : j])
return batches_text, batches_len
def calculate_acc(sess, model, vocab, sents):
total_correct = 0
batches_text, batches_lens = make_batches(sents)
for i in range(len(batches_text)):
batch_ids = batch_text_to_ids(batches_text[i], vocab)
# evaluate acc
feed_dict = {model.input: batch_ids,
model.enc_lens: batches_lens[i],
model.dropout: 1.0}
preds = sess.run(model.preds, feed_dict=feed_dict)
if i < 10:
total_correct += np.sum(preds == pos_label)
else:
total_correct += np.sum(preds == neg_label)
print('The total acc is: %f' % (total_correct/1000))
def calculate_domain_acc(sess, model, vocab, sents):
total_correct = 0
batches_text, batches_lens = make_batches(sents)
for i in range(len(batches_text)):
batch_ids = batch_text_to_ids(batches_text[i], vocab)
# evaluate acc
feed_dict = {model.input: batch_ids,
model.enc_lens: batches_lens[i],
model.dropout: 1.0}
preds = sess.run(model.preds, feed_dict=feed_dict)
total_correct += np.sum(preds == 1)
print('The total domain acc is: %f' % (total_correct/1000))
def calculate_gscore(sess, model, vocab, sents, refs):
total_gscore = 0
batches_text, batches_lens = make_batches(sents)
for i in range(len(batches_text)):
batch_ids = batch_text_to_ids(batches_text[i], vocab)
# evaluate acc
feed_dict = {model.input: batch_ids,
model.enc_lens: batches_lens[i],
model.dropout: 1.0}
preds = sess.run(model.probs, feed_dict=feed_dict)
if i < 10:
confids = preds[:, pos_label]
else:
confids = preds[:, neg_label]
for j in range(len(confids)):
idx = i * 50 + j
bleu = sentence_bleu([refs[idx].split()], sents[idx].split(), smoothing_function=smoothie)
bleu *= 100
confid = confids[j] * 100
gscore = 2 * confid * bleu / (confid + bleu)
total_gscore += gscore
print('The total g score is: %f' % (total_gscore/1000))
def load_file(file_path):
origin = []
ref = []
transfer = []
f = open(file_path, 'r')
lines = f.readlines()
for line in lines:
iterms = line.split('\t')
origin.append(iterms[0].strip())
ref.append(iterms[1].strip())
transfer.append(iterms[2].strip())
f.close()
return origin, ref, transfer
# elimiate the first variable scope, and restore the classifier from the path
def restore_classifier_by_path(classifier, classifier_path, scope):
new_vars = {}
for var in classifier.params:
pos = var.name.find('/')
# eliminate the first variable scope, e.g., target, source
new_vars[var.name[pos+1:-2]] = var
saver = tf.train.Saver(new_vars)
saver.restore(sess, os.path.join(classifier_path, 'model'))
logger.info("-----%s classifier model loading from %s successfully!-----" % (scope, classifier_path))
if __name__ == '__main__':
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
with tf.variable_scope('target'):
target_vocab = Vocabulary(args.target_vocab)
target_classifier = eval('network.classifier.CNN_Model')(args, target_vocab, 'target')
restore_classifier_by_path(target_classifier, args.target_classifier_path, 'target')
# classifier for domain accuracy evaluation
with tf.variable_scope('domain'):
assert args.domain_adapt, "domain_adapt arg should be True."
multi_vocab = Vocabulary(args.multi_vocab)
domain_classifier = eval('network.classifier.CNN_Model')(args, multi_vocab, 'domain')
restore_classifier_by_path(domain_classifier, args.domain_classifier_path, 'domain')
files = glob.glob(folder_path)
for file in files:
origin, ref, transfer = load_file(file)
print('##############################')
print('Evaluating %s file.' % file)
calculate_domain_acc(sess, domain_classifier, multi_vocab, transfer)
calculate_acc(sess, target_classifier, target_vocab, transfer)
calculate_bleu(transfer, ref)
calculate_gscore(sess, target_classifier, target_vocab, transfer, ref)