diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/README.md b/lib/node_modules/@stdlib/math/array/special/softmax/README.md new file mode 100644 index 000000000000..496b857a592b --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/README.md @@ -0,0 +1,135 @@ + + +# softmax + +> Compute the softmax function for each element in an input array. + +
+ +## 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 [ ~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 [ 0.0, 0.0, 0.0 ] + +var v = softmax.assign( [ 1.0, 2.0, 3.0 ], out ); +// returns [ ~0.090, ~0.245, ~0.665 ] + +var bool = ( v === out ); +// returns true +``` + +The method has the following parameters: + +- **x**: input array. +- **out**: output array. + +
+ + + +
+ +- To improve numerical stability, the function subtracts the maximum input value before exponentiation. + +
+ + + +
+ +## Examples + + + +```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 ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/math/array/special/softmax/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..971b590dadb1 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/benchmark/benchmark.assign.js @@ -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(); diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/math/array/special/softmax/benchmark/benchmark.length.js new file mode 100644 index 000000000000..947c25b663d4 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/benchmark/benchmark.length.js @@ -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(); diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/docs/repl.txt b/lib/node_modules/@stdlib/math/array/special/softmax/docs/repl.txt new file mode 100644 index 000000000000..cbb11a446df2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/docs/repl.txt @@ -0,0 +1,54 @@ + +{{alias}}( x[, options] ) + Computes the softmax function for each element in an input array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. + + Returns + ------- + out: Array|TypedArray + Output array. + + Examples + -------- + > var v = {{alias}}( [ 1.0, 2.0, 3.0 ] ) + [ ~0.090, ~0.245, ~0.665 ] + + +{{alias}}.assign( x, out ) + Computes the softmax function for each element in an input array and assigns + results to a provided output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + out: Array|TypedArray|Object + Output array. + + Returns + ------- + out: Array|TypedArray|Object + Output array. + + Examples + -------- + > var out = {{alias:@stdlib/array/zeros}}( 3, 'float64' ); + > var v = {{alias}}.assign( [ 1.0, 2.0, 3.0 ], out ) + [ ~0.090, ~0.245, ~0.665 ] + > var bool = ( out === v ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/array/special/softmax/docs/types/index.d.ts new file mode 100644 index 000000000000..e505832341bc --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { DataType, ArrayLike, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = ArrayLike | AccessorArrayLike; + +/** +* Output array. +*/ +type OutputArray = ArrayLike | AccessorArrayLike; + +/** +* Interface defining options. +*/ +interface Options { + /** + * Output array data type. + */ + dtype?: DataType; +} + +/** +* Interface for performing element-wise computation. +*/ +interface Softmax { + /** + * Computes the softmax function for each element in an input array. + * + * @param x - input array + * @param options - function options + * @returns output array + * + * @example + * var out = softmax( [ 1.0, 2.0, 3.0 ] ); + * // returns [ ~0.090, ~0.245, ~0.665 ] + */ + ( x: InputArray, options?: Options ): OutputArray; + + /** + * Computes the softmax function for each element in an input array and assigns results to a provided output array. + * + * @param x - input array + * @param out - output array + * @returns output array + * + * @example + * var y = [ 0.0, 0.0, 0.0 ]; + * + * var out = softmax.assign( [ 1.0, 2.0, 3.0 ], y ); + * // returns [ ~0.090, ~0.245, ~0.665 ] + * + * var bool = ( out === y ); + * // returns true + */ + assign>( x: InputArray, out: U ): U; +} + +/** +* Computes the softmax function for each element in an input array. +* +* @param x - input array +* @param options - function options +* @returns output array +* +* @example +* var out = softmax( [ 1.0, 2.0, 3.0 ] ); +* // returns [ ~0.090, ~0.245, ~0.665 ] +* +* @example +* var y = [ 0.0, 0.0, 0.0 ]; +* +* var out = softmax.assign( [ 1.0, 2.0, 3.0 ], y ); +* // returns [ ~0.090, ~0.245, ~0.665 ] +* +* var bool = ( out === y ); +* // returns true +*/ +declare const softmax: Softmax; + + +// EXPORTS // + +export = softmax; diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/docs/types/test.ts b/lib/node_modules/@stdlib/math/array/special/softmax/docs/types/test.ts new file mode 100644 index 000000000000..95924e6aebc0 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/docs/types/test.ts @@ -0,0 +1,125 @@ +/* +* @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. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions */ + +/// + +import zeros = require( '@stdlib/array/zeros' ); +import softmax = require( './index' ); + + +// TESTS // + +// The function returns an array-like object... +{ + softmax( [ 1.0, 2.0, 3.0 ] ); // $ExpectType OutputArray + softmax( [ 1.0, 2.0, 3.0 ], {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + softmax( '5' ); // $ExpectError + softmax( 5 ); // $ExpectError + softmax( true ); // $ExpectError + softmax( false ); // $ExpectError + softmax( null ); // $ExpectError + softmax( void 0 ); // $ExpectError + softmax( {} ); // $ExpectError + softmax( ( x: number ): number => x ); // $ExpectError + + softmax( '5', {} ); // $ExpectError + softmax( 5, {} ); // $ExpectError + softmax( true, {} ); // $ExpectError + softmax( false, {} ); // $ExpectError + softmax( null, {} ); // $ExpectError + softmax( void 0, {} ); // $ExpectError + softmax( {}, {} ); // $ExpectError + softmax( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + softmax( [ 1.0, 2.0, 3.0 ], '5' ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], true ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], false ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], null ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], [] ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': 5 } ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': true } ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': false } ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': null } ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': [] } ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': {} } ); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + softmax(); // $ExpectError + softmax( [ 1.0, 2.0, 3.0 ], {}, {} ); // $ExpectError +} + +// The function has an `assign` method which returns an array-like object... +{ + const x = zeros( 3, 'float64' ); + + softmax.assign( x, x ); // $ExpectType Float64Array +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... +{ + const x = zeros( 3, 'generic' ); + + softmax.assign( '5', x ); // $ExpectError + softmax.assign( 5, x ); // $ExpectError + softmax.assign( true, x ); // $ExpectError + softmax.assign( false, x ); // $ExpectError + softmax.assign( null, x ); // $ExpectError + softmax.assign( void 0, x ); // $ExpectError + softmax.assign( {}, x ); // $ExpectError + softmax.assign( ( x: number ): number => x, x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an array-like object... +{ + const x = zeros( 3, 'generic' ); + + softmax.assign( x, '5' ); // $ExpectError + softmax.assign( x, 5 ); // $ExpectError + softmax.assign( x, true ); // $ExpectError + softmax.assign( x, false ); // $ExpectError + softmax.assign( x, null ); // $ExpectError + softmax.assign( x, void 0 ); // $ExpectError + softmax.assign( x, {} ); // $ExpectError + softmax.assign( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( 3, 'generic' ); + + softmax.assign(); // $ExpectError + softmax.assign( x ); // $ExpectError + softmax.assign( x, x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/examples/index.js b/lib/node_modules/@stdlib/math/array/special/softmax/examples/index.js new file mode 100644 index 000000000000..5c8d039921a3 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/examples/index.js @@ -0,0 +1,34 @@ +/** +* @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'; + +var uniform = require( '@stdlib/random/array/uniform' ); +var logEach = require( '@stdlib/console/log-each' ); +var softmax = require( './../lib' ); + +// 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 ); diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/lib/index.js b/lib/node_modules/@stdlib/math/array/special/softmax/lib/index.js new file mode 100644 index 000000000000..f4125f80bad5 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/lib/index.js @@ -0,0 +1,40 @@ +/** +* @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'; + +/** +* Compute the softmax function for each element in an input array. +* +* @module @stdlib/math/array/special/softmax +* +* @example +* var softmax = require( '@stdlib/math/array/special/softmax' ); +* +* var y = softmax( [ 1.0, 2.0, 3.0 ] ); +* // returns [ ~0.090, ~0.245, ~0.665 ] +*/ + +// MODULES // + +var softmax = require( './main.js' ); + + +// EXPORTS // + +module.exports = softmax; diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/lib/main.js b/lib/node_modules/@stdlib/math/array/special/softmax/lib/main.js new file mode 100644 index 000000000000..991886f0882a --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/lib/main.js @@ -0,0 +1,224 @@ +/** +* @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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var getDType = require( '@stdlib/array/dtype' ); +var dtypes = require( '@stdlib/array/dtypes' ); +var empty = require( '@stdlib/array/empty' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var exp = require( '@stdlib/math/base/special/exp' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var IDTYPES = dtypes( 'real_and_generic' ); +var ODTYPES = dtypes( 'floating_point_and_generic' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating if a provided data type is allowed. +* +* @private +* @param {string} dt - data type +* @param {StringArray} list - list of allowed data types +* @returns {boolean} boolean indicating if a data type is allowed +*/ +function isAllowedDataType( dt, list ) { + var i; + for ( i = 0; i < list.length; i++ ) { + if ( list[ i ] === dt ) { + return true; + } + } + return false; +} + +/** +* Returns the default output data type. +* +* @private +* @param {string} dt - input data type +* @returns {string} output data type +*/ +function defaultOutputDType( dt ) { + if ( dt === 'float32' || dt === 'generic' ) { + return dt; + } + return 'float64'; +} + +/** +* Computes the softmax function for each element in an input array and assigns results to a provided output array. +* +* @private +* @param {Collection} x - input array +* @param {Collection} out - output array +* @throws {TypeError} first argument must be a collection +* @throws {TypeError} second argument must be a collection +* @throws {RangeError} output array must have the same length as the input array +* @returns {Collection} output array +*/ +function assign( x, out ) { + var count; + var max; + var sum; + var len; + var p; + var v; + var i; + + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', x ) ); + } + if ( !isCollection( out ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a collection. Value: `%s`.', out ) ); + } + len = x.length; + if ( out.length !== len ) { + throw new RangeError( format( 'invalid argument. Output array must have the same length as the input array. Input length: `%u`. Output length: `%u`.', len, out.length ) ); + } + if ( len === 0 ) { + return out; + } + max = NINF; + for ( i = 0; i < len; i++ ) { + v = x[ i ]; + if ( isNaN( v ) ) { + for ( i = 0; i < len; i++ ) { + out[ i ] = NaN; + } + return out; + } + if ( v > max ) { + max = v; + } + } + if ( max === Infinity ) { + count = 0; + for ( i = 0; i < len; i++ ) { + if ( x[ i ] === Infinity ) { + count += 1; + } + } + p = 1.0 / count; + for ( i = 0; i < len; i++ ) { + out[ i ] = ( x[ i ] === Infinity ) ? p : 0.0; + } + return out; + } + if ( max === NINF ) { + p = 1.0 / len; + for ( i = 0; i < len; i++ ) { + out[ i ] = p; + } + return out; + } + sum = 0.0; + for ( i = 0; i < len; i++ ) { + v = exp( x[ i ] - max ); + out[ i ] = v; + sum += v; + } + for ( i = 0; i < len; i++ ) { + out[ i ] /= sum; + } + return out; +} + + +// MAIN // + +/** +* Computes the softmax function for each element in an input array. +* +* @name softmax +* @type {Function} +* @param {Collection} x - input array +* @param {Object} [options] - function options +* @param {string} [options.dtype] - output array data type +* @throws {TypeError} first argument must be a collection +* @throws {TypeError} first argument must be a collection having a supported data type +* @throws {TypeError} options argument must be a plain object +* @throws {TypeError} must provide a valid `dtype` option +* @returns {(Array|TypedArray)} output array +* +* @example +* var out = softmax( [ 1.0, 2.0, 3.0 ] ); +* // returns [ ~0.090, ~0.245, ~0.665 ] +* +* @example +* var out = softmax( [ 1.0, 2.0, 3.0 ], { +* 'dtype': 'float64' +* }); +* // returns [ ~0.090, ~0.245, ~0.665 ] +* +* @example +* var y = [ 0.0, 0.0, 0.0 ]; +* +* var out = softmax.assign( [ 1.0, 2.0, 3.0 ], y ); +* // returns [ ~0.090, ~0.245, ~0.665 ] +* +* var bool = ( out === y ); +* // returns true +*/ +function softmax( x, options ) { + var outputDType; + var inputDType; + var out; + + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', x ) ); + } + inputDType = getDType( x ); + if ( !isAllowedDataType( inputDType, IDTYPES ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a collection having a supported data type. Value: `%s`.', x ) ); + } + if ( arguments.length > 1 ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a plain object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dtype' ) ) { + outputDType = options.dtype; + if ( !isAllowedDataType( outputDType, ODTYPES ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: `%s`. Option: `%s`.', 'dtype', ODTYPES.join( ', ' ), outputDType ) ); + } + } + } + if ( outputDType === void 0 ) { + outputDType = defaultOutputDType( inputDType ); + } + out = empty( x.length, outputDType ); + return assign( x, out ); +} + +setReadOnly( softmax, 'assign', assign ); + + +// EXPORTS // + +module.exports = softmax; diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/package.json b/lib/node_modules/@stdlib/math/array/special/softmax/package.json new file mode 100644 index 000000000000..116abe775904 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/math/array/special/softmax", + "version": "0.0.0", + "description": "Compute the softmax function for each element in an input array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "vector", + "array", + "apply", + "element-wise", + "elementwise", + "softmax", + "probability", + "normalization" + ] +} diff --git a/lib/node_modules/@stdlib/math/array/special/softmax/test/test.js b/lib/node_modules/@stdlib/math/array/special/softmax/test/test.js new file mode 100644 index 000000000000..dfa405e58f13 --- /dev/null +++ b/lib/node_modules/@stdlib/math/array/special/softmax/test/test.js @@ -0,0 +1,336 @@ +/** +* @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 tape = require( 'tape' ); +var empty = require( '@stdlib/array/empty' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var zeros = require( '@stdlib/array/zeros' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +var softmax = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns the sum of an array. +* +* @private +* @param {Collection} x - input array +* @returns {number} sum +*/ +function sum( x ) { + var s; + var i; + + s = 0.0; + for ( i = 0; i < x.length; i++ ) { + s += x[ i ]; + } + return s; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof softmax, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a collection', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + softmax( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which does not have a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( 5, 'bool' ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + softmax( value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a plain object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + softmax( [ 1.0, 2.0, 3.0 ], value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) { + var values; + var i; + + values = [ + 'int32', + 'foo', + 'bar', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + softmax( [ 1.0, 2.0, 3.0 ], { + 'dtype': value + }); + }; + } +}); + +tape( 'the function computes softmax values', function test( t ) { + var out; + + out = softmax( [ 1.0, 2.0, 3.0 ] ); + t.strictEqual( isArray( out ), true, 'returns expected value' ); + t.ok( out[ 0 ] < out[ 1 ] && out[ 1 ] < out[ 2 ], 'returns expected value' ); + t.ok( sum( out ) > 0.99999999999999 && sum( out ) < 1.00000000000001, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying an output array data type', function test( t ) { + var out; + + out = softmax( [ 1.0, 2.0, 3.0 ], { + 'dtype': 'float64' + }); + t.strictEqual( isFloat64Array( out ), true, 'returns expected value' ); + + out = softmax( [ 1.0, 2.0, 3.0 ], { + 'dtype': 'float32' + }); + t.strictEqual( isFloat32Array( out ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function defaults to float32 output for float32 input', function test( t ) { + var out; + + out = softmax( new Float32Array( [ 1.0, 2.0, 3.0 ] ) ); + t.strictEqual( isFloat32Array( out ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function defaults to float64 output for integer input arrays', function test( t ) { + var out; + + out = softmax( new Int32Array( [ 1, 2, 3 ] ) ); + t.strictEqual( isFloat64Array( out ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function is numerically stable for large values', function test( t ) { + var out; + + out = softmax( [ 1000.0, 1001.0 ] ); + t.ok( out[ 0 ] > 0.0 && out[ 0 ] < 1.0, 'returns expected value' ); + t.ok( out[ 1 ] > 0.0 && out[ 1 ] < 1.0, 'returns expected value' ); + t.ok( sum( out ) > 0.99999999999999 && sum( out ) < 1.00000000000001, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles positive infinity by distributing mass among the infinite values', function test( t ) { + var out; + + out = softmax( [ Infinity, 1.0, Infinity ] ); + t.strictEqual( out[ 0 ], 0.5, 'returns expected value' ); + t.strictEqual( out[ 1 ], 0.0, 'returns expected value' ); + t.strictEqual( out[ 2 ], 0.5, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles all negative infinity values', function test( t ) { + var out; + + out = softmax( [ -Infinity, -Infinity, -Infinity ] ); + t.strictEqual( out[ 0 ], 1.0/3.0, 'returns expected value' ); + t.strictEqual( out[ 1 ], 1.0/3.0, 'returns expected value' ); + t.strictEqual( out[ 2 ], 1.0/3.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if the input contains NaN, the function returns all NaN values', function test( t ) { + var out; + + out = softmax( [ 1.0, NaN, 3.0 ] ); + t.strictEqual( isNaN( out[ 0 ] ), true, 'returns expected value' ); + t.strictEqual( isNaN( out[ 1 ] ), true, 'returns expected value' ); + t.strictEqual( isNaN( out[ 2 ] ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function has an `assign` method which throws an error if provided a first argument which is not a collection', function test( t ) { + var values; + var out; + var i; + + out = zeros( 3, 'generic' ); + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + softmax.assign( value, out ); + }; + } +}); + +tape( 'the function has an `assign` method which throws an error if provided a second argument which is not a collection', function test( t ) { + var values; + var x; + var i; + + x = [ 1.0, 2.0, 3.0 ]; + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + softmax.assign( x, value ); + }; + } +}); + +tape( 'the function has an `assign` method which throws an error when input and output arrays do not have equal lengths', function test( t ) { + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + softmax.assign( [ 1.0, 2.0, 3.0 ], zeros( 2, 'generic' ) ); + } +}); + +tape( 'the function has an `assign` method which computes softmax values', function test( t ) { + var out; + var x; + var v; + + x = [ 1.0, 2.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + + v = softmax.assign( x, out ); + t.strictEqual( v, out, 'returns expected value' ); + t.ok( out[ 0 ] < out[ 1 ] && out[ 1 ] < out[ 2 ], 'returns expected value' ); + t.ok( sum( out ) > 0.99999999999999 && sum( out ) < 1.00000000000001, 'returns expected value' ); + + t.end(); +});