{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tensorflow.keras.datasets import mnist\n\n#load data\n(X_train, Y_train), (X_test, Y_test) = mnist.load_data()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler, MinMaxScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import r2_score\nfrom sklearn.metrics import accuracy_score\n\nfrom tensorflow.keras.layers import Dense,Dropout,LSTM\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.losses import sparse_categorical_crossentropy, categorical_crossentropy, CategoricalCrossentropy, SparseCategoricalCrossentropy\nfrom tensorflow.keras.backend import sign\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('MNIST Dataset Shape:')\nprint('X_train: ' + str(X_train.shape))\nprint('Y_train: ' + str(Y_train.shape))\nprint('X_test:  '  + str(X_test.shape))\nprint('Y_test:  '  + str(Y_test.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\n# normalize the data\nX_train /= 255.0\nX_test /= 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# change input dimensions to 3 for LSTM input\nX_train = np.reshape(X_train, (60000, 28, 28))\nX_test = np.reshape(X_test, (10000, 28, 28))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#convert y values to one hot encoding format\nY_train = to_categorical(Y_train)\nY_test = to_categorical(Y_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train the LSTM model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# LSTM model\nmodel = Sequential()\n\nmodel.add(LSTM(256, input_shape=(X_train.shape[1], 28)))\nmodel.add(Dropout(0.15))\nmodel.add(Dense(10, activation=\"sigmoid\"))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"adam_modified = optimizers.Adam(learning_rate=0.005, beta_1=0.7, beta_2=0.9, amsgrad=False)\n\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=adam_modified, metrics=[\"accuracy\"])\nhistory = model.fit(X_train, Y_train, epochs=5, validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict_classes(X_test)\naccuracy_score(np.argmax(Y_test, axis=1), predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#plot the loss and accuracy curves\ndef loss_curve(history):\n    \n    plt.figure(figsize=[8,6])\n    plt.plot(history.history['loss'],'r',linewidth=3.0)\n    plt.plot(history.history['val_loss'],'b',linewidth=3.0)\n    plt.legend(['Training loss', 'Validation Loss'],fontsize=18)\n    plt.xlabel('Epochs ',fontsize=16)\n    plt.ylabel('Loss',fontsize=16)\n    plt.title('Loss Curves',fontsize=16)\n\ndef accuracy_curve(history):\n    \n    plt.figure(figsize=[8,6])\n    plt.plot(history.history['accuracy'],'r',linewidth=3.0)\n    plt.plot(history.history['val_accuracy'],'b',linewidth=3.0)\n    plt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=18)\n    plt.xlabel('Epochs ',fontsize=16)\n    plt.ylabel('Accuracy',fontsize=16)\n    plt.title('Accuracy Curves',fontsize=16)\n    \naccuracy_curve(history)\nloss_curve(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#we should review \ndef plot_predictions(x_test, y_true, y_prediction):\n    #indice = rd.choice(np[0])   random 10 values\n    num_cols = 5\n    num_rows = 2\n    fig, axes = plt.subplots(num_rows, num_cols, figsize=(1.5*num_cols,2*num_rows))\n    for i in range(len(x_test)):\n        ax = axes[i//num_cols, i%num_cols]\n        ax.imshow(x_test[i], cmap='gray_r')\n        ax.set_title('t: {} \\n p: {}'.format(np.argmax(y_true[i]), y_prediction[i]))\n    plt.tight_layout()\n    plt.show()\n    \nplot_predictions(X_test[:10], Y_test[0:10], predictions[0:10])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Generating adversarial attack","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def adversarial_pattern(image, label):\n    image = tf.cast(image, tf.float32)\n    \n    with tf.GradientTape() as tape:\n        tape.watch(image)\n        prediction = model(image)\n        \n        loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)(label, prediction)\n    \n    gradient = tape.gradient(loss, image)\n    \n    signed_grad = tf.sign(gradient)\n    \n    return signed_grad\n\nsigned = adversarial_pattern(X_test, Y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"perturbed_data = X_test + 0.2 * signed #0.2\nX_perturbed = perturbed_data.numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"adversarial_predictions = model.predict_classes(X_perturbed)\naccuracy_score(Y_test, to_categorical(adversarial_predictions))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_predictions(X_test[:10], Y_test[0:10], adversarial_predictions[0:10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_predictions(X_perturbed[:10], Y_test[0:10], adversarial_predictions[0:10])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Apply adversarial attack iteratively","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def itrAdvAttacks(itr, epsilon, alfa, image, label):\n    adversarial = image\n    for i in range(itr):\n        n = alfa * adversarial_pattern(adversarial, label)\n        adversarial += n\n        maxValues = np.maximum((image-epsilon), np.array(adversarial)) \n        adversarial = np.minimum((image+epsilon), maxValues)\n        \n        itrPrediction = model.predict_classes(adversarial)\n        #print(i, np.array(itrPrediction).shape)\n        print(\"iteration \", (i+1) , accuracy_score(Y_test, to_categorical(itrPrediction)))\n    \n    return adversarial\n\niterativeAdv = itrAdvAttacks(5, 0.1, 0.05, X_test, Y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"itrPred = model.predict_classes(iterativeAdv)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_predictions(iterativeAdv[:10], Y_test[0:10], itrPred[0:10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}