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
135 changes: 135 additions & 0 deletions lib/node_modules/@stdlib/math/array/special/softmax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<!--

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

-->

# softmax

> Compute the softmax function for each element in an input array.

<section class="usage">

## Usage

```javascript
var softmax = require( '@stdlib/math/array/special/softmax' );
```

#### softmax( x\[, options] )

Computes the softmax function for each element in an input array.

```javascript
var v = softmax( [ 1.0, 2.0, 3.0 ] );
// returns [ ~0.090, ~0.245, ~0.665 ]
```

The function has the following parameters:

- **x**: input array.
- **options**: function options.

The function accepts the following options:

- **dtype**: output array data type.

To specify the output array data type, set the `dtype` option.

```javascript
var v = softmax( [ 1.0, 2.0, 3.0 ], {
'dtype': 'float64'
});
// returns <Float64Array>[ ~0.090, ~0.245, ~0.665 ]
```

#### softmax.assign( x, out )

Computes the softmax function for each element in an input array and assigns results to a provided output array.

```javascript
var zeros = require( '@stdlib/array/zeros' );

var out = zeros( 3, 'float64' );
// returns <Float64Array>[ 0.0, 0.0, 0.0 ]

var v = softmax.assign( [ 1.0, 2.0, 3.0 ], out );
// returns <Float64Array>[ ~0.090, ~0.245, ~0.665 ]

var bool = ( v === out );
// returns true
```

The method has the following parameters:

- **x**: input array.
- **out**: output array.

</section>

<!-- /.usage -->

<section class="notes">

- To improve numerical stability, the function subtracts the maximum input value before exponentiation.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var logEach = require( '@stdlib/console/log-each' );
var softmax = require( '@stdlib/math/array/special/softmax' );

// Generate an array of random numbers:
var x = uniform( 10, -3.0, 3.0, {
'dtype': 'generic'
});

// Perform element-wise computation:
var y = softmax( x );

// Print the results:
logEach( 'softmax(%f) = %f', x, y );
```

</section>

<!-- /.examples -->

<!-- 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">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @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 pow = require( '@stdlib/math/base/special/pow' );
var uniform = require( '@stdlib/random/array/uniform' );
var zeros = require( '@stdlib/array/zeros' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var softmax = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = [
uniform( len, -10.0, 10.0, {
'dtype': 'float64'
}),
uniform( len, -10.0, 10.0, {
'dtype': 'float64'
})
];
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var o;
var i;

out = zeros( len, 'float64' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
o = softmax.assign( x[ i%x.length ], out );
if ( isNaN( o[ i%len ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isNaN( o[ i%len ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s:len=%d', pkg, len ), f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @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 pow = require( '@stdlib/math/base/special/pow' );
var uniform = require( '@stdlib/random/array/uniform' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var softmax = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = [
uniform( len, -10.0, 10.0, {
'dtype': 'float64'
}),
uniform( len, -10.0, 10.0, {
'dtype': 'float64'
})
];
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var o;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
o = softmax( x[ i%x.length ] );
if ( isNaN( o[ i%len ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isNaN( o[ i%len ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s:len=%d', pkg, len ), f );
}
}

main();
Loading
Loading