本文整理匯總了Python中cleverhans.utils_tf.model_loss方法的典型用法代碼示例。如果您正苦於以下問題:Python utils_tf.model_loss方法的具體用法?Python utils_tf.model_loss怎麽用?Python utils_tf.model_loss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cleverhans.utils_tf
的用法示例。
在下文中一共展示了utils_tf.model_loss方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: attack_single_step
# 需要導入模塊: from cleverhans import utils_tf [as 別名]
# 或者: from cleverhans.utils_tf import model_loss [as 別名]
def attack_single_step(self, x, eta, y):
"""
Given the original image and the perturbation computed so far, computes
a new perturbation.
:param x: A tensor with the original input.
:param eta: A tensor the same shape as x that holds the perturbation.
:param y: A tensor with the target labels or ground-truth labels.
"""
from cleverhans.utils_tf import model_loss, clip_eta
adv_x = x + eta
preds = self.model.get_probs(adv_x)
loss = model_loss(y, preds)
if self.targeted:
loss = -loss
grad, = tf.gradients(loss, adv_x)
if self.pgd_update == 'sign':
adv_x = adv_x + self.eps_iter * tf.sign(grad)
elif self.pgd_update == 'plain':
adv_x = adv_x + self.eps_iter * grad / tf.reduce_sum(grad**2, axis=[1, 2, 3], keep_dims=True)**0.5
else:
raise Exception('Wrong pgd_update.')
if self.clip_min is not None and self.clip_max is not None:
adv_x = tf.clip_by_value(adv_x, self.clip_min, self.clip_max)
eta = adv_x - x
eta = clip_eta(eta, self.ord, self.eps)
return eta