Search code examples
luaneural-networktorchloss

Pairwise similarity criterion in torch


I want to implement a new Criterion in torch7.

Basically, I have pair of samples for which I have a normalised similarity real value (let's denote it 'd').

Among existing Criterion, the closest I can have is the CosineEmbeddingCriterion which provides the following loss :

             ⎧ 1 - cos(x1, x2),              if y ==  1
loss(x, y) = ⎨
             ⎩ max(0, cos(x1, x2) - margin), if y == -1

Obviously this is designed for pairs, but this is for classification problems.

In my problem, I want to implement a Criterion that provide the following loss function : loss(x1, y2, d) = | d - cos(x1,x2) |

Unfortunately, unlike in this question, it does not appear to me that I cannot simply combine existing criterion to do that.

So I plan to go on the painful way to create a new module from torch7 source file.

My questions are as follows :

  • Am I correct that I don't any other straightforward solutions ?
  • If I implement my own Criterion in torch7 :
    • is there a skeleton to write criterion or a guide ?
    • how can I check that it is valid ? (how to unit test it ?)

(it seems to me very easy to write a criterion that seems correct to me -- but not in practise)

Thank you in advance for any clue !!


Solution

  • You just need to write the updateOutput and updateGradInput functions for your criterion. Then, you can use it like any other criterion. Here is a skeleton.

    https://github.com/torch/nn/blob/master/BCECriterion.lua