Skip to content

Commit 0f64adc

Browse files
fix: update according to code review
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 80993d8 commit 0f64adc

18 files changed

Lines changed: 104 additions & 67 deletions

File tree

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ limitations under the License.
2424
2525
<section class="intro">
2626

27-
The [hinge loss][hinge-loss] between `y` and `p` is defined as
28-
29-
<!-- <equation class="equation" label="eq:hinge_loss" align="center" raw="g = max(0, 1 - y*p)" alt="Equation for the sample correlation distance."> -->
30-
31-
```math
32-
g = max(0, 1 - y*p)
33-
```
34-
35-
<!-- </equation> -->
36-
37-
3827
</section>
3928

4029
<!-- /.intro -->
@@ -59,6 +48,26 @@ v = hinge( 1.0, -0.9 );
5948
// returns 1.9
6049
```
6150

51+
If either argument is `NaN`, the function returns `NaN`.
52+
53+
```javascript
54+
var g = hinge( NaN, 12.0 );
55+
// returns NaN
56+
57+
g = hinge( 5.0, NaN );
58+
// returns NaN
59+
```
60+
61+
If `y` is not +1 or -1, the function returns `NaN`.
62+
63+
```javascript
64+
var g = hinge( 2.3, 1.0 );
65+
// returns NaN
66+
67+
g = hinge( -1.3, 0.987 );
68+
// returns NaN
69+
```
70+
6271
</section>
6372

6473
<!-- /.usage -->

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/benchmark/benchmark.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ var hinge = require( './../lib' );
3131
// MAIN //
3232

3333
bench( pkg, function benchmark( b ) {
34+
var len;
3435
var y;
3536
var p;
3637
var g;
3738
var i;
3839

40+
len = 100;
3941
y = sample( [ -1, 1 ], {
40-
'size': 100
42+
'size': len
4143
});
42-
p = uniform( 100, -2.0, 2.0 );
44+
p = uniform( len, -2.0, 2.0 );
4345

4446
b.tic();
4547
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/benchmark/benchmark.native.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ var opts = {
4141
// MAIN //
4242

4343
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
44+
var len;
4445
var y;
4546
var p;
4647
var g;
4748
var i;
4849

50+
len = 100;
4951
y = sample( [ -1, 1 ], {
50-
'size': 100
52+
'size': len
5153
});
52-
p = uniform( 100, -2.0, 2.0 );
54+
p = uniform( len, -2.0, 2.0 );
5355

5456
b.tic();
5557
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/benchmark/c/native/benchmark.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,43 @@ static double random_uniform( const double min, const double max ) {
8686
return min + ( v*(max-min) );
8787
}
8888

89+
/**
90+
* Randomly samples either +1 or -1.
91+
*
92+
* @return random number
93+
*/
94+
static double random_sample( void ) {
95+
double v = (double)rand() / ( (double)RAND_MAX );
96+
if ( v >= 0.5 ) {
97+
return 1;
98+
}
99+
return -1;
100+
}
101+
89102
/**
90103
* Runs a benchmark.
91104
*
92105
* @param y true target value
93106
* @return elapsed time in seconds
94107
*/
95-
static double benchmark( const double y ) {
108+
static double benchmark( void ) {
96109
double *p;
110+
double *y;
97111
double elapsed;
98112
double g;
99113
double t;
100114
int i;
101115

102116
p = (double *) malloc( 100 * sizeof( double ) );
117+
y = (double *) malloc( 100 * sizeof( double ) );
103118
for ( i = 0; i < 100; i++ ) {
119+
y[ i ] = random_sample();
104120
p[ i ] = random_uniform( -2.0, 2.0 );
105121
}
106122

107123
t = tic();
108124
for ( i = 0; i < ITERATIONS; i++ ) {
109-
g = stdlib_base_float64_hinge( y, p[ i % 100 ] );
125+
g = stdlib_base_float64_hinge( y[ i % 100 ], p[ i % 100 ] );
110126
if ( g != g ) {
111127
printf( "should not return NaN\n" );
112128
break;
@@ -116,6 +132,7 @@ static double benchmark( const double y ) {
116132
if ( g != g ) {
117133
printf( "should not return NaN\n" );
118134
}
135+
free( y );
119136
free( p );
120137
return elapsed;
121138
}
@@ -125,24 +142,15 @@ static double benchmark( const double y ) {
125142
*/
126143
int main( void ) {
127144
double elapsed;
128-
double y;
129145
int i;
130146

131147
// Use the current time to seed the random number generator:
132148
srand( time( NULL ) );
133149

134150
print_version();
135-
y = 1;
136-
for ( i = 0; i < REPEATS; i++ ) {
137-
printf( "# c::native::%s:y=%.1f\n", NAME, y );
138-
elapsed = benchmark( y );
139-
print_results( elapsed );
140-
printf( "ok %d benchmark finished\n", i+1 );
141-
}
142-
y = -1;
143151
for ( i = 0; i < REPEATS; i++ ) {
144-
printf( "# c::native::%s:y=%.1f\n", NAME, y );
145-
elapsed = benchmark( y );
152+
printf( "# c::native::%s\n", NAME );
153+
elapsed = benchmark();
146154
print_results( elapsed );
147155
printf( "ok %d benchmark finished\n", i+1 );
148156
}

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/docs/repl.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
{{alias}}( y, p )
33
Computes the hinge loss between two double-precision floating-point number.
44

5+
If either argument is `NaN`, the function returns `NaN`.
6+
If `y` is not +1 or -1, the function returns `NaN`.
7+
58
Parameters
69
----------
710
y: number
@@ -25,7 +28,7 @@
2528
~0.001
2629
> g = {{alias}}( -1.0, 0.2 )
2730
1.2
28-
> g = {{alias}}( NaN, NaN )
31+
> g = {{alias}}( NaN, 0.987 )
2932
NaN
3033

3134
See Also

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/examples/c/example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main( void ) {
2626
double v;
2727
int i;
2828
for ( i = 0; i < 10; i++ ) {
29-
v = stdlib_base_float64_hinge( x[ i ] );
29+
v = stdlib_base_float64_hinge( y[ i ], p[ i ] );
3030
printf( "hinge(%lf, %lf) = %lf\n", y[ i ], p[ i ], v );
3131
}
3232
}

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/lib/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var max = require( '@stdlib/math/base/special/max' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2425

2526

2627
// MAIN //
@@ -58,6 +59,9 @@ var max = require( '@stdlib/math/base/special/max' );
5859
* // returns 1.9
5960
*/
6061
function hinge( y, p ) {
62+
if ( isnan( y ) || isnan( p ) ) {
63+
return NaN;
64+
}
6165
if ( y !== -1.0 && y !== 1.0 ) {
6266
return NaN;
6367
}

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/manifest.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
"libraries": [],
3737
"libpath": [],
3838
"dependencies": [
39-
"@stdlib/napi/create-double",
40-
"@stdlib/napi/export",
41-
"@stdlib/napi/argv",
42-
"@stdlib/napi/argv-double",
39+
"@stdlib/math/base/napi/binary",
4340
"@stdlib/math/base/special/max",
41+
"@stdlib/math/base/assert/is-nan",
4442
"@stdlib/constants/float64/nan"
4543
]
4644
},
@@ -56,6 +54,7 @@
5654
"libpath": [],
5755
"dependencies": [
5856
"@stdlib/math/base/special/max",
57+
"@stdlib/math/base/assert/is-nan",
5958
"@stdlib/constants/float64/nan"
6059
]
6160
},
@@ -71,6 +70,7 @@
7170
"libpath": [],
7271
"dependencies": [
7372
"@stdlib/math/base/special/max",
73+
"@stdlib/math/base/assert/is-nan",
7474
"@stdlib/constants/float64/nan"
7575
]
7676
}

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
"loss",
5959
"float64",
6060
"hinge",
61-
"hinge loss"
61+
"hinge loss",
62+
"double-precision",
63+
"double",
64+
"dbl"
6265
],
6366
"__stdlib__": {}
6467
}

lib/node_modules/@stdlib/ml/base/loss/float64/hinge/src/addon.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@
1717
*/
1818

1919
#include "stdlib/ml/base/loss/float64/hinge.h"
20-
#include "stdlib/napi/create_double.h"
21-
#include "stdlib/napi/argv_double.h"
22-
#include "stdlib/napi/export.h"
23-
#include "stdlib/napi/argv.h"
24-
#include <node_api.h>
20+
#include "stdlib/math/base/napi/binary.h"
2521

26-
static napi_value addon( napi_env env, napi_callback_info info ) {
27-
STDLIB_NAPI_ARGV( env, info, argv, argc, 2 );
28-
STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 0 );
29-
STDLIB_NAPI_ARGV_DOUBLE( env, p, argv, 1 );
30-
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_float64_hinge( y, p ), v );
31-
return v;
32-
}
33-
34-
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
22+
STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_float64_hinge )

0 commit comments

Comments
 (0)