-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add ml/base/loss/float64/hinge-gradient
#12216
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
9
commits into
stdlib-js:develop
Choose a base branch
from
nakul-krishnakumar:ml-loss-hinge-gradient
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 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c323d3a
feat: add `ml/base/loss/float64/hinge-gradient`
nakul-krishnakumar d88b6cd
fix: update function desc and fix failing test cases
nakul-krishnakumar 798bd96
docs: update function desc
nakul-krishnakumar 7eccb37
docs: add test notes
nakul-krishnakumar 8d6cf95
chore: update according to code review
nakul-krishnakumar 56f4a23
chore: update according to code review
nakul-krishnakumar 2f7fde4
refactor: add threshold `t` as func arg
nakul-krishnakumar 18a9e20
chore: update according to code review
nakul-krishnakumar 9f6e6ca
chore: update according to code review
nakul-krishnakumar 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
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
lib/node_modules/@stdlib/ml/base/loss/float64/hinge-gradient/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,235 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # hingeGradient | ||
|
|
||
| > Compute the [hinge loss gradient][hinge-loss-gradient] between two double-precision floating-point numbers. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The [hinge loss gradient][hinge-loss-gradient] is defined as | ||
|
|
||
| <!-- <equation class="equation" label="eq:hinge_loss_gradient" align="center" raw="\frac{\partial \ell}{\partial w_i} = \begin{cases} -yx_i & \text{if } yp < 1 \\ 0 & \text{otherwise} \end{cases}" alt="Equation for the hinge loss gradient."> --> | ||
|
|
||
| ```math | ||
| \frac{\partial \ell}{\partial w_i} = | ||
| \begin{cases} | ||
| -yx_i & \text{if } yp < 1 \\ | ||
| 0 & \text{otherwise} | ||
| \end{cases} | ||
| ``` | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var hingeGradient = require( '@stdlib/ml/base/loss/float64/hinge-gradient' ); | ||
| ``` | ||
|
|
||
| #### hingeGradient( x, y, p ) | ||
|
|
||
| Computes the [hinge loss gradient][hinge-loss-gradient] between two double-precision floating-point numbers. | ||
|
nakul-krishnakumar marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```javascript | ||
| var v = hingeGradient( 3.0, 1.0, 0.782 ); | ||
| // returns -3.0 | ||
|
|
||
| v = hingeGradient( -1.3, 1.0, -0.999 ); | ||
| // returns 1.3 | ||
| ``` | ||
|
|
||
| If any argument is `NaN`, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var v = hingeGradient( NaN, 1.0, 0.782 ); | ||
| // returns NaN | ||
|
|
||
| v = hingeGradient( 1.0, NaN, 0.782 ); | ||
| // returns NaN | ||
|
|
||
| v = hingeGradient( NaN, NaN, 0.782 ); | ||
| // returns NaN | ||
|
|
||
| v = hingeGradient( NaN, NaN, NaN ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| If `y` is not +1 or -1, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var v = hingeGradient( 3.0, -0.9, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| v = hingeGradient( 2.4, 0.453, 0.76 ); | ||
| // 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 hingeGradient = require( '@stdlib/ml/base/loss/float64/hinge-gradient' ); | ||
|
|
||
| var y = sample( [ -1.0, 1.0 ], { | ||
| 'size': 100 | ||
| }); | ||
| var x = uniform( 100, -100.0, 100.0, { | ||
| 'dtype': 'float64' | ||
| }); | ||
| var p = uniform( 100, -5.0, 5.0, { | ||
| 'dtype': 'float64' | ||
| }); | ||
|
|
||
| logEachMap( 'hingeGradient(%0.4f, %0.4f, %0.4f) = %0.4f', x, y, p, hingeGradient ); | ||
|
|
||
| ``` | ||
|
|
||
| </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_gradient.h" | ||
| ``` | ||
|
|
||
| #### stdlib_base_float64_hinge_gradient( x, y, p ) | ||
|
|
||
| Computes the [hinge loss gradient][hinge-loss-gradient] between two double-precision floating-point numbers. | ||
|
nakul-krishnakumar marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```c | ||
| double out = stdlib_base_float64_hinge_gradient( 3.0, 1.0, 0.782 ); | ||
| // returns -3.0 | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **x**: `[in] double` input value. | ||
| - **y**: `[in] double` true target value. | ||
| - **p**: `[in] double` predicted value. | ||
|
|
||
| ```c | ||
| double stdlib_base_float64_hinge_gradient( const double x, 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_gradient.h" | ||
| #include <stdio.h> | ||
|
|
||
| int main( void ) { | ||
| const double x[] = { -10.0, -9.56, -8.67, -7.78, -6.89, 6.89, 7.78, 8.67, 9.56, 10.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_gradient( x[ i ], y[ i ], p[ i ] ); | ||
| printf( "hingeGradient(%lf, %lf, %lf) = %lf\n", x[ 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-gradient]: https://en.wikipedia.org/wiki/Hinge_loss#Optimization | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
61 changes: 61 additions & 0 deletions
61
lib/node_modules/@stdlib/ml/base/loss/float64/hinge-gradient/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,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 hingeGradient = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var len; | ||
| var x; | ||
| var y; | ||
| var p; | ||
| var v; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| y = sample( [ -1.0, 1.0 ], { | ||
| 'size': len | ||
| }); | ||
| x = uniform( len, -100.0, 100.0 ); | ||
| p = uniform( len, -2.0, 2.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = hingeGradient( x[ i%x.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(); | ||
| }); |
71 changes: 71 additions & 0 deletions
71
lib/node_modules/@stdlib/ml/base/loss/float64/hinge-gradient/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,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 hingeGradient = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
| var opts = { | ||
| 'skip': ( hingeGradient instanceof Error ) | ||
| }; | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::native', pkg ), opts, function benchmark( b ) { | ||
| var len; | ||
| var x; | ||
| var y; | ||
| var p; | ||
| var v; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| y = sample( [ -1.0, 1.0 ], { | ||
| 'size': len | ||
| }); | ||
| x = uniform( len, -100.0, 100.0 ); | ||
| p = uniform( len, -2.0, 2.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = hingeGradient( x[ i%x.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(); | ||
| }); |
Oops, something went wrong.
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.