-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
426 lines (329 loc) · 18.6 KB
/
models.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
import tensorflow as tf
import tensorflow_addons as tfa
from transformers import (AutoConfig,
TFBertForSequenceClassification,
TFBertModel,
TFDistilBertForSequenceClassification,
TFDistilBertModel,
TFRobertaModel,
TFXLMRobertaForSequenceClassification,
TFMT5ForConditionalGeneration,
TFT5ForConditionalGeneration)
def ModelCheckpoint(model_name: str)->tf.keras.callbacks.ModelCheckpoint:
return tf.keras.callbacks.ModelCheckpoint(model_name,
monitor = 'val_accuracy',
verbose = 1,
save_best_only = True,
save_weights_only = True,
mode = 'max',
period = 1)
### Multi-Lingual DistilBert ####################################################################################################
def create_distilmbert_model_v1(use_default_weights: bool,
custom_pretrained_model_checkpoint: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
lr: float)->tf.keras.Model:
# Set Model init specs
if use_default_weights:
model_type = 'distilbert-base-multilingual-cased'
from_pt = False
else:
model_type = custom_pretrained_model_checkpoint
from_pt = True
# Create 'Standard' Classification Model
with strategy.scope():
model = TFDistilBertForSequenceClassification.from_pretrained(model_type, config = config, from_pt = from_pt)
optimizer = tf.keras.optimizers.Adam(learning_rate = lr)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
model.compile(optimizer = optimizer, loss = loss, metrics = [metric])
return model
def create_distilmbert_model_v2(use_default_weights: bool,
custom_pretrained_model_checkpoint: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
max_len: int,
lr: float)->tf.keras.Model:
# Set Model init specs
if use_default_weights:
model_type = 'distilbert-base-multilingual-cased'
from_pt = False
else:
model_type = custom_pretrained_model_checkpoint
from_pt = True
# Create Custom Model
with strategy.scope():
input_ids = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'input_ids')
input_masks = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'attention_mask')
# Initializers
kernel_initializer = tf.keras.initializers.RandomNormal(mean = 0.0, stddev = 0.05, seed = None)
bias_initializer = tf.keras.initializers.RandomNormal(mean = 0.0, stddev = 0.05, seed = None)
transformers_model = TFDistilBertModel.from_pretrained(model_type, config = config, from_pt = from_pt)
last_hidden_states = transformers_model({'input_ids': input_ids, 'attention_mask': input_masks})
x = last_hidden_states[0][:, 0, :]
x = tf.keras.layers.Dropout(0.2)(x)
outputs = tf.keras.layers.Dense(2, kernel_initializer = kernel_initializer, bias_initializer = bias_initializer)(x)
model = tf.keras.Model(inputs = [input_ids, input_masks], outputs = outputs)
optimizer = tf.keras.optimizers.Adam(learning_rate = lr)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
# Compile
model.compile(optimizer = optimizer, loss = loss, metrics = [metric])
return model
def create_distilmbert_model_v3(model_type: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
max_len: int)->tf.keras.Model:
# Create Custom Model
with strategy.scope():
input_ids = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'input_ids')
input_masks = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'attention_mask')
transformers_model = TFDistilBertModel.from_pretrained('distilbert-base-multilingual-cased', config = config)
output_dict = transformers_model({'input_ids': input_ids, 'attention_mask': input_masks})
last_hidden_state = output_dict.last_hidden_state
outputs = last_hidden_state[:, 0, :]
model = tf.keras.Model(inputs = [input_ids, input_masks], outputs = outputs)
return model
### XLM-RoBERTa ######################################################################################################
def create_xlm_roberta_model_v1(use_default_weights: bool,
custom_pretrained_model_checkpoint: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
lr: float)->tf.keras.Model:
# Set Model init specs
if use_default_weights:
model_type = 'jplu/tf-xlm-roberta-base'
from_pt = False
else:
model_type = custom_pretrained_model_checkpoint
from_pt = True
# Create 'Standard' Classification Model
with strategy.scope():
model = TFXLMRobertaForSequenceClassification.from_pretrained(model_type, config = config, from_pt = from_pt)
optimizer = tf.keras.optimizers.Adam(learning_rate = lr)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
model.compile(optimizer = optimizer, loss = loss, metrics = [metric])
return model
def create_xlm_roberta_model_v2(use_default_weights: bool,
custom_pretrained_model_checkpoint: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
max_len: int,
lr: float)->tf.keras.Model:
# Set Model init specs
if use_default_weights:
model_type = 'jplu/tf-xlm-roberta-base'
from_pt = False
else:
model_type = custom_pretrained_model_checkpoint
from_pt = True
# Create Custom Model
with strategy.scope():
input_ids = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'input_ids')
input_masks = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'attention_mask')
# Initializers
kernel_initializer = tf.keras.initializers.RandomNormal(mean = 0.0, stddev = 0.05, seed = None)
bias_initializer = tf.keras.initializers.RandomNormal(mean = 0.0, stddev = 0.05, seed = None)
transformers_model = TFRobertaModel.from_pretrained(model_type, config = config, from_pt = from_pt)
last_hidden_states = transformers_model({'input_ids': input_ids, 'attention_mask': input_masks})
x = last_hidden_states[0][:, 0, :]
x = tf.keras.layers.Dropout(0.2)(x)
outputs = tf.keras.layers.Dense(2, kernel_initializer = kernel_initializer, bias_initializer = bias_initializer)(x)
model = tf.keras.Model(inputs = [input_ids, input_masks], outputs = outputs)
optimizer = tf.keras.optimizers.Adam(learning_rate = lr)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
# Compile
model.compile(optimizer = optimizer, loss = loss, metrics = [metric])
return model
def create_xlm_roberta_model_v3(model_type: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
max_len: int)->tf.keras.Model:
# Create Custom Model
with strategy.scope():
input_ids = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'input_ids')
input_masks = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'attention_mask')
transformers_model = TFRobertaModel.from_pretrained('jplu/tf-xlm-roberta-base', config = config)
output_dict = transformers_model({'input_ids': input_ids, 'attention_mask': input_masks})
last_hidden_state = output_dict.last_hidden_state
outputs = last_hidden_state[:, 0, :]
model = tf.keras.Model(inputs = [input_ids, input_masks], outputs = outputs)
return model
### Multi-Lingual BERT ######################################################################################################
def create_mbert_model_v1(use_default_weights: bool,
custom_pretrained_model_checkpoint: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
lr: float)->tf.keras.Model:
# Set Model init specs
if use_default_weights:
model_type = 'bert-base-multilingual-cased'
from_pt = False
else:
model_type = custom_pretrained_model_checkpoint
from_pt = True
# Create 'Standard' Classification Model
with strategy.scope():
model = TFBertForSequenceClassification.from_pretrained(model_type, config = config, from_pt = from_pt)
optimizer = tf.keras.optimizers.Adam(learning_rate = lr)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
model.compile(optimizer = optimizer, loss = loss, metrics = [metric])
return model
def create_mbert_model_v2(use_default_weights: bool,
custom_pretrained_model_checkpoint: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
max_len: int,
lr: float)->tf.keras.Model:
# Set Model init specs
if use_default_weights:
model_type = 'bert-base-multilingual-cased'
from_pt = False
else:
model_type = custom_pretrained_model_checkpoint
from_pt = True
# Create Custom Model
with strategy.scope():
input_ids = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'input_ids')
input_masks = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'attention_mask')
# Initializers
kernel_initializer = tf.keras.initializers.RandomNormal(mean = 0.0, stddev = 0.05, seed = None)
bias_initializer = tf.keras.initializers.RandomNormal(mean = 0.0, stddev = 0.05, seed = None)
transformers_model = TFBertModel.from_pretrained(model_type, config = config, from_pt = from_pt)
last_hidden_states = transformers_model({'input_ids': input_ids, 'attention_mask': input_masks})
x = last_hidden_states[0][:, 0, :]
x = tf.keras.layers.Dropout(0.2)(x)
outputs = tf.keras.layers.Dense(2, kernel_initializer = kernel_initializer, bias_initializer = bias_initializer)(x)
model = tf.keras.Model(inputs = [input_ids, input_masks], outputs = outputs)
optimizer = tf.keras.optimizers.Adam(learning_rate = lr)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
# Compile
model.compile(optimizer = optimizer, loss = loss, metrics = [metric])
return model
def create_mbert_model_v3(model_type: str,
strategy: tf.distribute.Strategy,
config: AutoConfig,
max_len: int)->tf.keras.Model:
# Create Custom Model
with strategy.scope():
input_ids = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'input_ids')
input_masks = tf.keras.layers.Input(shape = (max_len,), dtype = tf.int32, name = 'attention_mask')
transformers_model = TFBertModel.from_pretrained(model_type, config = config)
output_dict = transformers_model({'input_ids': input_ids, 'attention_mask': input_masks})
last_hidden_state = output_dict.last_hidden_state
outputs = last_hidden_state[:, 0, :]
model = tf.keras.Model(inputs = [input_ids, input_masks], outputs = outputs)
return model
### MT5 and ByT5 ############################################################################################################
class T5_Accuracy(tf.keras.metrics.Metric):
def __init__(self, label_length, name = 'accuracy', **kwargs):
super(T5_Accuracy, self).__init__(name = name, **kwargs)
self.t5_accuracy = self.add_weight(name = 'accuracy', initializer = 'zeros')
self.steps_counter = self.add_weight(name = 'steps_counter', initializer = 'zeros')
self.label_length = label_length
@tf.function
def update_state(self, y_true, y_pred, sample_weight = None):
# Reshape
y_pred = tf.reshape(y_pred, [-1, y_pred.shape[-1]])
y_true = tf.reshape(y_true, [-1])
# Get Max Indexes
y_pred = tf.math.argmax(y_pred, 1, output_type = 'int32')
# Cast to Int32
y_true = tf.cast(y_true, 'int32')
# Reshape according to max label length...we want to compare the exact predictions made.
y_pred = tf.reshape(y_pred, [-1, self.label_length])
y_true = tf.reshape(y_true, [-1, self.label_length])
# Compare Predicted and Labelled
y_comparison = tf.math.equal(y_pred, y_true)
accuracy = tf.keras.backend.mean(tf.cast(tf.math.reduce_all(y_comparison, 1), tf.keras.backend.floatx()))
self.t5_accuracy.assign_add(accuracy)
self.steps_counter.assign_add(tf.ones(shape = ()))
@tf.function
def result(self):
return self.t5_accuracy / self.steps_counter
@tf.function
def reset_state(self):
for var in self.variables:
var.assign(tf.zeros(shape = var.shape))
class KerasTFMT5ForConditionalGeneration(TFMT5ForConditionalGeneration):
def __init__(self, *args, log_dir = None, cache_dir = None, **kwargs):
super().__init__(*args, **kwargs)
self.loss_tracker= tf.keras.metrics.Mean(name = 'loss')
@tf.function
def train_step(self, data):
x = data
y = x['labels']
y = tf.reshape(y, [-1, 1])
with tf.GradientTape() as tape:
outputs = self(x, training = True)
loss = outputs[0]
logits = outputs[1]
loss = tf.reduce_mean(loss)
grads = tape.gradient(loss, self.trainable_variables)
self.optimizer.apply_gradients(zip(grads, self.trainable_variables))
self.loss_tracker.update_state(loss)
self.compiled_metrics.update_state(y, logits)
metrics = {m.name: m.result() for m in self.metrics}
return metrics
def test_step(self, data):
x = data
y = x['labels']
y = tf.reshape(y, [-1, 1])
output = self(x, training = False)
loss = output[0]
loss = tf.reduce_mean(loss)
logits = output[1]
self.loss_tracker.update_state(loss)
self.compiled_metrics.update_state(y, logits)
return {m.name: m.result() for m in self.metrics}
class KerasTFByT5ForConditionalGeneration(TFT5ForConditionalGeneration):
def __init__(self, *args, log_dir = None, cache_dir = None, **kwargs):
super().__init__(*args, **kwargs)
self.loss_tracker= tf.keras.metrics.Mean(name = 'loss')
@tf.function
def train_step(self, data):
x = data
y = x['labels']
y = tf.reshape(y, [-1, 1])
with tf.GradientTape() as tape:
outputs = self(x, training = True)
loss = outputs[0]
logits = outputs[1]
loss = tf.reduce_mean(loss)
grads = tape.gradient(loss, self.trainable_variables)
self.optimizer.apply_gradients(zip(grads, self.trainable_variables))
self.loss_tracker.update_state(loss)
self.compiled_metrics.update_state(y, logits)
metrics = {m.name: m.result() for m in self.metrics}
return metrics
def test_step(self, data):
x = data
y = x['labels']
y = tf.reshape(y, [-1, 1])
output = self(x, training = False)
loss = output[0]
loss = tf.reduce_mean(loss)
logits = output[1]
self.loss_tracker.update_state(loss)
self.compiled_metrics.update_state(y, logits)
return {m.name: m.result() for m in self.metrics}
def create_mt5_model(model_type: str, strategy: tf.distribute.Strategy, config: AutoConfig, lr: float, max_label_len: int, total_steps: int)->tf.keras.Model:
# Create Model
with strategy.scope():
radam = tfa.optimizers.RectifiedAdam(learning_rate = lr, total_steps = total_steps, warmup_proportion = 0.10, min_lr = lr/3.)
ranger = tfa.optimizers.Lookahead(radam, sync_period = 6, slow_step_size = 0.5)
model = KerasTFMT5ForConditionalGeneration.from_pretrained(model_type, config = config)
model.compile(optimizer = ranger, metrics = [T5_Accuracy(label_length = max_label_len)])
return model
def create_byt5_model(model_type: str, strategy: tf.distribute.Strategy, config: AutoConfig, lr: float, max_label_len: int, total_steps: int)->tf.keras.Model:
# Create Model
with strategy.scope():
radam = tfa.optimizers.RectifiedAdam(learning_rate = lr, total_steps = total_steps, warmup_proportion = 0.10, min_lr = lr/3.)
ranger = tfa.optimizers.Lookahead(radam, sync_period = 6, slow_step_size = 0.5)
model = KerasTFByT5ForConditionalGeneration.from_pretrained(model_type, config = config)
model.compile(optimizer = ranger, metrics = [T5_Accuracy(label_length = max_label_len)])
return model