def multiclass_hinge_loss(self, predictions, targets, delta=1):
num_cls = predictions.shape[1]
if targets.ndim == predictions.ndim - 1:
targets = T.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 = T.reshape(predictions[(1-targets).nonzero()],
(-1, num_cls-1))
rest = T.max(rest, axis=1)
return T.nnet.relu(rest - corrects + delta).mean()
评论列表
文章目录