Commit 61df8edc authored by Hasna Kalam's avatar Hasna Kalam

Carlini attack commit 2

parent 162f5793
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 150,
"metadata": {
"id": "5V1K2W9WcD3G"
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import LabelEncoder, StandardScaler\n",
"import numpy as np\n"
]
},
{
"cell_type": "code",
"source": [
"# Load the churn dataset\n",
"churn_data = pd.read_csv('/content/Telco_Cust_Churn.csv')"
],
"metadata": {
"id": "2MNJZhI2cLrm"
},
"execution_count": 151,
"outputs": []
},
{
"cell_type": "code",
"source": [
"\n",
"# Drop Null values \n",
"churn_data.dropna (inplace=True)\n",
"churn_data.reset_index(inplace=True)\n",
"churn_data.drop(columns=['index'], inplace=True)\n",
"\n",
"\n",
"# One-hot encoding of categorical variables\n",
"# Converting boolean variables to a format for further use\n"
],
"metadata": {
"id": "wnm8GG9yN-Y4"
},
"execution_count": 152,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Preprocess the churn dataset\n",
"#churn_data = churn_data.drop('customerID', axis=1) # Remove the customerID column\n",
"churn_data['TotalCharges'] = pd.to_numeric(churn_data['TotalCharges'], errors='coerce') # Convert TotalCharges to numeric\n"
],
"metadata": {
"id": "9efJu0d0cY38"
},
"execution_count": 153,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Perform label encoding for categorical columns\n",
"categorical_cols = churn_data.select_dtypes(include=['object']).columns\n",
"label_encoder = LabelEncoder()\n",
"for col in categorical_cols:\n",
" churn_data[col] = label_encoder.fit_transform(churn_data[col])\n"
],
"metadata": {
"id": "FEbCwXMMcdMx"
},
"execution_count": 154,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Split the dataset into features and target variable\n",
"X = churn_data.drop('Churn', axis=1)\n",
"y = churn_data['Churn']"
],
"metadata": {
"id": "BEi1DyOAcg87"
},
"execution_count": 155,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Split the dataset into train and test sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n"
],
"metadata": {
"id": "WCBEzi2dciZD"
},
"execution_count": 156,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Normalize the input features\n",
"scaler = StandardScaler()\n",
"X_train = scaler.fit_transform(X_train)\n",
"X_test = scaler.transform(X_test)"
],
"metadata": {
"id": "vFvTLBZFcm7M"
},
"execution_count": 157,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Define a simple target model\n",
"def target_model(inputs):\n",
" model = tf.keras.Sequential([\n",
" tf.keras.layers.Dense(16, activation='relu', input_shape=(X_train.shape[1],)),\n",
" tf.keras.layers.Dense(1, activation='sigmoid')\n",
" ])\n",
" return model(inputs)"
],
"metadata": {
"id": "0plu-Gqlcp4E"
},
"execution_count": 158,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Define the objective function for the attack\n",
"def carlini_objective_function(inputs, target_class):\n",
" # Compute the logits (output) of the target model\n",
" logits = target_model(inputs)\n",
"\n",
" # Calculate the binary cross-entropy loss between the target class and the model's predicted probabilities\n",
" loss = tf.keras.losses.binary_crossentropy(target_class, logits)\n",
"\n",
" return loss"
],
"metadata": {
"id": "7qeX15-7crF4"
},
"execution_count": 160,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Set up the optimization algorithm\n",
"optimizer = tf.keras.optimizers.Adam(lr=0.01)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ChS1B5K8cvkc",
"outputId": "75beaa93-54f7-46c3-cc8b-82e69ffbd95e"
},
"execution_count": 161,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:absl:`lr` is deprecated in Keras optimizer, please use `learning_rate` or use the legacy optimizer, e.g.,tf.keras.optimizers.legacy.Adam.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Function to perform the Carlini attack\n",
"def carlini_attack(input_sample, target_class, epsilon=0.2, num_iterations=100):\n",
" input_sample = tf.convert_to_tensor(input_sample, dtype=tf.float32)\n",
" input_sample = tf.expand_dims(input_sample, axis=0) # Add batch dimension\n",
"\n",
" for i in range(num_iterations):\n",
" with tf.GradientTape() as tape:\n",
" # Record the perturbation for gradient computation\n",
" tape.watch(input_sample)\n",
"\n",
" # Compute the objective function\n",
" loss = carlini_objective_function(input_sample, target_class)\n",
"\n",
" # Compute the gradients of the objective function with respect to the input sample\n",
" gradients = tape.gradient(loss, input_sample)\n",
"\n",
" # Update the input sample using the gradients\n",
" input_sample = input_sample - epsilon * tf.sign(gradients)\n",
"\n",
" # Clip the input sample to maintain values within a valid range\n",
" input_sample = tf.clip_by_value(input_sample, -1, 1)\n",
"\n",
" return input_sample\n"
],
"metadata": {
"id": "9Lbcd_ObcyBK"
},
"execution_count": 162,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Select a sample from the test set for the attack\n",
"sample_index = 0\n",
"#input_sample = X_test.iloc[sample_index].values\n",
"input_sample = X_test[sample_index]"
],
"metadata": {
"id": "GdSLlEFbc6ci"
},
"execution_count": 163,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Specify the target class for the attack\n",
"#target_class = 1\n",
"target_class = tf.constant([[1.0]], dtype=tf.float32)"
],
"metadata": {
"id": "xD1IxjMYdCOx"
},
"execution_count": 164,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"# Convert y_test to a NumPy array\n",
"y_test = np.array(y_test)\n",
"\n",
"# Create an instance of the model\n",
"model = target_model(X_train.shape[1])\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
"\n",
"# Train the model\n",
"model.fit(X_train, y_train, epochs=10, batch_size=32) # Adjust the number of epochs and batch size as needed\n",
"\n",
"# Make predictions on the original test set\n",
"predictions_before_attack = model.predict(X_test)\n",
"rounded_predictions_before_attack = (predictions_before_attack > 0.5).astype(int)\n",
"\n",
"# Calculate accuracy before the attack\n",
"accuracy_before_attack = accuracy_score(y_test, rounded_predictions_before_attack)\n",
"print(\"Accuracy before attack:\", accuracy_before_attack)\n",
"\n",
"# Get the model's weights\n",
"model_weights = model.get_weights()\n",
"\n",
"# Define a new model with the same architecture\n",
"target_model = target_model(X_train.shape[1])\n",
"\n",
"# Set the weights of the new model to the original model's weights\n",
"target_model.set_weights(model_weights)\n",
"\n",
"# Generate the adversarial example using the Carlini attack\n",
"input_sample_tensor = tf.convert_to_tensor(input_sample, dtype=tf.float32)\n",
"adversarial_example_tensor = carlini_attack(input_sample_tensor, target_class)\n",
"adversarial_example = adversarial_example_tensor.numpy()\n",
"\n",
"# Perform prediction on the adversarial example\n",
"predictions_after_attack = target_model.predict(adversarial_example)\n",
"rounded_predictions_after_attack = (predictions_after_attack > 0.5).astype(int)\n",
"\n",
"# Reshape rounded_predictions_after_attack to match the shape of y_test\n",
"rounded_predictions_after_attack = np.repeat(rounded_predictions_after_attack, y_test.shape[0])\n",
"\n",
"# Calculate accuracy after the attack\n",
"accuracy_after_attack = accuracy_score(y_test, rounded_predictions_after_attack)\n",
"print(\"Accuracy after attack:\", accuracy_after_attack)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ig6Z5ZoOpNyO",
"outputId": "fe7f92ac-19b4-46fe-f961-ffb0b96e71b7"
},
"execution_count": 165,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/10\n",
"177/177 [==============================] - 1s 2ms/step - loss: nan - accuracy: 0.7231\n",
"Epoch 2/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 3/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 4/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 5/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 6/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 7/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 8/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 9/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"Epoch 10/10\n",
"177/177 [==============================] - 0s 2ms/step - loss: nan - accuracy: 0.7345\n",
"45/45 [==============================] - 0s 1ms/step\n",
"Accuracy before attack: 0.7352732434350603\n",
"1/1 [==============================] - 0s 48ms/step\n",
"Accuracy after attack: 0.7352732434350603\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