Example #1
0
 def generate_target(self, _input: InputType, idx: int = 1, same: bool = False) -> torch.Tensor:
     with torch.no_grad():
         _output = self.get_logits(_input)
     target = _output.argsort(dim=-1, descending=True)[:, idx]
     if same:
         target = repeat_to_batch(target.mode(dim=0)[0], len(_input))
     return target
Example #2
0
def generate_target(module: nn.Module,
                    _input: torch.Tensor,
                    idx: int = 1,
                    same: bool = False) -> torch.Tensor:
    with torch.no_grad():
        _output: torch.Tensor = module(_input)
    target = _output.argsort(dim=-1, descending=True)[:, idx]
    if same:
        target = repeat_to_batch(target.mode(dim=0)[0], len(_input))
    return target
Example #3
0
 def get_fake_seq(self, X: torch.Tensor) -> torch.Tensor:
     if len(X.shape) == 4:
         X = X[0]
     noise = torch.normal(mean=0.0, std=1.0, size=X.shape, device=X.device)
     fake_seq = X + self.dist * self.attack.sigma * noise
     return repeat_to_batch(fake_seq, batch_size=self.fake_query_num)