code: def categorical_accuracy(predictions, targets, top_k=1):
      """Computes the categorical accuracy between predictions and targets.
  
      .. math:: L_i = \\mathbb{I}(t_i = \\operatorname{argmax}_c p_{i,c})
  
      Can be relaxed to allow matches among the top :math:`k` predictions:
  
      .. math::
          L_i = \\mathbb{I}(t_i \\in \\operatorname{argsort}_c (-p_{i,c})_{:k})
  
      Parameters
      ----------
      predictions : Theano 2D tensor
          Predictions in (0, 1), such as softmax output of a neural network,
          with data points in rows and class probabilities in columns.
      targets : Theano 2D tensor or 1D tensor
          Either a vector of int giving the correct class index per data point
          or a 2D tensor of 1 hot encoding of the correct class in the same
          layout as predictions
      top_k : int
          Regard a prediction to be correct if the target class is among the
          `top_k` largest class probabilities. For the default value of 1, a
          prediction is correct only if the target class is the most probable.
  
      Returns
      -------
      Theano 1D tensor
          An expression for the item-wise categorical accuracy in {0, 1}
  
      Notes
      -----
      This is a strictly non differential function as it includes an argmax.
      This objective function should never be used with a gradient calculation.
      It is intended as a convenience for validation and testing not training.
  
      To obtain the average accuracy, call :func:`theano.tensor.mean()` on the
      result, passing ``dtype=theano.config.floatX`` to compute the mean on GPU.
      """
      if targets.ndim == predictions.ndim:
          targets = theano.tensor.argmax(targets, axis=-1)
      elif targets.ndim != predictions.ndim - 1:
          raise TypeError(
              "rank mismatch between targets and predictions"
          )
  
      if top_k == 1:
          # standard categorical accuracy
          top = theano.tensor.argmax(predictions, axis=-1)
          return theano.tensor.eq(top, targets)
      else:
          # top-k accuracy
          top = theano.tensor.argsort(predictions, axis=-1)
          # (Theano cannot index with [..., -top_k:], we need to simulate that)
          top = top[
              [slice(None) for _ in range(top.ndim - 1)]
              + [slice(-top_k, None)]
          ]
          targets = theano.tensor.shape_padaxis(targets, axis=-1)
          return theano.tensor.any(
              theano.tensor.eq(top, targets), axis=-1
          )
  
file_path: Lasagne/lasagne/objectives.py
imports: import theano.tensor
  from .utils import as_theano_expression
label_desc: Use image node_id 10 for calling a global function with example usage: categorical_accuracy(predictions, targets, top_k) and returns: theano, theano