Skip to content

Latest commit

 

History

History
135 lines (84 loc) · 2.91 KB

File metadata and controls

135 lines (84 loc) · 2.91 KB

softmax

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

Usage

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

softmax( x[, options] )

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 ]

softmax.assign( x, out )

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

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 );