Help me make my code reproducible by setting seed. Give me the entire code:
Install and import required packages
!pip install keras-tuner --upgrade
Import required packages
import pandas as pd
import numpy as np
For data preprocessing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
For performance metrics
from sklearn.metrics import (accuracy_score, precision_score, recall_score,
f1_score, roc_auc_score, classification_report,
confusion_matrix)
For building the neural network
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv1D, MaxPooling1D, Flatten, Dense, Dropout,
BatchNormalization)
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
For hyperparameter tuning
import keras_tuner as kt
from keras_tuner.tuners import RandomSearch
Ensure TensorFlow version is compatible with Keras Tuner
X = data[features]
y = data['status'] # 'status' is 'legitimate' or 'phishing'
Map the 'status' to numerical labels
label_mapping = {'legitimate': 0, 'phishing': 1}
y = y.map(label_mapping)
Check for any unmapped values or missing values
if y.isnull().any():
print("Some 'status' values were not mapped properly. Please check the 'status' column for unexpected values.")
print(y[y.isnull()])
# Handle unmapped values if necessary
# For example, you can drop them or assign a default value
# y = y.fillna(0)
Handle missing values in features if any
X.fillna(0, inplace=True)
Split into training and testing sets (80% train, 20% test)
tuner.search(
x=X_train_cnn,
y=y_train,
epochs=50,
validation_split=0.2,
callbacks=[early_stopping],
verbose=1 # Show progress bar
)
Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"""
The hyperparameter search is complete.
The optimal number of filters in the first Conv1D layer is {best_hps.get('filters_1')},
kernel size is {best_hps.get('kernel_size_1')},
and the learning rate is {best_hps.get('lr')}.
""")
Build the model with the best hyperparameters
best_model = tuner.hypermodel.build(best_hps)
Train the best model
history = best_model.fit(
X_train_cnn, y_train,
epochs=50,
validation_split=0.2,
callbacks=[early_stopping],
verbose=1 # Show progress bar
)
Certainly! Ensuring reproducibility in machine learning experiments is crucial for verifying results and facilitating collaboration. To achieve reproducible results in your code, you need to set seeds for all sources of randomness. This includes Python’s built-in random module, NumPy, and TensorFlow. Additionally, setting certain environment variables can help enforce deterministic operations in TensorFlow.
Below is your complete code augmented with reproducibility settings. I've added comments to highlight the changes and explain each step.
python
# Install and upgrade required packages
!pip install keras-tuner --upgrade
# =============================================
# Reproducibility Settings
# =============================================
import os
import random
import numpy as np
import tensorflow as tf
# Define a seed value for reproducibility
seed = 42
# 1. Set the PYTHONHASHSEED environment variable for Python hash-based operations
os.environ['PYTHONHASHSEED'] = str(seed)
# 2. Set the Python built-in pseudo-random generator at a fixed value
random.seed(seed)
# 3. Set the NumPy pseudo-random generator at a fixed value
np.random.seed(seed)
# 4. Set the TensorFlow pseudo-random generator at a fixed value
tf.random.set_seed(seed)
# 5. (Optional) Configure TensorFlow to use deterministic operations
# Note: This may impact performance and may not cover all operations
os.environ['TF_DETERMINISTIC_OPS'] = '1'
# =============================================
# Import Required Packages
# =============================================
import pandas as pd
# For data preprocessing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# For performance metrics
from sklearn.metrics import (accuracy_score, precision_score, recall_score,
f1_score, roc_auc_score, classification_report,
confusion_matrix)
# For building the neural network
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv1D, MaxPooling1D, Flatten, Dense, Dropout,
BatchNormalization)
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
# For hyperparameter tuning
import keras_tuner as kt
from keras_tuner.tuners import RandomSearch
# =============================================
# Version Information (Optional)
# =============================================
print(f'TensorFlow version: {tf.__version__}')
print(f'Keras Tuner version: {kt.__version__}')
# =============================================
# Data Loading and Preprocessing
# =============================================
# Read the dataset
data = pd.read_csv('dataset_phishing.csv')
# Define features and target variable
features = ['shortest_word_path', 'ratio_intMedia', 'links_in_tags',
'nb_hyphens', 'page_rank', 'avg_word_path',
'ratio_extHyperlinks', 'longest_words_raw', 'google_index',
'length_hostname', 'longest_word_host',
'domain_registration_length', 'nb_www', 'nb_underscore',
'nb_dots', 'ratio_extMedia', 'phish_hints', 'domain_in_title',
'web_traffic', 'safe_anchor', 'nb_space', 'shortening_service',
'ip', 'domain_age', 'nb_qm', 'nb_hyperlinks', 'nb_slash']
X = data[features]
y = data['status'] # 'status' is 'legitimate' or 'phishing'
# Map the 'status' to numerical labels
label_mapping = {'legitimate': 0, 'phishing': 1}
y = y.map(label_mapping)
# Check for any unmapped values or missing values
if y.isnull().any():
print("Some 'status' values were not mapped properly. Please check the 'status' column for unexpected values.")
print(y[y.isnull()])
# Handle unmapped values if necessary
# For example, you can drop them or assign a default value
# y = y.fillna(0)
# Handle missing values in features if any
X.fillna(0, inplace=True)
# Split into training and testing sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.20, random_state=seed, stratify=y)
# Standardize the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Reshape data to be suitable for Conv1D (samples, timesteps, features)
# Since we have numerical features, we treat each feature as a "time step"
X_train_cnn = X_train_scaled.reshape(-1, X_train_scaled.shape[1], 1)
X_test_cnn = X_test_scaled.reshape(-1, X_test_scaled.shape[1], 1)
# =============================================
# Model Building and Hyperparameter Tuning
# =============================================
def build_model(hp):
model = Sequential()
# First Conv1D layer
model.add(Conv1D(
filters=hp.Int('filters_1', min_value=16, max_value=128, step=16),
kernel_size=hp.Int('kernel_size_1', min_value=2, max_value=5, step=1),
activation='relu',
input_shape=(X_train_cnn.shape[1], 1),
padding='same'
))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(hp.Float('dropout_rate_1', min_value=0.0, max_value=0.5, step=0.1)))
# Optional second Conv1D layer
if hp.Boolean('second_conv_layer'):
model.add(Conv1D(
filters=hp.Int('filters_2', min_value=16, max_value=128, step=16),
kernel_size=hp.Int('kernel_size_2', min_value=2, max_value=5, step=1),
activation='relu',
padding='same'
))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(hp.Float('dropout_rate_2', min_value=0.0, max_value=0.5, step=0.1)))
model.add(Flatten())
# Dense layers
for i in range(hp.Int('num_dense_layers', 1, 3)):
model.add(Dense(
units=hp.Int(f'units_{i}', min_value=32, max_value=256, step=32),
activation='relu'
))
model.add(BatchNormalization())
model.add(Dropout(hp.Float(f'dropout_rate_dense_{i}', min_value=0.0, max_value=0.5, step=0.1)))
# Output layer
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(
optimizer=Adam(
hp.Float('lr', min_value=1e-4, max_value=1e-2, sampling='LOG', default=1e-3)
),
loss='binary_crossentropy',
metrics=['accuracy']
)
return model
# Initialize the tuner
tuner = RandomSearch(
build_model,
objective='val_accuracy',
max_trials=20,
executions_per_trial=1,
directory='tuner_dir',
project_name='phishing_url_cnn',
seed=seed, # Set the seed for the tuner
overwrite=True # Overwrite previous tuner results if any
)
# Early stopping callback to prevent overfitting
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
# Run the hyperparameter search
tuner.search(
x=X_train_cnn,
y=y_train,
epochs=50,
validation_split=0.2,
callbacks=[early_stopping],
verbose=1 # Show progress bar
)
# Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"""
The hyperparameter search is complete.
The optimal number of filters in the first Conv1D layer is {best_hps.get('filters_1')},
kernel size is {best_hps.get('kernel_size_1')},
and the learning rate is {best_hps.get('lr')}.
""")
# Build the model with the best hyperparameters
best_model = tuner.hypermodel.build(best_hps)
# Train the best model
history = best_model.fit(
X_train_cnn, y_train,
epochs=50,
validation_split=0.2,
callbacks=[early_stopping],
verbose=1 # Show progress bar
)
# =============================================
# Model Evaluation
# =============================================
# Evaluate the model on test data
test_loss, test_acc = best_model.evaluate(X_test_cnn, y_test)
print('Test Accuracy: {:.4f}'.format(test_acc))
# Predict probabilities and classes
y_pred_proba = best_model.predict(X_test_cnn).ravel()
y_pred = (y_pred_proba >= 0.5).astype(int)
# Calculate and print metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba)
print('\nModel Performance Metrics:')
print('--------------------------')
print('Accuracy : {:.4f}'.format(accuracy))
print('Precision : {:.4f}'.format(precision))
print('Recall : {:.4f}'.format(recall))
print('F1 Score : {:.4f}'.format(f1))
print('ROC AUC : {:.4f}'.format(roc_auc))
print('\nConfusion Matrix:')
print(confusion_matrix(y_test, y_pred))
print('\nClassification Report:')
print(classification_report(y_test, y_pred))
Key Changes and Additions for Reproducibility
Import Additional Modules for Seed Setting:
python
import os
import random
Set a Fixed Seed Value:
python
seed = 42
Configure Environment Variables and Seed Generators:
PYTHONHASHSEED: Ensures that hash-based operations are deterministic.
random.seed(seed): Sets the seed for Python's built-in random module.
np.random.seed(seed): Sets the seed for NumPy's random number generator.
tf.random.set_seed(seed): Sets the seed for TensorFlow's random number generator.
TF_DETERMINISTIC_OPS: Forces TensorFlow to use deterministic algorithms, enhancing reproducibility. Note: This might impact performance and doesn't cover all operations.
Pass the Seed to Keras Tuner:
python
tuner = RandomSearch(
build_model,
objective='val_accuracy',
max_trials=20,
executions_per_trial=1,
directory='tuner_dir',
project_name='phishing_url_cnn',
seed=seed, # Ensures Keras Tuner's randomness is seeded
overwrite=True # Overwrites any previous tuner results
)
Ensure All Randomness is Controlled Within the Model Building Function:
While the global seeds handle most randomness, if there are any additional sources of randomness within or other custom functions, ensure they also respect the seed.
Additional Recommendations for Enhanced Reproducibility
Environment Specification: Consider using a virtual environment or Docker container to encapsulate all dependencies and their specific versions.
Fixed max_trials and executions_per_trial: Ensure that hyperparameter tuning runs the same number of trials with the same configurations each time.
Logging and Saving Models: Save the trained models and logging information to ensure that you can retrieve the exact model state in the future.
Hardware Considerations: Running the code on different hardware (e.g., GPU vs. CPU) can sometimes lead to slight variations. For utmost reproducibility, use the same hardware setup.
Version Pinning: Explicitly specify versions for all packages (e.g., TensorFlow, Keras, scikit-learn) to prevent updates from introducing changes.
By incorporating these changes and recommendations, your code should now produce consistent and reproducible results across multiple runs.