def test_multiclass_hinge_loss(): from lasagne.objectives import mutliclass_hinge_loss from lasagne.nonlinearities import rectify p = theano.tensor.matrix('p') t = theano.tensor.ivector('t') c = mutliclass_hinge_loss(p, t) # numeric version floatX = theano.config.floatX predictions = np.random.rand(10, 20).astype(floatX) targets = np.random.random_integers(0, 19, (10,)).astype("int8") one_hot = np.zeros((10, 20)) one_hot[np.arange(10), targets] = 1 correct = predictions[one_hot > 0] rest = predictions[one_hot < 1].reshape((10, 19)) rest = np.max(rest, axis=1) hinge = rectify(1 + rest - correct) # compare assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
def test_multiclass_hinge_loss_invalid(): from lasagne.objectives import mutliclass_hinge_loss with pytest.raises(TypeError) as exc: mutliclass_hinge_loss(theano.tensor.vector(), theano.tensor.matrix()) assert 'rank mismatch' in exc.value.args[0]