Skip to content

Commit 775470e

Browse files
Update phishing_email_detection_gpt2.py
Attempt to correct model serialization and make a better test of reconstituted model.
1 parent 44946da commit 775470e

File tree

1 file changed

+30
-50
lines changed

1 file changed

+30
-50
lines changed

phishing_email_detection_gpt2.py

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import zero_7_exp_decay, zero_95_exp_decay, simple_sigmoid
3232
from ast import literal_eval
3333
import time
34-
34+
from gc import collect
3535

3636
#
3737
# Load the email data
@@ -77,6 +77,14 @@
7777
training_x = [baseline_train_x]
7878
train_labels = [baseline_train_y]
7979

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+
8088
#
8189
# Input and output shapes
8290
#
@@ -532,53 +540,25 @@ def from_config(cls, config):
532540
print(f'Cerebros best accuracy achieved is {result}')
533541
print(f'val set accuracy')
534542

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+
)
584561

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

Comments
 (0)