|
31 | 31 | import zero_7_exp_decay, zero_95_exp_decay, simple_sigmoid |
32 | 32 | from ast import literal_eval |
33 | 33 | import time |
34 | | - |
| 34 | +from gc import collect |
35 | 35 |
|
36 | 36 | # |
37 | 37 | # Load the email data |
|
77 | 77 | training_x = [baseline_train_x] |
78 | 78 | train_labels = [baseline_train_y] |
79 | 79 |
|
| 80 | +# Package test set: |
| 81 | +test_x_tf = tf.constant(X_test, dtype=tf.string) |
| 82 | +test_y_tf = tf.constant(y_test, dtype=tf.int8) |
| 83 | + |
| 84 | +test_x_packaged = [test_x_tf] |
| 85 | +test_y_packaged = [test_y_tf] |
| 86 | + |
| 87 | + |
80 | 88 | # |
81 | 89 | # Input and output shapes |
82 | 90 | # |
|
86 | 94 | """### A custom GPT2 encoder layer for text embedding""" |
87 | 95 |
|
88 | 96 |
|
| 97 | +@tf.keras.utils.register_keras_serializable() |
89 | 98 | class GPT2Layer(tf.keras.layers.Layer): |
90 | 99 |
|
91 | 100 | def __init__(self, max_seq_length, **kwargs): |
@@ -190,6 +199,7 @@ def from_config(cls, config): |
190 | 199 | from transformers import AutoTokenizer |
191 | 200 | import tensorflow as tf |
192 | 201 |
|
| 202 | +@tf.keras.utils.register_keras_serializable() |
193 | 203 | class NewTokenizerLayer(tf.keras.layers.Layer): |
194 | 204 | def __init__(self, max_seq_length, tokenizer_checkpoint, **kwargs): |
195 | 205 | super().__init__(**kwargs) |
@@ -248,6 +258,7 @@ def from_config(cls, config): |
248 | 258 |
|
249 | 259 |
|
250 | 260 | # --- Updated RotaryEmbedding --- |
| 261 | +@tf.keras.utils.register_keras_serializable() |
251 | 262 | class RotaryEmbedding(tf.keras.layers.Layer): |
252 | 263 | def __init__(self, dim, max_seq_len=1024, temperature=10000.0, **kwargs): |
253 | 264 | super().__init__(**kwargs) |
@@ -347,6 +358,7 @@ def apply_rotary_pos_emb(x, sin, cos): |
347 | 358 | return x_rotated |
348 | 359 |
|
349 | 360 |
|
| 361 | +@tf.keras.utils.register_keras_serializable() |
350 | 362 | class InterleavedRoPE(tf.keras.layers.Layer): |
351 | 363 | def __init__(self, dim, max_seq_len=1024, **kwargs): |
352 | 364 | super().__init__(**kwargs) |
@@ -419,7 +431,7 @@ def from_config(cls, config): |
419 | 431 | # LayerNorm ... It degraded accuracy |
420 | 432 | # Just an FYI for anyone trying to apply conventional wisdom |
421 | 433 | # to save you the time ... |
422 | | -x = x = tf.keras.layers.Concatenate()([embedded, position_embedding]) |
| 434 | +x = tf.keras.layers.Concatenate()([embedded, position_embedding]) |
423 | 435 | x = tf.keras.layers.Dropout(0.4)(x) # AI suggested 0.4 |
424 | 436 | flattened = tf.keras.layers.Flatten()(x) |
425 | 437 |
|
@@ -528,4 +540,25 @@ def from_config(cls, config): |
528 | 540 | print(f'Cerebros best accuracy achieved is {result}') |
529 | 541 | print(f'val set accuracy') |
530 | 542 |
|
531 | | -# """### Testing the best model found""" |
| 543 | +"""### Testing the best model found""" |
| 544 | + |
| 545 | +MODEL_FILE_NAME = "cerebros-foundation-model.keras" |
| 546 | + |
| 547 | +best_model_found = cerebros_automl.get_best_model() |
| 548 | +best_model_found.save(MODEL_FILE_NAME) |
| 549 | +del(best_model_found) |
| 550 | +del(cerebros_automl) |
| 551 | +collect() |
| 552 | + |
| 553 | +reconstituted_model = tf.keras.models.load_model(MODEL_FILE_NAME) |
| 554 | +test_x_packaged = [test_x_tf] |
| 555 | +test_y_packaged = [test_y_tf] |
| 556 | + |
| 557 | +reconstituted_model.compile( |
| 558 | + loss='binary_crossentropy', |
| 559 | + metrics=['accuracy'] |
| 560 | +) |
| 561 | + |
| 562 | +results = reconstituted_model.evaluate(test_x_packaged, test_y_packaged) |
| 563 | +print("Test loss:", results[0]) |
| 564 | +print("Test accuracy:", results[-1]) |
0 commit comments