Skip to content

Latest commit

 

History

History
210 lines (129 loc) · 3.98 KB

File metadata and controls

210 lines (129 loc) · 3.98 KB

hinge

Compute the hinge loss between two double-precision floating-point number.

Usage

var hinge = require( '@stdlib/ml/base/loss/float64/hinge' );

hinge( y, p )

Computes the hinge loss between two double-precision floating-point number.

var v = hinge( 1.0, 0.782 );
// returns ~0.218

v = hinge( 1.0, -0.9 );
// returns 1.9

If either argument is NaN, the function returns NaN.

var g = hinge( NaN, 12.0 );
// returns NaN

g = hinge( 5.0, NaN );
// returns NaN

If y is not +1 or -1, the function returns NaN.

var g = hinge( 2.3, 1.0 );
// returns NaN

g = hinge( -1.3, 0.987 );
// returns NaN

Examples

var uniform = require( '@stdlib/random/array/uniform' );
var sample = require( '@stdlib/random/sample' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var hinge = require( '@stdlib/ml/base/loss/float64/hinge' );

var y = sample( [ -1.0, 1.0 ], {
    'size': 100
});
var p = uniform( 100, -5.0, 5.0, {
    'dtype': 'float64'
});

logEachMap( 'hinge(%0.4f, %0.4f) = %0.4f', y, p, hinge );

C APIs

Usage

#include "stdlib/ml/base/loss/float64/hinge.h"

stdlib_base_float64_hinge( y, p )

Computes the hinge loss between two double-precision floating-point number.

double out = stdlib_base_float64_hinge( 1.0, 0.782 );
// returns ~0.218

The function accepts the following arguments:

  • y: [in] double true target value.
  • p: [in] double predicted value.
double stdlib_base_float64_hinge( const double y, const double p );

Examples

#include "stdlib/ml/base/loss/float64/hinge.h"
#include <stdio.h>

int main( void ) {
    const double y[] = { -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
    const double p[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };

    double v;
    int i;
    for ( i = 0; i < 10; i++ ) {
        v = stdlib_base_float64_hinge( y[ i ], p[ i ] );
        printf( "hinge(%lf, %lf) = %lf\n", y[ i ], p[ i ], v );
    }
}