機器學習 - 對抗樣本



對抗機器學習是機器學習的一個子領域,專注於研究機器學習模型對抗攻擊的脆弱性。對抗攻擊是故意嘗試透過在輸入資料中引入小的擾動來欺騙機器學習模型。這些擾動通常對人類來說是難以察覺的,但它們會導致模型以高置信度做出錯誤的預測。對抗攻擊可能在自動駕駛、安全系統和醫療保健等現實世界應用中造成嚴重後果。

有幾種型別的對抗攻擊,包括:

  • 逃避攻擊 - 這些攻擊旨在操縱輸入資料以導致模型將其錯誤分類。逃避攻擊可以是有針對性的,攻擊者知道目標類別;也可以是非針對性的,攻擊者只想導致錯誤分類。

  • 投毒攻擊 - 這些攻擊旨在操縱訓練資料,使模型偏向於特定類別或降低其整體準確性。投毒攻擊可以是資料投毒(攻擊者修改訓練資料)或模型投毒(攻擊者修改模型本身)。

  • 模型反演攻擊 - 這些攻擊旨在透過觀察模型的輸出,推斷有關訓練資料或模型本身的敏感資訊。

為了防禦對抗攻擊,研究人員提出了一些技術,包括:

  • 對抗訓練 - 此技術包括使用對抗樣本增強訓練資料,以使模型更能抵抗對抗攻擊。

  • 防禦性蒸餾 - 此技術涉及訓練第二個模型來學習第一個模型的輸出,使其更能抵抗對抗攻擊。

  • 隨機化 - 此技術包括向輸入資料或模型引數新增隨機噪聲,以使攻擊者更難以建立對抗樣本。

  • 檢測和拒絕 - 此技術包括檢測對抗樣本並在它們被模型處理之前將其拒絕。

Python 實現

在 Python 中,一些庫提供了對抗攻擊和防禦的實現,包括:

  • CleverHans - 此庫為 TensorFlow、Keras 和 PyTorch 提供了對抗攻擊和防禦的集合。

  • ART(對抗魯棒性工具箱) - 此庫提供了一套全面的工具,用於評估和防禦機器學習模型中的對抗攻擊。

  • Foolbox - 此庫為 PyTorch、TensorFlow 和 Keras 提供了對抗攻擊的集合。

在下面的示例中,我們將使用對抗魯棒性工具箱 (ART) 來實現對抗機器學習:

首先,我們需要使用 pip 安裝 ART 包:

pip install adversarial-robustness-toolbox

然後,我們可以使用 ART 庫在一個預訓練的模型上建立一個對抗樣本。

示例

import tensorflow as tf
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from keras.optimizers import Adam
from keras.utils import to_categorical
from art.attacks.evasion import FastGradientMethod
from art.estimators.classification import KerasClassifier

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Preprocess the data
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Define the model architecture
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])

# Wrap the model with ART KerasClassifier
classifier = KerasClassifier(model=model, clip_values=(0, 1), use_logits=False)

# Train the model
classifier.fit(x_train, y_train)

# Evaluate the model on the test set
accuracy = classifier.evaluate(x_test, y_test)[1]
print("Accuracy on test set: %.2f%%" % (accuracy * 100))

# Generate adversarial examples using the FastGradientMethod attack
attack = FastGradientMethod(estimator=classifier, eps=0.1)
x_test_adv = attack.generate(x_test)

# Evaluate the model on the adversarial examples
accuracy_adv = classifier.evaluate(x_test_adv, y_test)[1]
print("Accuracy on adversarial examples: %.2f%%" % (accuracy_adv * 100))

在此示例中,我們首先載入並預處理 MNIST 資料集。然後,我們定義一個簡單的卷積神經網路 (CNN) 模型,並使用分類交叉熵損失和 Adam 最佳化器對其進行編譯。

我們將模型與 ART KerasClassifier 包裝起來,使其與 ART 攻擊相容。然後,我們在訓練集上訓練模型 10 個 epoch,並在測試集上對其進行評估。

接下來,我們使用最大擾動為 0.1 的 FastGradientMethod 攻擊生成對抗樣本。最後,我們評估模型在對抗樣本上的效能。

輸出

執行此程式碼時,將生成以下輸出:

Train on 60000 samples
Epoch 1/20
60000/60000 [==============================] - 17s 277us/sample - loss: 0.3530 - accuracy: 0.9030
Epoch 2/20
60000/60000 [==============================] - 15s 251us/sample - loss: 0.1296 - accuracy: 0.9636
Epoch 3/20
60000/60000 [==============================] - 18s 300us/sample - loss: 0.0912 - accuracy: 0.9747
Epoch 4/20
60000/60000 [==============================] - 18s 295us/sample - loss: 0.0738 - accuracy: 0.9791
Epoch 5/20
60000/60000 [==============================] - 18s 300us/sample - loss: 0.0654 - accuracy: 0.9809
-------continue
廣告