PGD attack code

parent 400084ad
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "TD110qNb1Gud"
},
"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",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GABizNMn34FL",
"outputId": "93016da7-e659-4a28-aa85-82b4e8f862ef"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
],
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 423
},
"id": "MKr76-0n-lT8",
"outputId": "cd1254bc-6aca-433e-da16-d4cfd6ed65f6"
},
"outputs": [
{
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "data"
},
"text/html": [
"\n",
" <div id=\"df-f31133c8-4450-4118-974a-7f0566260f80\" 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-f31133c8-4450-4118-974a-7f0566260f80')\"\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-f31133c8-4450-4118-974a-7f0566260f80 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-f31133c8-4450-4118-974a-7f0566260f80');\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-b38d1b62-f2ff-4ee7-853c-e710b5fa668c\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-b38d1b62-f2ff-4ee7-853c-e710b5fa668c')\"\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-b38d1b62-f2ff-4ee7-853c-e710b5fa668c button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
"\n",
" <div id=\"id_a67e323d-68d6-4c34-879a-a368c1e8a313\">\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_a67e323d-68d6-4c34-879a-a368c1e8a313 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"
],
"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]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.read_csv('/content/drive/MyDrive/disease_preprocess4 (1).csv')\n",
"\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ncnE4F12XrsK"
},
"outputs": [],
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "56kvDWrFX0Si"
},
"outputs": [],
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "GpbTdLNjX7t-"
},
"outputs": [],
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1-9Cm0dvYDA4",
"outputId": "3d7ee09c-54c8-4ba4-9e43-7e2e8d65bb34"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"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"
]
}
],
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Iwvr1lDWYJ8K",
"outputId": "0f4d93ea-7479-4b0d-f76a-b33e903e82df"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/12\n",
"11356/11356 - 200s - loss: 0.5595 - accuracy: 0.7166 - val_loss: 0.6568 - val_accuracy: 0.6807 - 200s/epoch - 18ms/step\n",
"Epoch 2/12\n",
"11356/11356 - 171s - loss: 0.4921 - accuracy: 0.7540 - val_loss: 0.6437 - val_accuracy: 0.7154 - 171s/epoch - 15ms/step\n",
"Epoch 3/12\n",
"11356/11356 - 171s - loss: 0.4857 - accuracy: 0.7585 - val_loss: 0.6327 - val_accuracy: 0.7088 - 171s/epoch - 15ms/step\n",
"Epoch 4/12\n",
"11356/11356 - 171s - loss: 0.4816 - accuracy: 0.7613 - val_loss: 0.6203 - val_accuracy: 0.7208 - 171s/epoch - 15ms/step\n",
"Epoch 5/12\n",
"11356/11356 - 171s - loss: 0.4801 - accuracy: 0.7620 - val_loss: 0.6420 - val_accuracy: 0.6980 - 171s/epoch - 15ms/step\n",
"Epoch 6/12\n",
"11356/11356 - 173s - loss: 0.4774 - accuracy: 0.7641 - val_loss: 0.6329 - val_accuracy: 0.7090 - 173s/epoch - 15ms/step\n",
"Epoch 7/12\n",
"11356/11356 - 175s - loss: 0.4753 - accuracy: 0.7645 - val_loss: 0.6236 - val_accuracy: 0.7121 - 175s/epoch - 15ms/step\n",
"Epoch 8/12\n",
"11356/11356 - 170s - loss: 0.4745 - accuracy: 0.7648 - val_loss: 0.6693 - val_accuracy: 0.6708 - 170s/epoch - 15ms/step\n",
"Epoch 9/12\n",
"11356/11356 - 188s - loss: 0.4725 - accuracy: 0.7663 - val_loss: 0.6373 - val_accuracy: 0.6903 - 188s/epoch - 17ms/step\n",
"Epoch 10/12\n",
"11356/11356 - 176s - loss: 0.4710 - accuracy: 0.7672 - val_loss: 0.6019 - val_accuracy: 0.7345 - 176s/epoch - 15ms/step\n",
"Epoch 11/12\n",
"11356/11356 - 174s - loss: 0.4699 - accuracy: 0.7672 - val_loss: 0.6487 - val_accuracy: 0.6945 - 174s/epoch - 15ms/step\n",
"Epoch 12/12\n",
"11356/11356 - 185s - loss: 0.4683 - accuracy: 0.7694 - val_loss: 0.6161 - val_accuracy: 0.7180 - 185s/epoch - 16ms/step\n",
"Execution time: 2132.9077491760254 seconds\n"
]
}
],
"source": [
"start_time = time.time()\n",
"# Train the model on the combined dataset. The method fit() trains the model for a fixed number of epochs (iterations over a dataset)\n",
"history = model.fit(X_train_combined, y_train_combined, epochs=12, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "mNYFq9YBOVht",
"outputId": "889cbb2b-de08-48d7-c4bb-2a129fb56050"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
" saving_api.save_model(\n"
]
}
],
"source": [
"model.save('modelh.h5')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Dz-1pZ7-OaU9",
"outputId": "cc9f3570-cfd2-4ac8-e0e9-dae2cace63b2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 10s 5ms/step - loss: 0.3811 - accuracy: 0.8027\n",
"Original Model Accuracy: 0.802674412727356\n"
]
}
],
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\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",
" perturbed_x = tf.stop_gradient(perturbed_x) #stop gradientflow\n",
" return perturbed_x, y # Return the adversarially perturbed examples along with their original labels\n",
"\n",
"#increasing itr and batch size dont make a diff\n",
"#increasing epsilon and dec alpha makes a change"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "c6qy-alIXgi0"
},
"outputs": [],
"source": [
"# This function modifies the original test inputs (X_test_scaled) to create new examples that are similar but slightly altered\n",
"X_test_pgd, y_test_pgd = pgd_attack(model, X_test_scaled, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ynNmdI5Z4yef",
"outputId": "c4c69321-4077-4606-8dc0-a5f29dce2538"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 9s 5ms/step - loss: 0.3811 - accuracy: 0.8027\n",
"Original Model Accuracy: 0.802674412727356\n",
"1931/1931 [==============================] - 9s 5ms/step - loss: 2.3346 - accuracy: 0.0809\n",
"Perturbed Model Accuracy: 0.0808955654501915\n",
"Accuracy Comparison:\n",
"Original Model Accuracy: 0.802674412727356\n",
"Perturbed Model Accuracy: 0.0808955654501915\n"
]
}
],
"source": [
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4vak9p645BTO",
"outputId": "9256fdb7-0828-457d-a1d6-7828ea666bf1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 8s 4ms/step\n",
"[[46274 10500]\n",
" [ 1689 3308]]\n"
]
}
],
"source": [
"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",
"print(confusion_matrix(y_test, y_pred))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dKmLGtYi5TGb",
"outputId": "fa4050b3-d6c9-45ef-bf54-8068526f50c0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 8s 4ms/step\n",
"[[46274 10500]\n",
" [ 1689 3308]]\n",
" precision recall f1-score support\n",
"\n",
" 0 0.96 0.82 0.88 56774\n",
" 1 0.24 0.66 0.35 4997\n",
"\n",
" accuracy 0.80 61771\n",
" macro avg 0.60 0.74 0.62 61771\n",
"weighted avg 0.91 0.80 0.84 61771\n",
"\n"
]
}
],
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5 # predictions above or equal to 0.5 are considered class 1 (True), and below 0.5 are class 0 (False)\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred)) # Generate a classification report that includes several metrics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "g3TxaLjc5omM",
"outputId": "d9d19ff1-79ae-4482-ad66-17c1345616e6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"14195/14195 [==============================] - 62s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"14195/14195 [==============================] - 65s 5ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n"
]
}
],
"source": [
"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",
" _warn_prf(average, modifier, msg_start, len(result))\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",
" _warn_prf(average, modifier, msg_start, len(result))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Precision for train set: 0.0000\n",
"\n",
"\n",
"Recall for test set: 0.0000\n",
"Recall for train set: 0.0000\n"
]
}
],
"source": [
"print('Accuracy for test set: %0.4f' % accuracy_score (y_test, test_set_preds))\n",
"print('Accuracy for train set: %0.4f' % accuracy_score (y_train_combined, train_set_preds))\n",
"\n",
"print('\\n')\n",
"\n",
"print('Precision for test set: %0.4f' % precision_score (y_test, test_set_preds))\n",
"print('Precision for train set: %0.4f' % precision_score (y_train_combined, train_set_preds))\n",
"\n",
"print('\\n')\n",
"\n",
"print('Recall for test set: %0.4f' % recall_score (y_test, test_set_preds))\n",
"print('Recall for train set: %0.4f' % recall_score (y_train_combined, train_set_preds))"
]
}
],
"metadata": {
"accelerator": "TPU",
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
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