Skip to content

Hinge

Description

Hinge, loss function used in binary classification problems.

The error function formula is:

L = max(0, 1 - t \cdot y)

where

y - "raw" prediction of the classifier;
t - label from the set {-1, 1} for the current object.

Important

Real labels of objects must have different signs for the error function to work correctly.

Note

It is possible to calculate the error for several classes to which the object may belong. See Example

Initializing

def __init__(self):

Parameters

-

Explanations

-

Examples


Binary Classification


Necessary imports:

import numpy as np
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Cost import Hinge

Info

gpuarray is required to properly place the tensor in the GPU.

Synthetic target and prediction tensors:

scores = gpuarray.to_gpu(np.random.randn(10, 1).astype(np.float32))
labels = gpuarray.to_gpu((np.random.randint(low=0, high=2, size=(10, 1)) * 2 - 1).astype(np.int32))

Important

For Hinge-loss the dimensions of the target and prediction tensors must be the same.

Important

For Hinge-loss the dimensions of the target and prediction tensors must be the same.

Initializing the error function:

hinge = Hinge()

Calculating the error and the gradient on the batch:

error, grad = hinge(scores, labels)


Multiclass Classification with Binary Flags


Everything is the same as for the previous example, except for the scores and labels tensors. Let us suppose you have to determine the presence or absence of four attributes of an object. In this case, the above tensors will be specified as follows:

scores = gpuarray.to_gpu(np.random.randn(10, 4).astype(np.float32))
labels = gpuarray.to_gpu((np.random.randint(low=0, high=2, size=(10, 4)) * 2 - 1).astype(np.int32))