Compute the softmax function for each element in an input array.
var softmax = require( '@stdlib/math/array/special/softmax' );Computes the softmax function for each element in an input array.
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.
var v = softmax( [ 1.0, 2.0, 3.0 ], {
'dtype': 'float64'
});
// returns <Float64Array>[ ~0.090, ~0.245, ~0.665 ]Computes the softmax function for each element in an input array and assigns results to a provided output array.
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 trueThe method has the following parameters:
- x: input array.
- out: output array.
- To improve numerical stability, the function subtracts the maximum input value before exponentiation.
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 );