def multiclass_hinge_loss(predictions, targets, delta=1):
 num_cls = predictions.shape[1]
 if (targets.ndim == (predictions.ndim - 1)):
  targets = theano.tensor.extra_ops.to_one_hot(targets, num_cls)
 elif (targets.ndim != predictions.ndim):
  raise TypeError('rank mismatch between targets and predictions')
 corrects = predictions[targets.nonzero()]
 rest = theano.tensor.reshape(predictions[(1 - targets).nonzero()], ((-1), (num_cls - 1)))
 rest = theano.tensor.max(rest, axis=1)
 return theano.tensor.nnet.relu(((rest - corrects) + delta))

def multiclass_hinge_loss ( predictions , targets , delta = 1 ) : num_cls = predictions . shape [ 1 ] if ( targets . ndim == ( predictions . ndim - 1 ) ) : targets = theano . tensor . extra_ops . to_one_hot ( targets , num_cls ) elif ( targets . ndim != predictions . ndim ) : raise TypeError ( 'rank mismatch between targets and predictions' ) corrects = predictions [ targets . nonzero ( ) ] rest = theano . tensor . reshape ( predictions [ ( 1 - targets ) . nonzero ( ) ] , ( ( - 1 ) , ( num_cls - 1 ) ) ) rest = theano . tensor . max ( rest , axis = 1 ) return theano . tensor . nnet . relu ( ( ( rest - corrects ) + delta ) )
computes the multi-class hinge loss between predictions and targets .