|
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 | # |
@@ -532,53 +540,25 @@ def from_config(cls, config): |
532 | 540 | print(f'Cerebros best accuracy achieved is {result}') |
533 | 541 | print(f'val set accuracy') |
534 | 542 |
|
535 | | -# """### Testing the best model found""" |
536 | | - |
537 | | -# Register custom objects for serialization |
538 | | -custom_objects = { |
539 | | - 'GPT2Layer': GPT2Layer, |
540 | | - 'NewTokenizerLayer': NewTokenizerLayer, |
541 | | - 'RotaryEmbedding': RotaryEmbedding, |
542 | | - 'InterleavedRoPE': InterleavedRoPE |
543 | | -} |
544 | | - |
545 | | -# Save the model with custom objects |
546 | | -gpt_baseline_model.save('gpt_baseline_model.h5', save_format='h5', custom_objects=custom_objects) |
547 | | -cerebros_base_model.save('cerebros_base_model.h5', save_format='h5', custom_objects=custom_objects) |
548 | | - |
549 | | -# Test loading the models back |
550 | | -print("Testing model loading...") |
551 | | -try: |
552 | | - # Load GPT baseline model |
553 | | - loaded_gpt_model = tf.keras.models.load_model('gpt_baseline_model.h5', custom_objects=custom_objects) |
554 | | - print("✓ GPT baseline model loaded successfully!") |
555 | | - |
556 | | - # Verify GPT model structure |
557 | | - print("GPT Model Summary:") |
558 | | - print(loaded_gpt_model.summary()) |
559 | | - |
560 | | - # Test GPT model prediction |
561 | | - test_input = tf.constant(["This is a test email for phishing detection."]) |
562 | | - gpt_prediction = loaded_gpt_model.predict(test_input) |
563 | | - print(f"GPT Model prediction shape: {gpt_prediction.shape}") |
564 | | - print(f"GPT Model prediction sample: {gpt_prediction[0]}") |
565 | | - |
566 | | - # Load Cerebros base model |
567 | | - loaded_cerebros_model = tf.keras.models.load_model('cerebros_base_model.h5', custom_objects=custom_objects) |
568 | | - print("✓ Cerebros base model loaded successfully!") |
569 | | - |
570 | | - # Verify Cerebros model structure |
571 | | - print("Cerebros Model Summary:") |
572 | | - print(loaded_cerebros_model.summary()) |
573 | | - |
574 | | - # Test Cerebros model prediction |
575 | | - cerebros_prediction = loaded_cerebros_model.predict(test_input) |
576 | | - print(f"Cerebros Model prediction shape: {cerebros_prediction.shape}") |
577 | | - print(f"Cerebros Model prediction sample shape: {cerebros_prediction[0].shape}") |
578 | | - |
579 | | - print("✓ All models loaded and validated successfully!") |
580 | | - |
581 | | -except Exception as e: |
582 | | - print(f"✗ Error loading models: {e}") |
583 | | - raise |
| 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 | +) |
584 | 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