from advertorch.attacks import LinfPGDAttack
import torch.nn as nn
import numpy as np
use_cuda = torch.cuda.is_available()
device = torch.device(“cuda” if use_cuda else “cpu”)

print(use_cuda)
model.to(device)
torch.cuda.empty_cache();
model = model.eval()

x = torch.from_numpy(train_image[0].asnumpy())

y = torch.from_numpy(class_ids[0])
x = torch.Tensor(batch[0][0].asnumpy())
y = torch.Tensor(batch[6][0].asnumpy())
print(type(x))

print("x = ",batch[0][0])

print(type(y))

print("y = ",batch[6][0])
adversary = LinfPGDAttack(
model, loss_fn=nn.BCEWithLogitsLoss(reduction=“none”), eps=0.3,
nb_iter=40, eps_iter=0.01, rand_init=False, clip_min=0.0, clip_max=1.0,
targeted=False)
#nn.CrossEntropyLoss(reduction=“sum”)
x = torch.unsqueeze(x,0)
adv_untargeted = adversary.perturb(x, y)
Output:
<class ‘torch.Tensor’>

AttributeError                            Traceback (most recent call last)
 in 
30 x = torch.unsqueeze(x,0)
31
—> 32 adv_untargeted = adversary.perturb(x, y)
~.conda\envs\final\lib\site-packages\advertorch\attacks\iterative_projected_gradient.py in perturb(self, x, y)
181                 x + delta.data, min=self.clip_min, max=self.clip_max) - x
182
 → 183         rval = perturb_iterative(
184             x, y, self.predict, nb_iter=self.nb_iter,
185             eps=self.eps, eps_iter=self.eps_iter,
~.conda\envs\final\lib\site-packages\advertorch\attacks\iterative_projected_gradient.py in perturb_iterative(xvar, yvar, predict, nb_iter, eps, eps_iter, loss_fn, delta_init, minimize, ord, clip_min, clip_max, l1_sparsity)
64     for ii in range(nb_iter):
65         outputs = predict(xvar + delta)
—> 66         loss = loss_fn(outputs, yvar)
67         if minimize:
68             loss = -loss
~.conda\envs\final\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720             result = self._slow_forward(*input, **kwargs)
721         else:
 → 722             result = self.forward(*input, **kwargs)
723         for hook in itertools.chain(
724                 _global_forward_hooks.values(),
~.conda\envs\final\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
626
627     def forward(self, input: Tensor, target: Tensor) → Tensor:
 → 628         return F.binary_cross_entropy_with_logits(input, target,
629                                                   self.weight,
630                                                   pos_weight=self.pos_weight,
~.conda\envs\final\lib\site-packages\torch\nn\functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
2535         reduction_enum = _Reduction.get_enum(reduction)
2536
 → 2537     if not (target.size() == input.size()):
2538         raise ValueError(“Target size ({}) must be the same as input size ({})”.format(target.size(), input.size()))
2539
AttributeError: ‘tuple’ object has no attribute ‘size’
Please Help !