Skip to content

Instantly share code, notes, and snippets.

@saeedizadi
Last active May 11, 2018 23:52
Show Gist options
  • Save saeedizadi/4935d4d4a36d47147c5e62e40d04cf9c to your computer and use it in GitHub Desktop.
Save saeedizadi/4935d4d4a36d47147c5e62e40d04cf9c to your computer and use it in GitHub Desktop.
Binary Jaccard Index in Lasagne
def binary_jaccard_index(predictions,targets):
"""Computes the binary (generalized) Jaccard index between predictions and targets.
.. math:: L_i = \\sum_i{\\min(p_i,t_i} / \\sum_i{\\max(p_i,t_i}
Parameters
----------
predictions : Theano tensor
Predictions in [0, 1], such as a sigmoidal output of a neural network,
giving the probability of the positive class
targets : Theano tensor
Targets in {0, 1}, such as ground truth labels.
Returns
-------
Theano tensor
An expression for the jaccard similarity coefficient(accuracy) in [0, 1]
Notes
-----
This objective is also known as (generalized) Jaccard similarity coefficient.
This objective function should not 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.
"""
intersection = theano.tensor.minimum(predictions, targets)
union = theano.tensor.maximum(predictions, targets)
axes = tuple(range(1,4))
return intersection.sum(axis=axes) / union.sum(axis=axes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment