【发布时间】:2017-08-30 11:35:05
【问题描述】:
我一直在尝试对抗性图像,并从以下链接 https://arxiv.org/pdf/1412.6572.pdf 阅读了 fast gradient sign method...
说明说明可以使用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 应用于我的图像,这将导致不同的预测。
【问题讨论】:
标签: python-3.x tensorflow neural-network adversarial-machines