Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions python/paddle/fluid/layers/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,11 +1707,6 @@ def kldiv_loss(x, target, reduction='mean', name=None):

def npair_loss(anchor, positive, labels, l2_reg=0.002):
'''
:alias_main: paddle.nn.functional.npair_loss
:alias: paddle.nn.functional.npair_loss,paddle.nn.functional.loss.npair_loss
:old_api: paddle.fluid.layers.npair_loss

**Npair Loss Layer**

Read `Improved Deep Metric Learning with Multi class N pair Loss Objective\
<http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/\
Expand All @@ -1722,29 +1717,31 @@ def npair_loss(anchor, positive, labels, l2_reg=0.002):
takes the similarity matrix of anchor and positive as logits.

Args:
anchor(Variable): embedding vector for the anchor image. shape=[batch_size, embedding_dims],
anchor(Tensor): 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],
positive(Tensor): 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.
labels(Tensor): 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
A Tensor representing the npair loss, the data type is the same as
anchor, the shape is [1].

Examples:
.. code-block:: python

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')
import paddle

DATATYPE = "float32"

anchor = paddle.rand(shape=(18, 6), dtype=DATATYPE)
positive = paddle.rand(shape=(18, 6), dtype=DATATYPE)
labels = paddle.rand(shape=(18,), dtype=DATATYPE)

npair_loss = paddle.nn.functional.npair_loss(anchor, positive, labels, l2_reg = 0.002)
print(npair_loss.numpy())

npair_loss = fluid.layers.npair_loss(anchor, positive, labels, l2_reg = 0.002)
'''
check_variable_and_dtype(anchor, 'anchor', ['float32', 'float64'],
'npair_loss')
Expand Down
1 change: 0 additions & 1 deletion python/paddle/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
from .loss import cross_entropy #DEFINE_ALIAS
from .loss import dice_loss #DEFINE_ALIAS
from .loss import edit_distance #DEFINE_ALIAS
from .loss import huber_loss #DEFINE_ALIAS
from .loss import iou_similarity #DEFINE_ALIAS
from .loss import kl_div #DEFINE_ALIAS
from .loss import l1_loss #DEFINE_ALIAS
Expand Down
2 changes: 0 additions & 2 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from ...fluid.layers import teacher_student_sigmoid_loss #DEFINE_ALIAS

from ...fluid.layers import edit_distance #DEFINE_ALIAS
from ...fluid.layers import huber_loss #DEFINE_ALIAS
from ...fluid.layers import sampled_softmax_with_cross_entropy #DEFINE_ALIAS
from ...fluid.layer_helper import LayerHelper
from ...fluid.framework import in_dygraph_mode
Expand All @@ -55,7 +54,6 @@
'cross_entropy',
'dice_loss',
'edit_distance',
'huber_loss',
'iou_similarity',
'kl_div',
'l1_loss',
Expand Down