Search code examples
pythontheanoconv-neural-networklasagne

Custom loss function for lasagne/ theano


I am trying to create a custom loss function for use in lasagne.

I would like to use Sorensen-dice coefficient which I have written with numpy, and use for evaluation like so:

np.sum(np.logical_and(preds == num_labs, labels == num_labs)))*2/ (np.sum(preds == num_labs) + np.sum(labels == num_labs)

Which is doing:

Dice = (2*|X & Y|)/ (|X|+ |Y|)

I am now trying to implement this in theano, unsure how feasible it is.

Is it possible to use this as a loss function? I would like to use it as I am segmenting volumes but I as this should be differentiable for back propagation, how can I change this?

Any thoughts?


Solution

  • you can write is as sum(A*B)/(sum(A^2)+sum(B^2)). refer to https://arxiv.org/abs/1606.04797