Repository navigation
feat: add ml/base/loss/float64/hinge
#11953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
16d1653
80993d8
0f64adc
6cdb43e
e8965fd
054a591
6f86245
c855aa0
b65324f
7fa6355
a8808ad
c891a2e
3ac321d
b906af9
257de46
e995cd8
aeb8913
570a228
a4398e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,230 @@ | ||||||||||||||||||||||||||
| <!-- | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @license Apache-2.0 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Copyright (c) 2026 The Stdlib Authors. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||
| you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||
| You may obtain a copy of the License at | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||
| See the License for the specific language governing permissions and | ||||||||||||||||||||||||||
| limitations under the License. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # hinge | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| > Compute the [hinge loss][hinge-loss] between two double-precision floating-point numbers. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="intro"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| The [hinge loss][hinge-loss] is defined as | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- <equation class="equation" label="eq:hinge_loss" align="center" raw="{L(y,p)=\max(0,t-y\cdot p)}" alt="Equation for the hinge loss."> --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
kgryte marked this conversation as resolved.
|
||||||||||||||||||||||||||
| ```math | ||||||||||||||||||||||||||
| {L(y,p)=\max(0,t-y\cdot p)} | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- </equation> --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.intro --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="usage"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ## Usage | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```javascript | ||||||||||||||||||||||||||
| var hinge = require( '@stdlib/ml/base/loss/float64/hinge' ); | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| #### hinge( t, y, p ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Computes the [hinge loss][hinge-loss] between two double-precision floating-point numbers. | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why "between two double-precision floating-point numbers"? Why not "with respect to a model parameter" as you did for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, “with respect to a model parameter” would not be applicable, since we are not differentiating the loss function with respect to a model parameter, as we would when computing the loss gradient. However, I’m open to changing the description to something more appropriate if you have a better suggestion.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make things more clear:
Here it is being differentiated wrt to model parameter. |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```javascript | ||||||||||||||||||||||||||
| var v = hinge( 1.0, 1.0, 0.782 ); | ||||||||||||||||||||||||||
| // returns ~0.218 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| v = hinge( 2.0, 1.0, -0.9 ); | ||||||||||||||||||||||||||
| // returns 2.9 | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter list. |
||||||||||||||||||||||||||
| The function accepts the following arguments: | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - **t**: margin threshold. | ||||||||||||||||||||||||||
| - **y**: true target value. | ||||||||||||||||||||||||||
| - **p**: predicted value. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
Comment on lines
+66
to
+67
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| If any argument is `NaN`, the function returns `NaN`. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```javascript | ||||||||||||||||||||||||||
| var v = hinge( 1.0, NaN, 12.0 ); | ||||||||||||||||||||||||||
| // returns NaN | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| v = hinge( 2.0, 5.0, NaN ); | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| // returns NaN | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| If `y` is not +1 or -1, the function returns `NaN`. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```javascript | ||||||||||||||||||||||||||
| var v = hinge( 1.0, 2.3, 1.0 ); | ||||||||||||||||||||||||||
| // returns NaN | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| v = hinge( 1.0, -1.3, 0.987 ); | ||||||||||||||||||||||||||
| // returns NaN | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.usage --> | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same note as |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="examples"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ## Examples | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- eslint no-undef: "error" --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```javascript | ||||||||||||||||||||||||||
| 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 t = uniform( 100, 0.0, 10.0, { | ||||||||||||||||||||||||||
| 'dtype': 'float64' | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| 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) = %0.4f', t, y, p, hinge ); | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.examples --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- C interface documentation. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| * * * | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="c"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ## C APIs | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="intro"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.intro --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- C usage documentation. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="usage"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ### Usage | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||
| #include "stdlib/ml/base/loss/float64/hinge.h" | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| #### stdlib_base_float64_hinge( t, y, p ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Computes the [hinge loss][hinge-loss] between two double-precision floating-point numbers. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||
| double out = stdlib_base_float64_hinge( 1.0, 1.0, 0.782 ); | ||||||||||||||||||||||||||
| // returns ~0.218 | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| The function accepts the following arguments: | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - **t**: `[in] double` threshold value. | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Matches line 63 and |
||||||||||||||||||||||||||
| - **y**: `[in] double` true target value. | ||||||||||||||||||||||||||
| - **p**: `[in] double` predicted value. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||
| double stdlib_base_float64_hinge( const double t, const double y, const double p ); | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.usage --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="notes"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.notes --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- C API usage examples. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="examples"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ### Examples | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```c | ||||||||||||||||||||||||||
| #include "stdlib/ml/base/loss/float64/hinge.h" | ||||||||||||||||||||||||||
| #include <stdio.h> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| int main( void ) { | ||||||||||||||||||||||||||
| const double t[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }; | ||||||||||||||||||||||||||
| 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( t[ i ], y[ i ], p[ i ] ); | ||||||||||||||||||||||||||
| printf( "hinge(%lf, %lf, %lf) = %lf\n", t[ i ], y[ i ], p[ i ], v ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.examples --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.c --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="related"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.related --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <section class="links"> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| [hinge-loss]: https://en.wikipedia.org/wiki/Hinge_loss | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- <related-links> --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
kgryte marked this conversation as resolved.
|
||||||||||||||||||||||||||
| <!-- </related-links> --> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| <!-- /.links --> | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var sample = require( '@stdlib/random/sample' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var hinge = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var len; | ||
| var t; | ||
| var y; | ||
| var p; | ||
| var v; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| y = sample( [ -1.0, 1.0 ], { | ||
| 'size': len | ||
| }); | ||
| p = uniform( len, -2.0, 2.0 ); | ||
| t = uniform( len, 0.0, 10.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = hinge( t[ i % t.length ], y[ i % y.length ], p[ i % p.length ] ); | ||
| if ( isnan( v ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( v ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var resolve = require( 'path' ).resolve; | ||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var sample = require( '@stdlib/random/sample' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var tryRequire = require( '@stdlib/utils/try-require' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var hinge = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
| var opts = { | ||
| 'skip': ( hinge instanceof Error ) | ||
| }; | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::native', pkg ), opts, function benchmark( b ) { | ||
| var len; | ||
| var t; | ||
| var y; | ||
| var p; | ||
| var v; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| y = sample( [ -1.0, 1.0 ], { | ||
| 'size': len | ||
| }); | ||
| p = uniform( len, -2.0, 2.0 ); | ||
| t = uniform( len, 0.0, 10.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = hinge( t[ i % t.length ], y[ i % y.length ], p[ i % p.length ] ); | ||
| if ( isnan( v ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( v ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.