npair_loss

paddle.fluid.layers.npair_loss(anchor, positive, labels, l2_reg=0.002)[source]

Npair Loss Layer

Read Improved Deep Metric Learning with Multi class N pair Loss Objective .

Npair loss requires paired data. Npair loss has two parts: the first part is L2 regularizer on the embedding vector; the second part is cross entropy loss which takes the similarity matrix of anchor and positive as logits.

Parameters
  • anchor (Variable) – embedding vector for the anchor image. shape=[batch_size, embedding_dims], the data type is float32 or float64.

  • positive (Variable) – embedding vector for the positive image. shape=[batch_size, embedding_dims], the data type is float32 or float64.

  • labels (Variable) – 1-D tensor. shape=[batch_size], the data type is float32 or float64 or int64.

  • l2_reg (float32) – L2 regularization term on embedding vector, default: 0.002.

Returns

A Variable holding Tensor representing the npair loss, the data type is the same as anchor, the shape is [1].

Examples

import paddle.fluid as fluid
anchor = fluid.data(
              name = 'anchor', shape = [18, 6], dtype = 'float32')
positive = fluid.data(
              name = 'positive', shape = [18, 6], dtype = 'float32')
labels = fluid.data(
              name = 'labels', shape = [18], dtype = 'float32')

npair_loss = fluid.layers.npair_loss(anchor, positive, labels, l2_reg = 0.002)