PGD Attack Defense Strategy - Stochastic Distillation

parent 2ed1dd31
Pipeline #7321 canceled with stages
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true,
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "0pmoUFCaX-vO"
},
"outputs": [],
"source": [
"# Import necessary libraries for data manipulation, visualization, and mathematical operations\n",
"import pandas as pd #manupilationg and analysis\n",
"from matplotlib import pyplot as plt\n",
"import numpy as np #array and numerical calculations\n",
"%matplotlib inline\n",
"\n",
"# Import TensorFlow and Keras for building neural network models\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential # For linear stack of layers\n",
"from tensorflow.keras.layers import Dense # For fully connected layers\n",
"from tensorflow.keras.layers import Flatten # To flatten input layers\n",
"from sklearn.model_selection import train_test_split # For splitting data into training and testing sets\n",
"\n",
"# Import imbalanced-learn library functions for handling imbalanced datasets\n",
"from imblearn.over_sampling import SMOTE\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"from imblearn.combine import SMOTEENN # A hybrid method\n",
"import time\n",
"\n",
"from imblearn.under_sampling import NearMiss\n",
"from imblearn.over_sampling import ADASYN\n",
"from imblearn.combine import SMOTEENN\n",
"\n",
"# Import metrics for evaluating the models\n",
"from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, roc_curve, auc"
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "k7Zu57XzYt6R",
"outputId": "183bddf7-f856-407e-bac6-7699c3b0fa36"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"data = pd.read_csv('/content/drive/MyDrive/disease_preprocess4 (1).csv')\n",
"\n",
"data"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 443
},
"id": "dIC6ipieY2Ed",
"outputId": "139dc2be-f164-41d2-d37f-06340ca71f80"
},
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" GeneralHealth Checkup Exercise HeartDisease Depression Diabetes \\\n",
"0 1 2 0 0 0 0 \n",
"1 5 1 0 1 0 1 \n",
"2 5 1 1 0 0 1 \n",
"3 1 1 1 1 0 1 \n",
"4 4 1 0 0 0 0 \n",
"... ... ... ... ... ... ... \n",
"308849 5 1 1 0 0 0 \n",
"308850 2 3 1 0 0 1 \n",
"308851 5 4 1 0 1 1 \n",
"308852 5 1 1 0 0 0 \n",
"308853 3 1 1 0 0 0 \n",
"\n",
" Arthritis Gender AgeCategory BMI SmokingHistory \\\n",
"0 1 1 10 -2.159696 1 \n",
"1 0 1 10 -0.051548 0 \n",
"2 0 1 8 0.742649 0 \n",
"3 0 0 11 0.015913 0 \n",
"4 0 0 12 -0.652562 1 \n",
"... ... ... ... ... ... \n",
"308849 0 0 1 0.064975 0 \n",
"308850 0 0 9 -1.095656 0 \n",
"308851 0 1 2 -0.603499 1 \n",
"308852 0 0 9 -0.750686 0 \n",
"308853 0 1 5 0.472806 0 \n",
"\n",
" AlcoholConsumption FriedPotatoConsumption \n",
"0 -0.621527 0.664502 \n",
"1 -0.621527 -0.267579 \n",
"2 -0.133707 1.130543 \n",
"3 -0.621527 0.198462 \n",
"4 -0.621527 -0.733620 \n",
"... ... ... \n",
"308849 -0.133707 -0.733620 \n",
"308850 0.354113 -0.267579 \n",
"308851 -0.133707 -0.267579 \n",
"308852 -0.255662 -0.733620 \n",
"308853 -0.499572 -0.617110 \n",
"\n",
"[308854 rows x 13 columns]"
],
"text/html": [
"\n",
" <div id=\"df-3fa9d270-27c4-4c4a-a607-ed4c5b2faf80\" class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>GeneralHealth</th>\n",
" <th>Checkup</th>\n",
" <th>Exercise</th>\n",
" <th>HeartDisease</th>\n",
" <th>Depression</th>\n",
" <th>Diabetes</th>\n",
" <th>Arthritis</th>\n",
" <th>Gender</th>\n",
" <th>AgeCategory</th>\n",
" <th>BMI</th>\n",
" <th>SmokingHistory</th>\n",
" <th>AlcoholConsumption</th>\n",
" <th>FriedPotatoConsumption</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>10</td>\n",
" <td>-2.159696</td>\n",
" <td>1</td>\n",
" <td>-0.621527</td>\n",
" <td>0.664502</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>10</td>\n",
" <td>-0.051548</td>\n",
" <td>0</td>\n",
" <td>-0.621527</td>\n",
" <td>-0.267579</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>8</td>\n",
" <td>0.742649</td>\n",
" <td>0</td>\n",
" <td>-0.133707</td>\n",
" <td>1.130543</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>11</td>\n",
" <td>0.015913</td>\n",
" <td>0</td>\n",
" <td>-0.621527</td>\n",
" <td>0.198462</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>12</td>\n",
" <td>-0.652562</td>\n",
" <td>1</td>\n",
" <td>-0.621527</td>\n",
" <td>-0.733620</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>308849</th>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0.064975</td>\n",
" <td>0</td>\n",
" <td>-0.133707</td>\n",
" <td>-0.733620</td>\n",
" </tr>\n",
" <tr>\n",
" <th>308850</th>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>9</td>\n",
" <td>-1.095656</td>\n",
" <td>0</td>\n",
" <td>0.354113</td>\n",
" <td>-0.267579</td>\n",
" </tr>\n",
" <tr>\n",
" <th>308851</th>\n",
" <td>5</td>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>-0.603499</td>\n",
" <td>1</td>\n",
" <td>-0.133707</td>\n",
" <td>-0.267579</td>\n",
" </tr>\n",
" <tr>\n",
" <th>308852</th>\n",
" <td>5</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>9</td>\n",
" <td>-0.750686</td>\n",
" <td>0</td>\n",
" <td>-0.255662</td>\n",
" <td>-0.733620</td>\n",
" </tr>\n",
" <tr>\n",
" <th>308853</th>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>0.472806</td>\n",
" <td>0</td>\n",
" <td>-0.499572</td>\n",
" <td>-0.617110</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>308854 rows × 13 columns</p>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-3fa9d270-27c4-4c4a-a607-ed4c5b2faf80')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-3fa9d270-27c4-4c4a-a607-ed4c5b2faf80 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-3fa9d270-27c4-4c4a-a607-ed4c5b2faf80');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
"<div id=\"df-d0c0e5e7-a999-4417-9152-3c07de6b5959\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-d0c0e5e7-a999-4417-9152-3c07de6b5959')\"\n",
" title=\"Suggest charts\"\n",
" style=\"display:none;\">\n",
"\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-d0c0e5e7-a999-4417-9152-3c07de6b5959 button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
"\n",
" <div id=\"id_2da3b264-056b-4e70-9c2d-d5ef1f6581e9\">\n",
" <style>\n",
" .colab-df-generate {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-generate:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-generate {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-generate:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
" <button class=\"colab-df-generate\" onclick=\"generateWithVariable('data')\"\n",
" title=\"Generate code using this dataframe.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M7,19H8.4L18.45,9,17,7.55,7,17.6ZM5,21V16.75L18.45,3.32a2,2,0,0,1,2.83,0l1.4,1.43a1.91,1.91,0,0,1,.58,1.4,1.91,1.91,0,0,1-.58,1.4L9.25,21ZM18.45,9,17,7.55Zm-12,3A5.31,5.31,0,0,0,4.9,8.1,5.31,5.31,0,0,0,1,6.5,5.31,5.31,0,0,0,4.9,4.9,5.31,5.31,0,0,0,6.5,1,5.31,5.31,0,0,0,8.1,4.9,5.31,5.31,0,0,0,12,6.5,5.46,5.46,0,0,0,6.5,12Z\"/>\n",
" </svg>\n",
" </button>\n",
" <script>\n",
" (() => {\n",
" const buttonEl =\n",
" document.querySelector('#id_2da3b264-056b-4e70-9c2d-d5ef1f6581e9 button.colab-df-generate');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" buttonEl.onclick = () => {\n",
" google.colab.notebook.generateWithVariable('data');\n",
" }\n",
" })();\n",
" </script>\n",
" </div>\n",
"\n",
" </div>\n",
" </div>\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "data"
}
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"source": [
"# define target variable and features\n",
"\n",
"y='HeartDisease'\n",
"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",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(data [x],\n",
" data [y],\n",
" test_size=0.2,\n",
" random_state=42)"
],
"metadata": {
"id": "4cESrzPuZadK"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#apply standardization\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"# Create a StandardScaler instance\n",
"scaler = StandardScaler()\n",
"\n",
"\n",
"# Fit the scaler on the training data and transform it\n",
"X_train_scaled = scaler.fit_transform(X_train)\n",
"\n",
"# Use the same scaler to transform the test data\n",
"X_test_scaled = scaler.transform(X_test)"
],
"metadata": {
"id": "txOpB_6bZdn3"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"\n",
"# Apply SMOTE to oversample the minority class\n",
"smote = SMOTE(sampling_strategy='auto', random_state=42)\n",
"X_train_smote, y_train_smote = smote.fit_resample(X_train_scaled, y_train)\n",
"\n",
"# Apply undersampling to the majority class\n",
"under_sampler = RandomUnderSampler(sampling_strategy='auto', random_state=42)\n",
"X_train_combined, y_train_combined = under_sampler.fit_resample(X_train_smote, y_train_smote)\n",
"\n",
"# Train and evaluate your machine learning model using X_train_combined and y_train_combined\n",
"# Evaluate the model on X_test_scaled and y_test\n"
],
"metadata": {
"id": "Nh2uH4-ZZj2M"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Import the necessary modules from Keras for building a neural network model\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"\n",
"input_shape = (X_train_combined.shape[1], 1) # Define the input shape for the neural network based on the shape of the training data\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten()) # Flatten the 3D output to 1D to transition from convolutional layers to dense layers.\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5)) # Dropout 50% of the neurons to prevent overfitting\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"model.summary()\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "p6qUv5erZpAq",
"outputId": "9af68444-c9cb-4626-c3b8-59a07ea78661"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" conv1d (Conv1D) (None, 10, 128) 512 \n",
" \n",
" batch_normalization (Batch (None, 10, 128) 512 \n",
" Normalization) \n",
" \n",
" max_pooling1d (MaxPooling1 (None, 5, 128) 0 \n",
" D) \n",
" \n",
" conv1d_1 (Conv1D) (None, 3, 256) 98560 \n",
" \n",
" batch_normalization_1 (Bat (None, 3, 256) 1024 \n",
" chNormalization) \n",
" \n",
" max_pooling1d_1 (MaxPoolin (None, 1, 256) 0 \n",
" g1D) \n",
" \n",
" flatten (Flatten) (None, 256) 0 \n",
" \n",
" dense (Dense) (None, 512) 131584 \n",
" \n",
" batch_normalization_2 (Bat (None, 512) 2048 \n",
" chNormalization) \n",
" \n",
" dropout (Dropout) (None, 512) 0 \n",
" \n",
" dense_1 (Dense) (None, 256) 131328 \n",
" \n",
" batch_normalization_3 (Bat (None, 256) 1024 \n",
" chNormalization) \n",
" \n",
" dropout_1 (Dropout) (None, 256) 0 \n",
" \n",
" dense_2 (Dense) (None, 128) 32896 \n",
" \n",
" batch_normalization_4 (Bat (None, 128) 512 \n",
" chNormalization) \n",
" \n",
" dropout_2 (Dropout) (None, 128) 0 \n",
" \n",
" dense_3 (Dense) (None, 64) 8256 \n",
" \n",
" batch_normalization_5 (Bat (None, 64) 256 \n",
" chNormalization) \n",
" \n",
" dropout_3 (Dropout) (None, 64) 0 \n",
" \n",
" dense_4 (Dense) (None, 1) 65 \n",
" \n",
"=================================================================\n",
"Total params: 408577 (1.56 MB)\n",
"Trainable params: 405889 (1.55 MB)\n",
"Non-trainable params: 2688 (10.50 KB)\n",
"_________________________________________________________________\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def stochastic_distillation_loss(temperature):\n",
" def loss_fn(y_true, y_pred): # loss betwn true label and predicted label cal\n",
" softened_logits = tf.math.log(y_pred + 1e-20) / temperature # predictions are softened using temp\n",
" loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(y_true, softened_logits)) # cal loss using sigmoid ce and softend logits\n",
" return loss\n",
" return loss_fn\n",
"\n",
"\n",
"num_epochs = 10\n",
"num_samples = len(X_train_combined)\n",
"batch_size = 32\n",
"\n",
"for epoch in range(num_epochs):\n",
" print(f\"Epoch {epoch + 1}/{num_epochs}\")\n",
" np.random.shuffle(X_train_combined)\n",
" np.random.shuffle(y_train_combined)\n",
" #bothe the data, label are shuffled to avoide model memorizing the order of samples and gen better\n",
"\n",
"\n",
" for batch_start in range(0, num_samples, batch_size):\n",
" batch_end = min(batch_start + batch_size, num_samples)\n",
" X_batch = X_train[batch_start:batch_end]\n",
" y_batch = y_train[batch_start:batch_end]\n",
"\n",
"\n",
" temperature = np.random.uniform(1.0, 5.0) #chosse a random temp value for each batch\n",
" loss_fn = stochastic_distillation_loss(temperature)\n",
" model.train_on_batch(X_batch, y_batch, reset_metrics=False)# Train the model for one batch with the stochastic distillation loss\n",
"\n",
" val_loss, val_acc = model.evaluate(X_test_scaled, y_test)# Evaluate the model's performance on the validation data\n",
" print(f\"Validation loss: {val_loss:.4f}, Validation accuracy: {val_acc:.4f}\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mlTFxsu6Z0_5",
"outputId": "6ba0dc7b-9a57-4214-f9bd-ad8f1bcbd278"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 7s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 2/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 6s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 3/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 9s 5ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 4/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 6s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 5/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 7s 4ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 6/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 6s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 7/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 7s 4ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 8/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 6s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 9/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 6s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n",
"Epoch 10/10\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-8-4e138f0dc27c>:16: UserWarning: you are shuffling a 'Series' object which is not a subclass of 'Sequence'; `shuffle` is not guaranteed to behave correctly. E.g., non-numpy array/tensor objects with view semantics may contain duplicates after shuffling.\n",
" np.random.shuffle(y_train_combined)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 7s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Validation loss: nan, Validation accuracy: 0.9191\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def pgd_attack(model, x, y, epsilon=0.5, alpha=0.04, num_iter=40, targeted=False, num_random_init=0, batch_size=128):\n",
" perturbed_x = tf.identity(x) # create a copy of the input\n",
"\n",
" for _ in range(num_iter):\n",
" with tf.GradientTape() as tape:\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) # calculate gradient of loss relevent to pur_x\n",
"\n",
" if targeted:\n",
" gradients = -gradients\n",
"\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, 1) # ensure pixel values are in [0, 1] range\n",
"\n",
" perturbed_x = tf.stop_gradient(perturbed_x) #stop gradientflow\n",
" return perturbed_x, y\n",
"\n",
"#increasing itr and batch size dont make a diff\n",
"#increasing epsilon and dec alpha makes a change"
],
"metadata": {
"id": "8poeBBC4ibGR"
},
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"source": [
"X_test_pgd, y_test_pgd = pgd_attack(model, X_test_scaled, y_test) # even the alpha changed it worked"
],
"metadata": {
"id": "PEdE4UvrigK2"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)\n",
"\n",
"perturbed_model_accuracy = model.evaluate(X_test_pgd, y_test_pgd)[1]\n",
"print(\"Perturbed Model Accuracy:\", perturbed_model_accuracy)\n",
"\n",
"print(\"Accuracy Comparison:\")\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)\n",
"print(\"Perturbed Model Accuracy:\", perturbed_model_accuracy)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aOgYeI3-imxF",
"outputId": "28459de0-9fb3-4c7d-bfba-3149294c2e0c"
},
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1931/1931 [==============================] - 6s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Original Model Accuracy: 0.9191044569015503\n",
"1931/1931 [==============================] - 7s 3ms/step - loss: nan - accuracy: 0.9191\n",
"Perturbed Model Accuracy: 0.9191044569015503\n",
"Accuracy Comparison:\n",
"Original Model Accuracy: 0.9191044569015503\n",
"Perturbed Model Accuracy: 0.9191044569015503\n"
]
}
]
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment