Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix the order of error term's operands #13745

Merged
merged 2 commits into from
Jan 16, 2019
Merged
Changes from 1 commit
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
22 changes: 11 additions & 11 deletions python/mxnet/gluon/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def hybrid_forward(self, F, x, *args, **kwargs):
class L2Loss(Loss):
r"""Calculates the mean squared error between `pred` and `label`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the same reasoning, this should be swapped as label is the subject.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good finding!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


.. math:: L = \frac{1}{2} \sum_i \vert {pred}_i - {label}_i \vert^2.
.. math:: L = \frac{1}{2} \sum_i \vert {label}_i - {pred}_i \vert^2.

`pred` and `label` can have arbitrary shape as long as they have the same
number of elements.
Expand Down Expand Up @@ -131,15 +131,15 @@ def __init__(self, weight=1., batch_axis=0, **kwargs):

def hybrid_forward(self, F, pred, label, sample_weight=None):
label = _reshape_like(F, label, pred)
loss = F.square(pred - label)
loss = F.square(label - pred)
loss = _apply_weighting(F, loss, self._weight/2, sample_weight)
return F.mean(loss, axis=self._batch_axis, exclude=True)


class L1Loss(Loss):
r"""Calculates the mean absolute error between `pred` and `label`.

.. math:: L = \sum_i \vert {pred}_i - {label}_i \vert.
.. math:: L = \sum_i \vert {label}_i - {pred}_i \vert.

`pred` and `label` can have arbitrary shape as long as they have the same
number of elements.
Expand Down Expand Up @@ -169,7 +169,7 @@ def __init__(self, weight=None, batch_axis=0, **kwargs):

def hybrid_forward(self, F, pred, label, sample_weight=None):
label = _reshape_like(F, label, pred)
loss = F.abs(pred - label)
loss = F.abs(label - pred)
loss = _apply_weighting(F, loss, self._weight, sample_weight)
return F.mean(loss, axis=self._batch_axis, exclude=True)

Expand Down Expand Up @@ -481,9 +481,9 @@ class HuberLoss(Loss):
exceeds rho but is equal to L2 loss otherwise. Also called SmoothedL1 loss.

.. math::
L = \sum_i \begin{cases} \frac{1}{2 {rho}} ({pred}_i - {label}_i)^2 &
\text{ if } |{pred}_i - {label}_i| < {rho} \\
|{pred}_i - {label}_i| - \frac{{rho}}{2} &
L = \sum_i \begin{cases} \frac{1}{2 {rho}} ({label}_i - {pred}_i)^2 &
\text{ if } |{label}_i - {pred}_i| < {rho} \\
|{label}_i - {pred}_i| - \frac{{rho}}{2} &
\text{ otherwise }
\end{cases}

Expand Down Expand Up @@ -518,7 +518,7 @@ def __init__(self, rho=1, weight=None, batch_axis=0, **kwargs):

def hybrid_forward(self, F, pred, label, sample_weight=None):
label = _reshape_like(F, label, pred)
loss = F.abs(pred - label)
loss = F.abs(label - pred)
loss = F.where(loss > self._rho, loss - 0.5 * self._rho,
(0.5/self._rho) * F.square(loss))
loss = _apply_weighting(F, loss, self._weight, sample_weight)
Expand Down Expand Up @@ -670,8 +670,8 @@ class TripletLoss(Loss):
example and a negative example:

.. math::
L = \sum_i \max(\Vert {pred}_i - {pos_i} \Vert_2^2 -
\Vert {pred}_i - {neg_i} \Vert_2^2 + {margin}, 0)
L = \sum_i \max(\Vert {pos_i}_i - {pred} \Vert_2^2 -
\Vert {neg_i}_i - {pred} \Vert_2^2 + {margin}, 0)

`pred`, `positive` and `negative` can have arbitrary shape as long as they
have the same number of elements.
Expand Down Expand Up @@ -703,7 +703,7 @@ def __init__(self, margin=1, weight=None, batch_axis=0, **kwargs):
def hybrid_forward(self, F, pred, positive, negative):
positive = _reshape_like(F, positive, pred)
negative = _reshape_like(F, negative, pred)
loss = F.sum(F.square(pred-positive) - F.square(pred-negative),
loss = F.sum(F.square(positive-pred) - F.square(negative-pred),
axis=self._batch_axis, exclude=True)
loss = F.relu(loss + self._margin)
return _apply_weighting(F, loss, self._weight, None)
Expand Down