roywei commented on a change in pull request #12750: [MXNET -1030] Cosine 
Embedding Loss
URL: https://github.com/apache/incubator-mxnet/pull/12750#discussion_r223443775
 
 

 ##########
 File path: python/mxnet/gluon/loss.py
 ##########
 @@ -706,3 +706,77 @@ def hybrid_forward(self, F, pred, positive, negative):
                      axis=self._batch_axis, exclude=True)
         loss = F.relu(loss + self._margin)
         return _apply_weighting(F, loss, self._weight, None)
+
+class CosineEmbeddingLoss(Loss):
+    r"""For a target label 1 or -1, vectors target and pred, the function 
computes the cosine distance
+    between the vectors. This can be interpretted as how similar/dissimilar 
two input vectors are.
+    .. math::
+        Cosine\_loss = \begin{gather*}
+                               \begin{cases}
+                                       1 - cos\_sim(pred, target) & \text{if } 
label = 1\\
+                                       cos\_sim(pred, target)     & \text{if } 
label = -1
+                               \end{cases}
+                        \end{gather*}
+        If
+        \begin{equation}
+               pred = p_1x + p_2y + p_3z
+        \end{equation}
+        and
+        \begin{equation}
+               target = t_1x + t_2y + t_3z
+        \end{equation}\\
+        Cosine Similarity:\\
+        \begin{equation}
+               cos\_sim = \frac{pred.target}
+                                               {||pred||.||target||}
+        \end{equation}
+
+        \begin{equation}
+               cos\_sim(pred, target) = \frac{p_1.t_1 + p_2.t_2 + p_3.t_3}
+                                                                         
{\sqrt{p_1^2 + p_2^2 + p_3^2}.\sqrt{t_1^2 + t_2^2 + t_3^2}}
+        \end{equation}
+
+    `pred`, `target` can have arbitrary shape as long as they have the same 
number of elements.
+
+    Parameters
+    ----------
+    weight : float or None
+        Global scalar weight for loss.
+    batch_axis : int, default 0
+        The axis that represents mini-batch.
+    margin : float
+        Margin of separation between correct and incorrect pair.
+
+
+    Inputs:
+    ------
+        - **pred**:   prediction tensor with arbitrary shape
+        - **target**: target tensor with same shape as pred.
+        - **sample_weight**: element-wise weighting tensor. Must be 
broadcastable
+          to the same shape as pred. For example, if pred has shape (64, 10)
+          and you want to weigh each sample in the batch separately,
+          sample_weight should have shape (64, 1).
+        - label: A 1-D tensor indicating for each pair input and pred, target 
label is 1 or -1
 
 Review comment:
   nit: ** label**

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to