Skip to content

Instantly share code, notes, and snippets.

@cvvs
Forked from saeedizadi/binary_jaccard_index.py
Created May 11, 2018 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvvs/4043b44c1f807bcafb4497a51bb83132 to your computer and use it in GitHub Desktop.
Save cvvs/4043b44c1f807bcafb4497a51bb83132 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