为什么是我的tf.梯度在不应该返回的时候返回[无]?

2024-04-26 05:08:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Tensorflow的初学者,在本练习中,我尝试基于预先训练的模型(更快的rcnn\U inception\U v2\U coco)生成对抗性攻击。 我找到了以下函数来执行快速梯度符号方法:

def step_fgsm(x, eps, logits):
  label = tf.argmax(logits,1)
  one_hot_label = tf.one_hot(label, NUM_CLASSES)
  cross_entropy = tf.losses.softmax_cross_entropy(one_hot_label, logits, 
  label_smoothing=0.1, weights=1.0)
  gradient = tf.gradients(cross_entropy,x)[0]
  x_adv = x + eps*tf.sign(gradient)
  x_adv = tf.clip_by_value(x_adv,-1.0,1.0)
  return tf.stop_gradient(x_adv)

Fgsm = step_fgsm(resized_im, 0.1, Logits)

但是当我试图计算梯度时,它返回[无],这不是我所期望的。 我想这可能与我获取Logits的表单有关,即通过调用以下函数:

def get_logits(self, image):

  width, height = image.size
  resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
  target_size = (int(resize_ratio * width), int(resize_ratio * height))
  resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)

  output_node = self.graph.get_tensor_by_name('SecondStagePostprocessor/Reshape_5:0')
  output_logits = self.sess.run(
    output_node,
    feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
  return output_logits

Logits = MODEL.get_logits(resized_im)

我可能犯了一些愚蠢的大错,但正如我所说,我开始在这个世界上,我担心我可能会得到一些错误的概念。你们有谁看到我可能犯的错误吗?我应该在这里补充更多信息吗?你知道吗


Tags: imageselfoutputtfonelabelentropycross