MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / hinge_loss

Function hinge_loss

imperative/python/megengine/functional/loss.py:260–296  ·  view source on GitHub ↗

r"""Caculates the hinge loss which is often used in SVM. The hinge loss can be described as: .. math:: loss(x, y) = \frac{1}{N}\sum_i\sum_j(max(0, 1 - x_{ij}*y_{ij})) Args: pred: input tensor representing the predicted probability, shape is `(N, C)`. label: input tenso

(
    pred: Tensor, label: Tensor, norm: str = "L1", reduction: str = "mean"
)

Source from the content-addressed store, hash-verified

258
259@_reduce_output
260def hinge_loss(
261 pred: Tensor, label: Tensor, norm: str = "L1", reduction: str = "mean"
262) -> Tensor:
263 r"""Caculates the hinge loss which is often used in SVM.
264
265 The hinge loss can be described as:
266
267 .. math:: loss(x, y) = \frac{1}{N}\sum_i\sum_j(max(0, 1 - x_{ij}*y_{ij}))
268
269 Args:
270 pred: input tensor representing the predicted probability, shape is `(N, C)`.
271 label: input tensor representing the binary classification label, shape is `(N, C)`.
272 norm: specify the norm to caculate the loss, should be "L1" or "L2".
273 reduction: the reduction to apply to the output: 'none' | 'mean' | 'sum'. Default: 'mean'
274
275 Returns:
276 loss value.
277
278 Examples:
279 >>> pred = Tensor([[0.5, -0.5, 0.1], [-0.6, 0.7, 0.8]])
280 >>> label = Tensor([[1, -1, -1], [-1, 1, 1]])
281 >>> F.nn.hinge_loss(pred, label)
282 Tensor(1.5, device=xpux:0)
283 >>> F.nn.hinge_loss(pred, label, reduction="none")
284 Tensor([2.1 0.9], device=xpux:0)
285 >>> F.nn.hinge_loss(pred, label, reduction="sum")
286 Tensor(3.0, device=xpux:0)
287
288 """
289 norm = norm.upper()
290 assert norm in ["L1", "L2"], "norm must be L1 or L2"
291 # Converts binary labels to -1/1 labels.
292 loss = relu(1.0 - pred * label)
293 if norm == "L1":
294 return loss.sum(axis=1)
295 else:
296 return (loss ** 2).sum(axis=1)
297
298
299def _gen_repeat_idx(inp: Tensor):

Callers

nothing calls this directly

Calls 2

reluFunction · 0.70
sumMethod · 0.45

Tested by

no test coverage detected