HSJA
HSJA copied to clipboard
Which true gradient does HSJA estimated for? (in function "approximate_gradient")
Dear Sir:
In your code def approximate_gradient(model, sample, num_evals, delta, params)
, https://github.com/Jianbo-Lab/HSJA/blob/daecd5c7055d5214b39c34a7a28a98acd3557fbc/hsja.py#L165
I thought this code is used for estimating the true gradient as the following code:
def get_grad(self, model, x, true_labels, target_labels):
with torch.enable_grad():
# x = torch.clamp(x,min=0,max=1.0).cuda()
x = x.cuda()
x.requires_grad_()
logits = torch.softmax(model(x),dim=1)
if true_labels is not None:
true_labels = true_labels.cuda()
if target_labels is not None:
target_labels = target_labels.cuda()
loss = self.cw_loss(logits, true_labels, target_labels)
gradient = torch.autograd.grad(loss, x,retain_graph=True)[0].cpu().detach()
return gradient
where cw_loss
is defined as the following:
def cw_loss(self, logit, label, target=None):
if target is not None:
# targeted cw loss: logit_t - max_{i\neq t}logit_i
_, argsort = logit.sort(dim=1, descending=True)
target_is_max = argsort[:, 0].eq(target).long()
second_max_index = target_is_max.long() * argsort[:, 1] + (1 - target_is_max).long() * argsort[:, 0]
target_logit = logit[torch.arange(logit.shape[0]), target]
second_max_logit = logit[torch.arange(logit.shape[0]), second_max_index]
return target_logit - second_max_logit
else:
# untargeted cw loss: max_{i\neq y}logit_i - logit_y
_, argsort = logit.sort(dim=1, descending=True)
gt_is_max = argsort[:, 0].eq(label).long()
second_max_index = gt_is_max.long() * argsort[:, 1] + (1 - gt_is_max).long() * argsort[:, 0]
gt_logit = logit[torch.arange(logit.shape[0]), label]
second_max_logit = logit[torch.arange(logit.shape[0]), second_max_index]
return second_max_logit - gt_logit
But when I compute the cosine simillarity between two gradient, I found it was very low, about 0.02?
Which true gradient does approximate_gradient
approximate?
Can you give me some code or example please?