parameters: (self, x_train_data, gradients)
return_statement: return adv_loss
docstring: Computes the adversarial loss:
          1) Sample points initially at random around the positive training
              data points
          2) Gradient ascent to find the most optimal point in set N_i(r) 
              classified as +ve (label=0). This is done by maximizing 
              the CE loss wrt label 0
          3) Project the points between spheres of radius R and gamma * R 
              (set N_i(r) with mahalanobis distance as a distance measure),
              by solving the optimization problem
          4) Pass the calculated adversarial points through the model, 
              and calculate the CE loss wrt target class 0
          
          Parameters
          ----------
          x_train_data: Batch of data to compute loss on.
          gradients: gradients of the model for the given data.
docstring_summary: Computes the adversarial loss:
          1) Sample points initially at random around the positive training
              data points
          2) Gradient ascent to find the most optimal point in set N_i(r) 
              classified as +ve (label=0). This is done by maximizing 
              the CE loss wrt label 0
          3) Project the points between spheres of radius R and gamma * R 
              (set N_i(r) with mahalanobis distance as a distance measure),
              by solving the optimization problem
          4) Pass the calculated adversarial points through the model, 
              and calculate the CE loss wrt target class 0
          
          Parameters
          ----------
          x_train_data: Batch of data to compute loss on.
          gradients: gradients of the model for the given data.
docstring_tokens: Computes the adversarial loss : 1 ) Sample points initially at random around the positive training data points 2 ) Gradient ascent to find the most optimal point in set N_i ( r ) classified as + ve ( label = 0 ) . This is done by maximizing the CE loss wrt label 0 3 ) Project the points between spheres of radius R and gamma * R ( set N_i ( r ) with mahalanobis distance as a distance measure ) by solving the optimization problem 4 ) Pass the calculated adversarial points through the model and calculate the CE loss wrt target class 0 Parameters ---------- x_train_data : Batch of data to compute loss on . gradients : gradients of the model for the given data .
function: def one_class_adv_loss(self, x_train_data, gradients):
          """Computes the adversarial loss:
          1) Sample points initially at random around the positive training
              data points
          2) Gradient ascent to find the most optimal point in set N_i(r) 
              classified as +ve (label=0). This is done by maximizing 
              the CE loss wrt label 0
          3) Project the points between spheres of radius R and gamma * R 
              (set N_i(r) with mahalanobis distance as a distance measure),
              by solving the optimization problem
          4) Pass the calculated adversarial points through the model, 
              and calculate the CE loss wrt target class 0
          
          Parameters
          ----------
          x_train_data: Batch of data to compute loss on.
          gradients: gradients of the model for the given data.
          """
          batch_size = len(x_train_data)
          # Randomly sample points around the training data
          # We will perform SGD on these to find the adversarial points
          x_adv = torch.randn(x_train_data.shape).to(self.device).detach().requires_grad_()
          x_adv_sampled = x_adv + x_train_data
  
          for step in range(self.ascent_num_steps):
              with torch.enable_grad():
  
                  new_targets = torch.zeros(batch_size, 1).to(self.device)
                  new_targets = torch.squeeze(new_targets)
                  new_targets = new_targets.to(torch.float)
                  
                  logits = self.model(x_adv_sampled)         
                  logits = torch.squeeze(logits, dim = 1)
                  new_loss = F.binary_cross_entropy_with_logits(logits, new_targets)
  
                  grad = torch.autograd.grad(new_loss, [x_adv_sampled])[0]
                  grad_norm = torch.norm(grad, p=2, dim = tuple(range(1, grad.dim())))
                  grad_norm = grad_norm.view(-1, *[1]*(grad.dim()-1))
                  grad_normalized = grad/grad_norm 
              with torch.no_grad():
                  x_adv_sampled.add_(self.ascent_step_size * grad_normalized)
  
              if (step + 1) % 5==0:
                  # Project the normal points to the set N_i(r) based on mahalanobis distance
                  h = x_adv_sampled - x_train_data
                  h_flat = torch.reshape(h, (h.shape[0], -1))
                  gradients_flat = torch.reshape(gradients, (gradients.shape[0], -1))
                  #Normalize the gradients 
                  gradients_normalized = normalize_grads(gradients_flat)
                  #Solve the non-convex 1D optimization
                  h_flat = optim_solver(gradients_normalized, h_flat, self.radius, self.device, self.gamma)
                  h = torch.reshape(h_flat, h.shape)
                  x_adv_sampled = x_train_data + h  #These adv_points are now on the surface of hyper-sphere
  
          adv_pred = self.model(x_adv_sampled)
          adv_pred = torch.squeeze(adv_pred, dim=1)
          adv_loss = F.binary_cross_entropy_with_logits(adv_pred, (new_targets * 0))
  
          return adv_loss
function_tokens: def one_class_adv_loss ( self , x_train_data , gradients ) : batch_size = len ( x_train_data ) # Randomly sample points around the training data # We will perform SGD on these to find the adversarial points x_adv = torch . randn ( x_train_data . shape ) . to ( self . device ) . detach ( ) . requires_grad_ ( ) x_adv_sampled = x_adv + x_train_data for step in range ( self . ascent_num_steps ) : with torch . enable_grad ( ) : new_targets = torch . zeros ( batch_size , 1 ) . to ( self . device ) new_targets = torch . squeeze ( new_targets ) new_targets = new_targets . to ( torch . float ) logits = self . model ( x_adv_sampled ) logits = torch . squeeze ( logits , dim = 1 ) new_loss = F . binary_cross_entropy_with_logits ( logits , new_targets ) grad = torch . autograd . grad ( new_loss , [ x_adv_sampled ] ) [ 0 ] grad_norm = torch . norm ( grad , p = 2 , dim = tuple ( range ( 1 , grad . dim ( ) ) ) ) grad_norm = grad_norm . view ( - 1 , * [ 1 ] * ( grad . dim ( ) - 1 ) ) grad_normalized = grad / grad_norm with torch . no_grad ( ) : x_adv_sampled . add_ ( self . ascent_step_size * grad_normalized ) if ( step + 1 ) % 5 == 0 : # Project the normal points to the set N_i(r) based on mahalanobis distance h = x_adv_sampled - x_train_data h_flat = torch . reshape ( h , ( h . shape [ 0 ] , - 1 ) ) gradients_flat = torch . reshape ( gradients , ( gradients . shape [ 0 ] , - 1 ) ) #Normalize the gradients  gradients_normalized = normalize_grads ( gradients_flat ) #Solve the non-convex 1D optimization h_flat = optim_solver ( gradients_normalized , h_flat , self . radius , self . device , self . gamma ) h = torch . reshape ( h_flat , h . shape ) x_adv_sampled = x_train_data + h #These adv_points are now on the surface of hyper-sphere adv_pred = self . model ( x_adv_sampled ) adv_pred = torch . squeeze ( adv_pred , dim = 1 ) adv_loss = F . binary_cross_entropy_with_logits ( adv_pred , ( new_targets * 0 ) ) return adv_loss