-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Open
nakul-krishnakumar
wants to merge
14
commits into
stdlib-js:develop
Choose a base branch
from
nakul-krishnakumar:ml-loss-hinge
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16d1653
feat: add `ml/base/loss/float64/hinge`
nakul-krishnakumar 80993d8
chore: update copyright years
stdlib-bot 0f64adc
fix: update according to code review
nakul-krishnakumar 6cdb43e
fix: update according to code review
nakul-krishnakumar e8965fd
Apply suggestions from code review
kgryte 054a591
Apply suggestions from code review
kgryte 6f86245
Apply suggestions from code review
kgryte c855aa0
chore: update according to code review
nakul-krishnakumar b65324f
chore: update according to code review
nakul-krishnakumar 7fa6355
chore: update according to code review
nakul-krishnakumar a8808ad
Apply suggestions from code review
kgryte c891a2e
Apply suggestions from code review
kgryte 3ac321d
Apply suggestions from code review
kgryte b906af9
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
lib/node_modules/@stdlib/ml/base/loss/float64/hinge/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| <!-- | ||
|
|
||
| @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 number. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
|
kgryte marked this conversation as resolved.
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var hinge = require( '@stdlib/ml/base/loss/float64/hinge' ); | ||
| ``` | ||
|
|
||
| #### hinge( y, p ) | ||
|
|
||
| Computes the [hinge loss][hinge-loss] between two double-precision floating-point number. | ||
|
kgryte marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```javascript | ||
| 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`. | ||
|
|
||
| ```javascript | ||
| 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`. | ||
|
|
||
| ```javascript | ||
| var g = hinge( 2.3, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| g = hinge( -1.3, 0.987 ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <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 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 ); | ||
|
|
||
| ``` | ||
|
|
||
| </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( y, p ) | ||
|
|
||
| Computes the [hinge loss][hinge-loss] between two double-precision floating-point number. | ||
|
kgryte marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```c | ||
| 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. | ||
|
|
||
| ```c | ||
| double stdlib_base_float64_hinge( 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 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 ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </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 --> | ||
59 changes: 59 additions & 0 deletions
59
lib/node_modules/@stdlib/ml/base/loss/float64/hinge/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @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 y; | ||
| var p; | ||
| var g; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| y = sample( [ -1, 1 ], { | ||
| 'size': len | ||
| }); | ||
| p = uniform( len, -2.0, 2.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| g = hinge( y[ i % y.length ], p[ i % p.length ] ); | ||
| if ( isnan( g ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( g ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
69 changes: 69 additions & 0 deletions
69
lib/node_modules/@stdlib/ml/base/loss/float64/hinge/benchmark/benchmark.native.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * @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 y; | ||
| var p; | ||
| var g; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| y = sample( [ -1, 1 ], { | ||
| 'size': len | ||
| }); | ||
| p = uniform( len, -2.0, 2.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| g = hinge( y[ i % y.length ], p[ i % p.length ] ); | ||
| if ( isnan( g ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( g ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.