code: def select_best_example(x_adv, losses):
      """
      Given a collection of potential adversarial examples, select the best.
  
      :param x_adv: Candidate adversarial examples
          - shape [nbatch, nsample, ndim]
      :param losses: Loss values for each candidate exampe
          - shape [nbatch, nsample]
      """
      best_loss_ind = losses.argmin(-1)[:, None, None]
      best_loss_ind = best_loss_ind.expand(-1, -1, x_adv.shape[-1])
  
      best_adv = torch.gather(x_adv, dim=1, index=best_loss_ind)
      return best_adv.squeeze(1)
  
imports: from math import inf
  from typing import Optional
  import torch
  import torch.nn.functional
  from advertorch.attacks.base import Attack
  from advertorch.attacks.base import LabelMixin
  from .utils import _check_param, _flatten, _make_projector
  from advertorch.utils import to_one_hot
label_desc: Use image node_id 2 for calling a global function with example usage: select_best_example(x_adv, losses) and returns: best_adv