def categorical_accuracy(predictions, targets, top_k=1):
 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):
  top = theano.tensor.argmax(predictions, axis=(-1))
  return theano.tensor.eq(top, targets)
 else:
  top = theano.tensor.argsort(predictions, axis=(-1))
  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))

def categorical_accuracy ( predictions , targets , top_k = 1 ) : 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 ) : top = theano . tensor . argmax ( predictions , axis = ( - 1 ) ) return theano . tensor . eq ( top , targets ) else : top = theano . tensor . argsort ( predictions , axis = ( - 1 ) ) 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 ) )
computes the categorical accuracy between predictions and targets .