L1Hinge

Description

The Pairwise Ranking Loss function, which is used in tasks where it is necessary to find the optimal separation of features in space (for example, in tasks on identification by face or voice).

The error function formula is:

S = \begin{cases} ||x1 - x2||, & \quad \text{if } y == 1\\ max(0, 1 - ||x1 - x2||), & \quad \text{if } y == -1 \end{cases}

where

x1, x2 - feature vectors that are compared with each other through the L1 metric;
y - label from the set {-1, 1} for the current pair of vectors, reflecting whether the vectors from the pair belong to the same class or not.

Initializing

def __init__(self):

Parametrs

-

Explanations

-

Examples


Necessary imports:

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

Info

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

Synthetic feature tensors:

x1 = gpuarray.to_gpu(np.random.randn(10, 5).astype(np.float32))
x2 = gpuarray.to_gpu(np.random.randn(10, 5).astype(np.float32))

Important

Please remember that the first dimension of tensors is the size of the batch.

Important

For L1Hinge-loss, the shapes of the compared feature tensors should be the same.

Synthetic vector of correspondence of feature tensors:

labels = gpuarray.to_gpu(np.random.randint(low=0, high=2, size=(10, )).astype(np.int32))
where 0 means that the feature vectors belong to different classes, 1 means that the feature vectors belong to the same class

Initializing the error function:

l1Hinge = L1Hinge()

Calculating the error and the gradient on the batch:

error, (g1, g2) = l1Hinge([x1, x2], labels)