BVLC/caffe — pull request #1278 by jackculpepper (2014-10-14 20:36:06+00:00)
Title: Add label pair functionality to contrastive loss function

This PR adds functionality to the contrastive loss function.

Now, in addition to being able to pass a 1/0 label that specifies similar or dissimilar pairs, you can pass in two labels, and similarity will be calculated on the fly.

I do this by keeping another buffer, `similar_`, which is computed at feed-forward time.

This is useful, for example, if you want to combine a contrastive loss function with a soft-max loss, or if you want to use the same data loaders for both siamese or N-way classification networks.

The way I'm doing it does add a slight bit of additional overhead, but I think it's negligible.

If you hook up only one label, the 1/0 behavior still happens.

I also added a check to make sure there are either 3 or 4 bottom blobs.