/
model.py
428 lines (353 loc) · 13.7 KB
/
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""Base class for models."""
import abc
import tensorflow as tf
from opennmt import optimizers
from opennmt import schedules
from opennmt.utils import exporters
from opennmt.utils import losses
from opennmt.utils import misc
class Model(tf.keras.layers.Layer):
"""Base class for models."""
def __init__(self, examples_inputter):
super(Model, self).__init__()
self.examples_inputter = examples_inputter
self.params = {}
self.initialized = False
self._frozen_layers = False
@property
def unsupervised(self):
"""Unsupervised model."""
return self.labels_inputter is None
@property
def features_inputter(self):
"""The inputter producing features."""
return getattr(self.examples_inputter, "features_inputter", self.examples_inputter)
@property
def labels_inputter(self):
"""The inputter producing labels."""
return getattr(self.examples_inputter, "labels_inputter", None)
@property
def trainable_weights(self):
if not self._frozen_layers:
self._frozen_layers = True
freeze_layers = self.params.get("freeze_layers")
if freeze_layers:
if not isinstance(freeze_layers, list):
freeze_layers = [freeze_layers]
for layer_path in freeze_layers:
layer = misc.index_structure(self, layer_path)
layer.trainable = False
return super(Model, self).trainable_weights
@property
def ctranslate2_spec(self):
"""The equivalent CTranslate2 model specification."""
return None
def auto_config(self, num_replicas=1):
"""Returns automatic configuration values specific to this model.
Args:
num_replicas: The number of concurrent model replicas used for the
training.
Returns:
A partial training configuration.
"""
_ = num_replicas
return {}
def initialize(self, data_config, params=None):
"""Initializes the model from the data configuration.
Args:
data_config: A dictionary containing the data configuration set
by the user (e.g. vocabularies, tokenization, pretrained embeddings,
etc.).
params: A dictionary of hyperparameters.
"""
if params is None:
params = {}
self.params.update(params)
dropout = self.params.get("dropout")
if dropout is not None:
misc.set_dropout(self, dropout)
self.examples_inputter.initialize(data_config)
self.initialized = True
def build(self, input_shape):
self.examples_inputter.build(input_shape)
self.built = True
def __call__(self, *args, **kwargs): # pylint: disable=arguments-differ
if not self.initialized:
raise ValueError("The model should be first initialized with initialize()")
return super(Model, self).__call__(*args, **kwargs)
@abc.abstractmethod
def call(self, features, labels=None, training=None, step=None): # pylint: disable=arguments-differ
"""Runs the model.
Args:
features: A nested structure of features ``tf.Tensor``.
labels: A nested structure of labels ``tf.Tensor``.
training: Run in training mode.
step: The current training step.
Returns:
A tuple containing,
- The model outputs (usually unscaled probabilities).
- The model predictions.
"""
raise NotImplementedError()
def infer(self, features):
"""Runs inference on :obj:`features`.
This is a small convenience wrapper around
:meth:`opennmt.models.Model.call`.
Args:
features: A nested structure of features ``tf.Tensor``.
Returns:
The model predictions.
"""
_, predictions = self(features)
if "index" in features:
predictions["index"] = features["index"]
return predictions
def evaluate(self, features, labels):
"""Evaluates :obj:`features` predictions against `labels`.
Args:
features: A nested structure of features ``tf.Tensor``.
labels: A nested structure of features ``tf.Tensor``.
Returns:
The loss and predictions.
"""
outputs, predictions = self(features, labels=labels)
loss = self.compute_loss(outputs, labels, training=False)
return loss, predictions
def score(self, features, labels):
"""Scores labels.
Args:
features: A nested structure of features ``tf.Tensor``.
labels: A nested structure of labels ``tf.Tensor``.
Returns:
The score results.
"""
raise NotImplementedError("This model does not define a score function")
@abc.abstractmethod
def compute_loss(self, outputs, labels, training=True):
"""Computes the loss.
Args:
outputs: The model outputs (usually unscaled probabilities).
labels: The dict of labels ``tf.Tensor``.
training: Compute training loss.
Returns:
The loss or a tuple ``(numerator, train_denominator, stats_denominator)``
to use a different normalization for training compared to reporting (e.g.
batch-normalized for training vs. token-normalized for reporting).
"""
raise NotImplementedError()
def regularize_loss(self, loss, variables=None):
"""Regularizes the loss.
Args:
loss: The loss.
variables: List of variables.
Returns:
The regularized loss.
"""
if variables is None:
variables = self.trainable_variables
regularization = self.params.get("regularization")
if regularization is not None:
loss += losses.regularization_penalty(
regularization["type"], regularization["scale"], variables)
return loss
def get_metrics(self):
"""Returns the metrics for this model.
Returns:
A dictionary of ``tf.keras.metrics.Metric`` metrics.
"""
return None
def update_metrics(self, metrics, predictions, labels): # pylint: disable=unused-argument
"""Computes additional metrics on the predictions.
Args:
metrics: A dictionary of metrics to update.
predictions: The model predictions.
labels: The dict of labels ``tf.Tensor``.
"""
return
def get_optimizer(self):
"""Returns the optimizer for this model.
Returns:
A ``tf.keras.optimizers.Optimizer`` instance.
"""
params = self.params
learning_rate = tf.constant(params["learning_rate"], dtype=tf.float32)
if params.get("decay_type") is not None:
schedule_params = params.get("decay_params", {})
learning_rate = schedules.make_learning_rate_schedule(
learning_rate,
params["decay_type"],
schedule_params=schedule_params,
start_step=params.get("start_decay_steps", 0),
minimum_learning_rate=params.get("minimum_learning_rate", 0))
optimizer_params = params.get("optimizer_params")
if optimizer_params is None:
optimizer_params = {}
optimizer = optimizers.make_optimizer(
params["optimizer"], learning_rate, **optimizer_params)
return optimizer
def serve_function(self):
"""Returns a function for serving this model.
Returns:
A ``tf.function``.
"""
# Set name attribute of the input TensorSpec.
input_signature = {
name:tf.TensorSpec.from_spec(spec, name=name)
for name, spec in self.features_inputter.input_signature().items()}
@tf.function(input_signature=(input_signature,))
def _run(features):
features = self.features_inputter.make_features(features=features.copy())
_, predictions = self(features)
return predictions
return _run
def export(self, export_dir, exporter=None):
"""Exports the model for serving.
Args:
export_dir: The output directory.
exporter: A :class:`opennmt.utils.Exporter` instance. Defaults to
:class:`opennmt.utils.SavedModelExporter`.
"""
if exporter is None:
exporter = exporters.SavedModelExporter()
exporter.export(self, export_dir)
def create_variables(self, optimizer=None):
"""Creates the model variables by running it once.
Args:
optimizer: If set, also create the optimizer variables.
"""
# Create input features from the input signatures. We remove the leading
# batch dimension as sometimes assumed by make_features methods and set
# unspecified dimensions to 1.
features = tf.nest.map_structure(
lambda spec: tf.fill(
[dim or 1 for dim in spec.shape.as_list()[1:]],
tf.constant("a" if spec.dtype is tf.string else 1, dtype=spec.dtype)),
self.examples_inputter.input_signature())
features = self.examples_inputter.make_features(features=features)
# Add the batch dimension back before calling the model.
features, labels = tf.nest.map_structure(lambda x: tf.expand_dims(x, 0), features)
_ = self(features, labels=labels, training=True, step=0)
if optimizer is not None:
_ = optimizer.iterations
optimizer._create_hypers() # pylint: disable=protected-access
optimizer._create_slots(self.trainable_variables) # pylint: disable=protected-access
def transfer_weights(self, new_model, new_optimizer=None, optimizer=None, ignore_weights=None):
"""Transfers weights (and optionally optimizer slots) from this model to
another.
This default implementation assumes that :obj:`self` and :obj:`new_model`
have exactly the same variables. Subclasses can override this method to
transfer weights to another model type or architecture. For example,
:class:`opennmt.models.SequenceToSequence` can transfer weights to a model
with a different vocabulary.
All model and optimizer variables are expected to be initialized.
Args:
new_model: The new model to transfer weights to.
new_optimizer: The new optimizer.
optimizer: The optimizer used for the current model.
ignore_weights: Optional list of weights to not transfer.
"""
if type(self) is not type(new_model):
raise ValueError("Transferring weights to another model type is not supported")
if ignore_weights is None:
ignore_weights = set()
ignore_weights_ref = set(weight.experimental_ref() for weight in ignore_weights)
weights = self.weights
new_weights = new_model.weights
for weight, new_weight in zip(weights, new_weights):
if new_weight.experimental_ref() not in ignore_weights_ref:
new_weight.assign(weight)
if new_optimizer is not None and optimizer is not None:
for slot_name in new_optimizer.get_slot_names():
if slot_name not in optimizer.get_slot_names():
continue
new_slot = new_optimizer.get_slot(new_weight, slot_name)
slot = optimizer.get_slot(weight, slot_name)
new_slot.assign(slot)
def map_v1_weights(self, weights):
"""Maps current weights to V1 weights.
Args:
weights: A nested dictionary following the scope names used in V1. The
leaves are tuples with the variable value and optionally the optimizer
slots.
Returns:
A list of tuples associating variables and their V1 equivalent.
"""
raise NotImplementedError("This model can not restore V1 checkpoints")
def export_assets(self, asset_dir):
"""Exports additional assets used by this model.
Args:
asset_dir: The directory where assets can be written.
Returns:
A dictionary of additional assets.
"""
return self.examples_inputter.export_assets(asset_dir)
def visualize(self, log_dir):
"""Setups model visualization (e.g. word embedding projections).
Args:
log_dir: The log directory.
"""
self.features_inputter.visualize(self, log_dir)
if not self.unsupervised:
self.labels_inputter.visualize(self, log_dir)
def print_prediction(self, prediction, params=None, stream=None):
"""Prints the model prediction.
Args:
prediction: The evaluated prediction.
params: (optional) Dictionary of formatting parameters.
stream: (optional) The stream to print to.
"""
_ = params
print(prediction, file=stream)
def print_score(self, score, params=None, stream=None):
"""Prints the score result.
Args:
score: The score result (output of :meth:`opennmt.models.Model.score`).
params: (optional) Dictionary of formatting parameters.
stream: (optional) The stream to print to.
"""
_ = params
print(score, file=stream)
class SequenceGenerator(Model):
"""Base class for models generating sequences."""
@property
def decoder_inputter(self):
"""The inputter used on the decoder side."""
return (
self.labels_inputter if not self.unsupervised
else self.features_inputter)
def score(self, features, labels):
outputs, _ = self(features, labels=labels)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels["ids_out"], outputs["logits"])
weights = tf.sequence_mask(labels["length"], dtype=cross_entropy.dtype)
masked_cross_entropy = cross_entropy * weights
scores = tf.reduce_sum(masked_cross_entropy, axis=1)
results = {
"cross_entropy": cross_entropy,
"score": scores,
"tokens": labels["tokens"],
"length": self.decoder_inputter.get_length(labels, ignore_special_tokens=True)
}
if "attention" in outputs:
results["attention"] = outputs["attention"]
return results
def print_score(self, score, params=None, stream=None):
if params is None:
params = {}
length = score["length"]
tokens = score["tokens"][:length]
sentence = self.decoder_inputter.tokenizer.detokenize(tokens)
token_level_scores = None
attention = None
if params.get("with_token_level"):
token_level_scores = score["cross_entropy"][:length]
if "attention" in score:
attention = score["attention"][:length]
alignment_type = params.get("with_alignments")
sentence = misc.format_translation_output(
sentence,
score=score["score"],
token_level_scores=token_level_scores,
attention=attention,
alignment_type=alignment_type)
misc.print_as_bytes(sentence, stream=stream)