我一直在嘗試與對抗的圖像和我的fast gradient sign method
從以下鏈接閱讀https://arxiv.org/pdf/1412.6572.pdf起來......獲取必要的梯度值,打破圖像
的說明解釋必要的梯度可以用backpropagation
計算...
我已經成功地產生對抗性的圖像,但我在試圖提取失敗創建對抗圖像所必需的梯度。我將證明我的意思。
讓我們假設我已經使用logistic regression
來訓練我的算法。我在restore
模型中提取了我希望變成敵對圖像的數字。在這種情況下,它是數字2 ...
# construct model
logits = tf.matmul(x, W) + b
pred = tf.nn.softmax(logits)
...
...
# assign the images of number 2 to the variable
sess.run(tf.assign(x, labels_of_2))
# setup softmax
sess.run(pred)
# placeholder for target label
fake_label = tf.placeholder(tf.int32, shape=[1])
# setup the fake loss
fake_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=fake_label)
# minimize fake loss using gradient descent,
# calculating the derivatives of the weight of the fake image will give the direction of weights necessary to change the prediction
adversarial_step = tf.train.GradientDescentOptimizer(learning_rate=FLAGS.learning_rate).minimize(fake_loss, var_list=[x])
# continue calculating the derivative until the prediction changes for all 10 images
for i in range(FLAGS.training_epochs):
# fake label tells the training algorithm to use the weights calculated for number 6
sess.run(adversarial_step, feed_dict={fake_label:np.array([6])})
sess.run(pred)
這是我的方法,它完美的作品。這需要數2的我的形象,並改變它只有輕微,以至於當我運行以下...
x_in = np.expand_dims(x[0], axis=0)
classification = sess.run(tf.argmax(pred, 1))
print(classification)
將預測數2作爲6號
的問題是,我需要提取必要的梯度來欺騙神經網絡進入思維數字2爲6.我需要使用這個梯度來創建上面提到的nematode
。
我不知道如何提取梯度值。我試着看tf.gradients
,但我無法弄清楚如何使用這個功能產生敵對圖像。我實現上述fake_loss
變量後,下面...
tf.gradients(fake_loss, x)
for i in range(FLAGS.training_epochs):
# calculate gradient with weight of number 6
gradient_value = sess.run(gradients, feed_dict={fake_label:np.array([6])})
# update the image of number 2
gradient_update = x+0.007*gradient_value[0]
sess.run(tf.assign(x, gradient_update))
sess.run(pred)
不幸的是,預測並沒有我想要的方式發生變化,而且這種邏輯導致了一個相當模糊的圖像。
我希望能解釋一下爲了計算和提取能夠欺騙神經網絡的梯度,我需要做的事情,以便如果我將這個梯度作爲nematode
應用到我的圖像上,會導致不同的預測。