Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions lib/node_modules/@stdlib/ml/base/loss/float64/log-gradient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<!--

@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.

-->

# logGradient

> Compute the [log loss gradient][log-loss-gradient] with respect to the predicted value.

<section class="intro">

The [log loss gradient][log-loss-gradient] is defined as

<!-- <equation class="equation" label="eq:log_loss_gradient" align="center" raw="\frac{\partial L}{\partial p} = -\frac{y}{1+e^{yp}}" alt="Equation for the log loss gradient."> -->

```math
\frac{\partial L}{\partial p} = -\frac{y}{1+e^{yp}}
```

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var logGradient = require( '@stdlib/ml/base/loss/float64/log-gradient' );
```

#### logGradient( y, p )

Computes the [log loss gradient][log-loss-gradient] with respect to the predicted value.

```javascript
var v = logGradient( 1.0, 0.782 );
// returns ~-0.314

v = logGradient( 1.0, -0.999 );
// returns ~-0.731
```

If either argument is `NaN`, the function returns `NaN`.

```javascript
var v = logGradient( NaN, 0.782 );
// returns NaN

v = logGradient( 1.0, NaN );
// returns NaN
```

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

```javascript
var v = logGradient( -0.9, 1.0 );
// returns NaN

v = logGradient( 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 logGradient = require( '@stdlib/ml/base/loss/float64/log-gradient' );

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

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

```

</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/log_gradient.h"
```

#### stdlib_base_float64_log_gradient( y, p )

Computes the [log loss gradient][log-loss-gradient] with respect to the predicted value.

```c
double out = stdlib_base_float64_log_gradient( 1.0, 0.782 );
// returns ~-0.314
```

The function accepts the following arguments:

- **y**: `[in] double` true target value.
- **p**: `[in] double` predicted value.

```c
double stdlib_base_float64_log_gradient( 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/log_gradient.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_log_gradient( y[ i ], p[ i ] );
printf( "logGradient(%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">

[log-loss-gradient]: https://en.wikipedia.org/wiki/Cross-entropy#Relation_to_linear_regression

<!-- <related-links> -->


<!-- </related-links> -->

</section>

<!-- /.links -->
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 logGradient = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var len;
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 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = logGradient( 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,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 logGradient = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( logGradient instanceof Error )
};


// MAIN //

bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
var len;
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 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = logGradient( 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();
});
Loading