-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_executor.py
260 lines (231 loc) · 7.8 KB
/
test_executor.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
Copyright 2016 Deepgram
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import sys
import pytest
from kur.loss import MeanSquaredError, Ctc
from kur.optimizer import Adam
from kur.model import Executor
from kur.providers import BatchProvider
Executor.DEFAULT_RETRY_ENABLED = False
###############################################################################
@pytest.fixture
def ctc_eval_data(ctc_data):
""" Provides CTC data with only the evaluation-time input data available.
"""
for key, source in zip(ctc_data.keys, ctc_data.sources):
if key == 'TEST_input':
return BatchProvider(sources={key : source})
@pytest.fixture
def loss():
return MeanSquaredError()
@pytest.fixture
def ctc_loss():
return Ctc(
input_length='TEST_input_length',
output_length='TEST_transcription_length',
output='TEST_transcription'
)
@pytest.fixture
def optimizer():
return Adam()
###############################################################################
class TestExecutor:
""" Tests for the Executor (training/validation/testing).
"""
###########################################################################
def test_trainer_with_optimizer(self, simple_model, loss, optimizer):
""" Tests if we can compile a Executor instance with an optimizer.
"""
simple_model.parse(None)
simple_model.build()
trainer = Executor(
model=simple_model,
loss=loss,
optimizer=optimizer
)
trainer.compile()
###########################################################################
def test_trainer_without_optimizer(self, simple_model, loss):
""" Tests if we can compile a Executor instance without an optimizer.
"""
simple_model.parse(None)
simple_model.build()
trainer = Executor(
model=simple_model,
loss=loss
)
trainer.compile()
###########################################################################
def test_training(self, simple_model, loss, optimizer, simple_data):
""" Tests that a single training epoch can succeed.
"""
simple_model.parse(None)
simple_model.build()
trainer = Executor(
model=simple_model,
loss=loss,
optimizer=optimizer
)
trainer.train(provider=simple_data, stop_when={'epochs' : 1})
###########################################################################
def test_testing(self, simple_model, loss, simple_data):
""" Tests that a testing run can succeed.
"""
simple_model.parse(None)
simple_model.build()
trainer = Executor(
model=simple_model,
loss=loss
)
trainer.test(providers={'default' : simple_data})
###########################################################################
def test_evaluator(self, simple_model):
""" Tests if we can compile an Executor instance.
"""
simple_model.parse(None)
simple_model.build()
evaluator = Executor(
model=simple_model
)
evaluator.compile()
###########################################################################
def test_evaluating(self, simple_model, simple_data):
""" Tests that an evaluation run can succeed.
"""
simple_model.parse(None)
simple_model.build()
evaluator = Executor(
model=simple_model
)
evaluator.evaluate(provider=simple_data)
###########################################################################
def test_ctc_train(self, ctc_model, ctc_data, ctc_loss, optimizer):
""" Tests that we can compile and train a model using the CTC loss
function.
"""
ctc_model.parse(None)
ctc_model.register_provider(ctc_data)
ctc_model.build()
trainer = Executor(
model=ctc_model,
loss=ctc_loss,
optimizer=optimizer
)
trainer.train(provider=ctc_data, stop_when={'epochs' : 1})
###########################################################################
def test_ctc_test(self, ctc_model, ctc_data, ctc_loss):
""" Tests that we can compile and test a model using the CTC loss
function.
"""
ctc_model.parse(None)
ctc_model.register_provider(ctc_data)
ctc_model.build()
trainer = Executor(
model=ctc_model,
loss=ctc_loss
)
trainer.test(providers={'default' : ctc_data})
###########################################################################
def test_ctc_evaluating(self, ctc_model, ctc_eval_data):
""" Tests that we can evaluate a model that was trained with CTC loss.
"""
ctc_model.parse(None)
ctc_model.register_provider(ctc_eval_data)
ctc_model.build()
evaluator = Executor(
model=ctc_model
)
evaluator.evaluate(provider=ctc_eval_data)
###########################################################################
def test_uber_train(self, uber_model, uber_data, jinja_engine, loss,
optimizer):
""" Tests that we can compile and train a diverse model.
"""
if uber_model.get_backend().get_name() == 'keras' and \
uber_model.get_backend().keras_version() == 2 and \
uber_model.get_backend().get_toolchain() == 'tensorflow' and \
sys.version_info < (3, 5):
pytest.skip('Occassional SIGSEGV')
uber_model.parse(jinja_engine)
uber_model.register_provider(uber_data)
uber_model.build()
trainer = Executor(
model=uber_model,
loss=loss,
optimizer=optimizer
)
trainer.compile()
trainer.train(provider=uber_data, stop_when={'epochs' : 1})
###########################################################################
def test_uber_test(self, uber_model, uber_data, jinja_engine, loss):
""" Tests that we can compile and test a diverse model.
"""
uber_model.parse(jinja_engine)
uber_model.register_provider(uber_data)
uber_model.build()
trainer = Executor(
model=uber_model,
loss=loss
)
trainer.compile()
trainer.test(providers={'default' : uber_data})
###########################################################################
def test_uber_evaluating(self, uber_model, uber_data, jinja_engine):
""" Tests that we can evaluate a very diverse model.
"""
uber_model.parse(jinja_engine)
uber_model.register_provider(uber_data)
uber_model.build()
evaluator = Executor(
model=uber_model
)
evaluator.evaluate(provider=uber_data)
###########################################################################
def test_embedding_train(self, embedding_model, embedding_data, loss,
optimizer):
""" Tests that we can compile and train a model which has an Embedding.
"""
embedding_model.parse(None)
embedding_model.register_provider(embedding_data)
embedding_model.build()
trainer = Executor(
model=embedding_model,
loss=loss,
optimizer=optimizer
)
trainer.train(provider=embedding_data, stop_when={'epochs' : 1})
###########################################################################
def test_embedding_test(self, embedding_model, embedding_data, loss):
""" Tests that we can compile and test a model which has an Embedding.
function.
"""
embedding_model.parse(None)
embedding_model.register_provider(embedding_data)
embedding_model.build()
trainer = Executor(
model=embedding_model,
loss=loss
)
trainer.test(providers={'default' : embedding_data})
###########################################################################
def test_embedding_evaluating(self, embedding_model, embedding_data):
""" Tests that we can evaluate a model that has an Embedding.
"""
embedding_model.parse(None)
embedding_model.register_provider(embedding_data)
embedding_model.build()
evaluator = Executor(
model=embedding_model
)
evaluator.evaluate(provider=embedding_data)
### EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF.EOF