"x = [x for x in list(data.columns) if x != y] # Create a list of feature names by including all columns from the dataset except the target variable.\n",
"/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)"
]
},
{
"cell_type":"code",
"execution_count":null,
"metadata":{
"id":"bHC52p_SRm8s"
},
"outputs":[],
"source":[
"def pgd_attack(model, x, y, epsilon=0.5, alpha=0.7, num_iter=10, targeted=False, num_random_init=0, batch_size=280): #Apply PGD attck on the model\n",
" perturbed_x = tf.identity(x) # create a copy of the input\n",
"\n",
" for _ in range(num_iter): # Loop through a specified number of iterations to incrementally adjust the perturbed input.\n",
" with tf.GradientTape() as tape: # Use TensorFlow's GradientTape to record operations for automatic differentiation.\n",
" tape.watch(perturbed_x) # keep track of purturbed_x\n",
" loss = model(perturbed_x) #calculate loss\n",
"\n",
" gradients = tape.gradient(loss, perturbed_x) # Compute the gradients of the loss with respect to the perturbed inputs\n",
"\n",
" # If the attack is targeted, invert the gradients to minimize loss instead of maximizing it\n",
" if targeted:\n",
" gradients = -gradients\n",
"\n",
" # to ensure they remain within epsilon distance from the original inputs.\n",
" perturbed_x = tf.clip_by_value(perturbed_x + alpha * tf.sign(gradients), x - epsilon, x + epsilon) #update purtubate x and clip to stay in a specific range\n",
" perturbed_x = tf.clip_by_value(perturbed_x, 0, 0.5) # ensure pixel values are in [0, 1] range\n",
"\n",
" # Disable gradient tracking on the perturbed_x tensor to prevent any gradient flow during training\n",
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1] # Evaluate the model on the original (non-perturbed) test dataset and extract the accuracy metric.\n",
"print(\"Original Model Accuracy:\", original_model_accuracy) # Print the accuracy of the model when tested against the original test data.\n",
"\n",
"perturbed_model_accuracy = model.evaluate(X_test_pgd, y_test_pgd)[1] # Print the accuracy of the model when tested against the original test data.\n",
"print(\"Perturbed Model Accuracy:\", perturbed_model_accuracy) # Print the accuracy of the model when tested against the adversarially perturbed test data.\n",
"\n",
"# Print a comparison of the original and perturbed model accuracies.\n",
"print(\"Accuracy Comparison:\")\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)\n",
"print(\"Perturbed Model Accuracy:\", perturbed_model_accuracy)"
"from sklearn.metrics import confusion_matrix , classification_report # Import the confusion_matrix and classification_report functions from sklearn.metrics module.\n",
"# Use the trained model to predict the outcomes on the scaled test data.\n",
"y_pred = model.predict(X_test_scaled) > 0.5 # The result is compared against a threshold (0.5 in this case) to convert probabilities to binary outcomes.\n",
"# Generate the confusion matrix from the true labels and the predicted labels.\n",
"train_set_preds = [np.argmax (x[0]) for x in model.predict(X_train_combined)]\n",
"test_set_preds = [np.argmax (x[0]) for x in model.predict(X_test_scaled)]\n",
"train_preds = [x[0] for x in model.predict(X_train_combined)]\n",
"test_preds = [x[0] for x in model.predict(X_test_scaled)]"
]
},
{
"cell_type":"code",
"execution_count":null,
"metadata":{
"colab":{
"base_uri":"https://localhost:8080/"
},
"id":"WXrmfGTj7eru",
"outputId":"438c99b3-d5cd-493f-b10f-17e3bae8d4d3"
},
"outputs":[
{
"name":"stdout",
"output_type":"stream",
"text":[
"Accuracy for test set: 0.9191\n",
"Accuracy for train set: 0.5000\n",
"\n",
"\n",
"Precision for test set: 0.0000\n"
]
},
{
"name":"stderr",
"output_type":"stream",
"text":[
"/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.\n",
"/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.\n",