code: def entmax_bisect(
      X,
      alpha=1.5,
      dim=-1,
      n_iter=50,
      ensure_sum_one=True,
      training=True,
  ):
      """alpha-entmax: normalizing sparse transform (a la softmax).
  
      Solves the optimization problem:
  
          max_p <x, p> - H_a(p)    s.t.    p >= 0, sum(p) == 1.
  
      where H_a(p) is the Tsallis alpha-entropy with custom alpha >= 1,
      using a bisection (root finding, binary search) algorithm.
  
      This function is differentiable with respect to both X and alpha.
  
      Parameters
      ----------
      X : torch.Tensor
          The input tensor.
  
      alpha : float or torch.Tensor
          Tensor of alpha parameters (> 1) to use. If scalar
          or python float, the same value is used for all rows, otherwise,
          it must have shape (or be expandable to)
          alpha.shape[j] == (X.shape[j] if j != dim else 1)
          A value of alpha=2 corresponds to sparsemax, and alpha=1 would in theory recover
          softmax. For numeric reasons, this algorithm does not work with `alpha=1`: if you
          want softmax, we recommend `torch.nn.softmax`.
  
      dim : int
          The dimension along which to apply alpha-entmax.
  
      n_iter : int
          Number of bisection iterations. For float32, 24 iterations should
          suffice for machine precision.
  
      ensure_sum_one : bool,
          Whether to divide the result by its sum. If false, the result might
          sum to close but not exactly 1, which might cause downstream problems.
  
      Returns
      -------
      P : torch tensor, same shape as X
          The projection result, such that P.sum(dim=dim) == 1 elementwise.
      """
      # Avoids call to custom autograd.Function during eval to ensure torchscript compatibility
      # custom autograd.Function is not scriptable: https://github.com/pytorch/pytorch/issues/22329#issuecomment-506608053
      if not training:
          output, _ = _entmax_bisect_forward(
              X, alpha, dim, n_iter, ensure_sum_one
          )
          return output
      return EntmaxBisectFunction.apply(
          X, alpha, dim, n_iter, ensure_sum_one
      )
  
imports: import torch
  import torch.nn
  from torch.autograd import Function
label_desc: Use image node_id 3 for calling a global function with example usage: entmax_bisect(X, alpha, dim, n_iter, ensure_sum_one, training) and returns: EntmaxBisectFunction, output