简体   繁体   English

了解tf.gradients如何评估

[英]Understanding how tf.gradients evaluates

I'm studying how to break linear classifiers, but I'm having trouble understanding tf.gradients . 我正在研究如何破坏线性分类器,但是在理解tf.gradients遇到了麻烦。

The point of the project is to take a model and train it on the mnist dataset. 该项目的重点是建立模型并将其训练在mnist数据集上。 Once it is trained, I am taking an image, slightly changing it, and feed it back to the model. 训练完成后,我将拍摄一张图像,对其稍作更改,然后将其反馈给模型。 However, when I feed it back, the prediction should be different. 但是,当我反馈它时,预测应该有所不同。 For example, if I have an image of a 2 and I want the model to predict a 6, I will change the image slightly so that the image still looks like a 2 but the model will think its a 6. 例如,如果我的图像为2,并且我希望模型预测为6,则我将略微更改图像,以使图像看起来仍然像2,但是模型会将其视为6。

How this is done is a simple equation. 如何做到这一点很简单。 We take the derivative of the loss function and take the sign of it and apply it to the image multiplied by some epsilon value. 我们取损失函数的导数,取它的正负号,并将其应用到乘以某些ε值的图像上。 For example, the equation is something like this... 例如,方程式是这样的...

new image = image + (epsilon * sign of derivative of loss function)

The part that confuses me is tf.gradients . 让我tf.gradients困惑的部分是tf.gradients I am looking at an example but I am having a hard time understanding it. 我正在看一个示例,但是我很难理解它。

First, 10 images of a number 2 are extracted. 首先,提取10张数字为2的图像。 Next, 10 labels are created representing the label 6. So the labels looks as follows... 接下来,创建代表标签6的10个标签。因此,标签如下所示...

[[0, 0, 0, 0, 0, 1, 0, 0, 0 ,0],
 [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
 ...etc...

And then to the derivative of the cost function looks as so ( cross_entropy is the cost function)... 然后对成本函数的导数看起来就这样( cross_entropy是成本函数)...

im_derivative = tf.gradients(cross_entropy, x)[0]

im_derivative = im_derivative.eval({x: x0, 
                                y_: y_six, 
                                keep_prob: 1.0})

x0 is are the 10 images of a 2 and y_six are the labels representing the number 6. The sign of this derivative is then used in the equation I demonstrated above. x0是2的10张图像, y_six是表示数字6的标签。该导数的符号然后用在上面展示的方程式I中。

My question is this, what exactly is the tf.gradients returning and why is the derivative being evaluated with a label of 6 rather than a label of 2? 我的问题是,返回的tf.gradients到底是什么?为什么要使用6而不是2来评估导数? I'm having a hard time understanding what is being returned and why a fake label is being used. 我很难理解返回的内容以及为何使用假标签。 I understand that a fake label is probably necessary to trick the classifier but it is hard to see this because I don't understand what tf.gradients is returning. 我知道伪造标签可能是欺骗分类器的必要条件,但由于我不了解tf.gradients返回的内容,因此很难看到这一点。

tf.gradient(ys, xs) is returning the symbolic partial derivatives of sum of ys wrt x in xs. tf.gradient(ys, xs)返回tf.gradient(ys, xs)中ys wrt x的和的符号偏导数。 In your case, you're defining the partial derivative of cross_entropy with respect to x (and extracting the first (and only) element, since tf.gradient returns a list). 在您的情况下,您要相对于x定义cross_entropy的偏导数(并提取第一个(也是唯一的)元素,因为tf.gradient返回一个列表)。

The gradient of the cost with respect to the input gives you an indication of how much you have to update your network parameters and in which direction perform this update in order to minimize the cost. 成本相对于输入的梯度可指示您必须更新多少网络参数,以及在哪个方向上执行此更新以最小化成本。

Hence, since you want to trick the classifier you compute the gradient of a certain input with a different label, in order to find the "indication" (or signal) you have to follow in order to make the network consider that input a 6 . 因此,由于要欺骗分类器,因此需要使用不同的标签来计算特定输入的梯度,为了找到“指示”(或信号),必须遵循该指示才能使网络将输入视为6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM