forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_eval_model.py
236 lines (206 loc) · 7.71 KB
/
test_eval_model.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from parlai.scripts.eval_model import setup_args
import os
import unittest
import parlai.utils.testing as testing_utils
class TestEvalModel(unittest.TestCase):
"""
Basic tests on the eval_model.py example.
"""
def test_noevalmode(self):
"""
Ensure you get an error trying to use eval_model with -dt train.
"""
with self.assertRaises(ValueError):
testing_utils.eval_model(
{'task': 'integration_tests', 'model': 'repeat_label'},
valid_datatype='train',
)
def test_evalmode(self):
"""
Eval_model with -dt train:evalmode should be okay.
"""
testing_utils.eval_model(
{'task': 'integration_tests', 'model': 'repeat_label'},
valid_datatype='train:evalmode',
)
def test_output(self):
"""
Test output of running eval_model.
"""
parser = setup_args()
parser.set_defaults(
task='integration_tests',
model='repeat_label',
datatype='valid',
num_examples=5,
display_examples=False,
)
opt = parser.parse_args([], print_args=False)
valid, test = testing_utils.eval_model(opt)
self.assertEqual(valid['accuracy'], 1)
self.assertEqual(test['accuracy'], 1)
self.assertNotIn('rouge-L', valid)
self.assertNotIn('rouge-L', test)
def test_metrics_all(self):
"""
Test output of running eval_model.
"""
parser = setup_args()
parser.set_defaults(
task='integration_tests',
model='repeat_label',
datatype='valid',
num_examples=5,
display_examples=False,
metrics='all',
)
opt = parser.parse_args([], print_args=False)
valid, test = testing_utils.eval_model(opt)
self.assertEqual(valid['accuracy'], 1)
self.assertEqual(valid['rouge-L'], 1)
self.assertEqual(valid['rouge-1'], 1)
self.assertEqual(valid['rouge-2'], 1)
self.assertEqual(test['accuracy'], 1)
self.assertEqual(test['rouge-L'], 1)
self.assertEqual(test['rouge-1'], 1)
self.assertEqual(test['rouge-2'], 1)
def test_metrics_select(self):
"""
Test output of running eval_model.
"""
parser = setup_args()
parser.set_defaults(
task='integration_tests',
model='repeat_label',
datatype='valid',
num_examples=5,
display_examples=False,
metrics='accuracy,rouge',
)
opt = parser.parse_args([], print_args=False)
valid, test = testing_utils.eval_model(opt)
self.assertEqual(valid['accuracy'], 1)
self.assertEqual(valid['rouge-L'], 1)
self.assertEqual(valid['rouge-1'], 1)
self.assertEqual(valid['rouge-2'], 1)
self.assertEqual(test['accuracy'], 1)
self.assertEqual(test['rouge-L'], 1)
self.assertEqual(test['rouge-1'], 1)
self.assertEqual(test['rouge-2'], 1)
self.assertNotIn('bleu-4', valid)
self.assertNotIn('bleu-4', test)
def test_multitasking_metrics_macro(self):
valid, test = testing_utils.eval_model(
{
'task': 'integration_tests:candidate,'
'integration_tests:multiturnCandidate',
'model': 'random_candidate',
'num_epochs': 0.5,
'aggregate_micro': False,
}
)
task1_acc = valid['integration_tests:candidate/accuracy']
task2_acc = valid['integration_tests:multiturnCandidate/accuracy']
total_acc = valid['accuracy']
# task 2 is 4 times the size of task 1
self.assertEqual(
total_acc,
(task1_acc.value() + task2_acc.value()) * 0.5,
'Task accuracy is averaged incorrectly',
)
valid, test = testing_utils.eval_model(
{
'task': 'integration_tests:candidate,'
'integration_tests:multiturnCandidate',
'model': 'random_candidate',
'num_epochs': 0.5,
'aggregate_micro': False,
}
)
task1_acc = valid['integration_tests:candidate/accuracy']
task2_acc = valid['integration_tests:multiturnCandidate/accuracy']
total_acc = valid['accuracy']
# metrics are combined correctly
self.assertEqual(
total_acc,
(task1_acc.value() + task2_acc.value()) * 0.5,
'Task accuracy is averaged incorrectly',
)
def test_multitasking_metrics_micro(self):
valid, test = testing_utils.eval_model(
{
'task': 'integration_tests:candidate,'
'integration_tests:multiturnCandidate',
'model': 'random_candidate',
'num_epochs': 0.5,
'aggregate_micro': True,
}
)
task1_acc = valid['integration_tests:candidate/accuracy']
task2_acc = valid['integration_tests:multiturnCandidate/accuracy']
total_acc = valid['accuracy']
# task 2 is 4 times the size of task 1
self.assertEqual(
total_acc, task1_acc + task2_acc, 'Task accuracy is averaged incorrectly',
)
valid, test = testing_utils.eval_model(
{
'task': 'integration_tests:candidate,'
'integration_tests:multiturnCandidate',
'model': 'random_candidate',
'num_epochs': 0.5,
'aggregate_micro': True,
}
)
task1_acc = valid['integration_tests:candidate/accuracy']
task2_acc = valid['integration_tests:multiturnCandidate/accuracy']
total_acc = valid['accuracy']
# metrics are combined correctly
self.assertEqual(
total_acc, (task1_acc + task2_acc), 'Task accuracy is averaged incorrectly',
)
def test_train_evalmode(self):
"""
Test that evaluating a model with train:evalmode completes an epoch.
"""
base_dict = {'model': 'repeat_label', 'datatype': 'train:evalmode'}
teachers = ['integration_tests:fixed_dialog_candidate', 'integration_tests']
batchsize = [1, 64]
for bs in batchsize:
for teacher in teachers:
d = base_dict.copy()
d['task'] = teacher
d['batchsize'] = bs
with testing_utils.timeout(time=20):
valid, test = testing_utils.eval_model(
d, valid_datatype=d['datatype']
)
self.assertEqual(
int(valid['exs']),
500,
f'train:evalmode failed with bs {bs} and teacher {teacher}',
)
def test_save_report(self):
"""
Test that we can save report from eval model.
"""
with testing_utils.tempdir() as tmpdir:
save_report = os.path.join(tmpdir, 'report')
parser = setup_args()
parser.set_defaults(
task='integration_tests',
model='repeat_label',
datatype='valid',
num_examples=5,
display_examples=False,
save_world_logs=True,
report_filename=save_report,
)
opt = parser.parse_args([], print_args=False)
valid, test = testing_utils.eval_model(opt)
if __name__ == '__main__':
unittest.main()