Skip to content
Draft
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
125 changes: 125 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gelu/README.md
Original file line number Diff line number Diff line change
@@ -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.

-->

# gelu

> Evaluate the [Gaussian error linear unit][gelu] (GELU).

<section class="intro">

The [Gaussian error linear unit][gelu] (GELU) activation function is defined as:

```math
\mathrm{gelu}(x) = x \cdot \Phi(x) = \frac{x}{2} \left[ 1 + \mathrm{erf}\left( \frac{x}{\sqrt{2}} \right) \right]
```

where $\Phi(x)$ is the cumulative distribution function of the standard normal distribution and $\mathrm{erf}(x)$ is the [error function][@stdlib/math/base/special/erf].

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var gelu = require( '@stdlib/math/base/special/gelu' );
```

#### gelu( x )

Evaluates the Gaussian Error Linear Unit (GELU) activation function.

```javascript
var v = gelu( 0.0 );
// returns 0.0

v = gelu( 1.0 );
// returns ~0.8413

v = gelu( -Infinity );
// returns 0.0

v = gelu( Infinity );
// returns Infinity

v = gelu( NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```javascript
var logEachMap = require( '@stdlib/console/log-each-map' );
var uniform = require( '@stdlib/random/array/uniform' );
var gelu = require( '@stdlib/math/base/special/gelu' );

var opts = {
'dtype': 'float64'
};
var x = uniform( 100, -5.0, 5.0, opts );

logEachMap( 'gelu(%0.4f) = %0.4f', x, gelu );
```

</section>

<!-- /.examples -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/math/base/special/erf`][@stdlib/math/base/special/erf]</span><span class="delimiter">: </span><span class="description">evaluate the error function.</span>
- <span class="package-name">[`@stdlib/math/base/special/erfc`][@stdlib/math/base/special/erfc]</span><span class="delimiter">: </span><span class="description">evaluate the complementary error function.</span>
- <span class="package-name">[`@stdlib/math/base/special/ramp`][@stdlib/math/base/special/ramp]</span><span class="delimiter">: </span><span class="description">evaluate the ramp function.</span>

</section>

<!-- /.related -->

<section class="links">

[gelu]: https://en.wikipedia.org/wiki/Gelu

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

[@stdlib/math/base/special/erf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/erf

[@stdlib/math/base/special/erfc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/erfc

[@stdlib/math/base/special/ramp]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ramp

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var gelu = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

x = uniform( 100, -10.0, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = gelu( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gelu/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( x )
Evaluates the Gaussian Error Linear Unit (GELU) activation function.

Parameters
----------
x: number
Input value.

Returns
-------
y: number
Function value.

Examples
--------
> var y = {{alias}}( 0.0 )
0.0
> y = {{alias}}( 1.0 )
~0.8413
> y = {{alias}}( -Infinity )
0.0
> y = {{alias}}( Infinity )
Infinity
> y = {{alias}}( NaN )
NaN

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* @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

/**
* Evaluate the Gaussian Error Linear Unit (GELU) activation function.
*
* @param x - input value
* @returns function value
*
* @example
* var y = gelu( 0.0 );
* // returns 0.0
*
* @example
* var y = gelu( 1.0 );
* // returns ~0.8413
*
* @example
* var y = gelu( -Infinity );
* // returns 0.0
*
* @example
* var y = gelu( NaN );
* // returns NaN
*/
declare function gelu( x: number ): number;


// EXPORTS //

export = gelu;
44 changes: 44 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gelu/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @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.
*/

import gelu = require( './index' );


// TESTS //

// The function returns a number...
{
gelu( 8 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a value other than a number...
{
gelu( true ); // $ExpectError
gelu( false ); // $ExpectError
gelu( null ); // $ExpectError
gelu( undefined ); // $ExpectError
gelu( '5' ); // $ExpectError
gelu( [] ); // $ExpectError
gelu( {} ); // $ExpectError
gelu( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
gelu(); // $ExpectError
}
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gelu/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @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 logEachMap = require( '@stdlib/console/log-each-map' );
var uniform = require( '@stdlib/random/array/uniform' );
var gelu = require( './../lib' );

var opts = {
'dtype': 'float64'
};
var x = uniform( 100, -5.0, 5.0, opts );

logEachMap( 'gelu(%0.4f) = %0.4f', x, gelu );
49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gelu/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @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';

/**
* Evaluate the Gaussian Error Linear Unit (GELU) activation function.
*
* @module @stdlib/math/base/special/gelu
*
* @example
* var gelu = require( '@stdlib/math/base/special/gelu' );
*
* var y = gelu( 0.0 );
* // returns 0.0
*
* y = gelu( 1.0 );
* // returns ~0.8413
*
* y = gelu( -Infinity );
* // returns 0.0
*
* y = gelu( NaN );
* // returns NaN
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading
Loading