Documentation
¶
Overview ¶
Package activations implements several common activations, and includes a generic Apply method to apply an activation by its type.
There is also FromName to convert an activation name (string) to its type, and ApplyFromContext that applies an activation based on the hyperparameter ParamActivation defined in a context.
Index ¶
- Constants
- Variables
- func Apply(activation Type, x *Node) *Node
- func ApplyFromContext(ctx *context.Context, x *Node) *Node
- func Gelu(x *Node) *Node
- func GeluApproximate(x *Node) *Node
- func HardSwish(x *Node) *Node
- func LeakyRelu(x *Node) *Node
- func LeakyReluWithAlpha(x *Node, alpha float64) *Node
- func ParameterizedHardSwish(x, scale, bias *Node) *Node
- func Relu(x *Node) *Node
- func Selu(x *Node) *Node
- func Swish(x *Node) *Node
- func TypeStrings() []string
- type Type
Constants ¶
const ( SeluAlpha = 1.67326324 SeluScale = 1.05070098 )
const ( // ParamActivation context hyperparameter defines the activation to use, for models using ApplyFromContext. // Available values are: `none`, `relu`, `leaky_relu`, `sigmoid`, `tanh`, `swish` (same as `silu`), `hard_swish`, // `selu`, `gelu` or `gelu_approx`. // The default is `relu`. // See activations.TypeValues for complete list. ParamActivation = "activation" )
Variables ¶
var ( // Aliases are alternative names to activations. Aliases = map[string]Type{ "gelu_pytorch_tanh": TypeGeluApprox, } )
Functions ¶
func Apply ¶
func Apply(activation Type, x *Node) *Node
Apply the given activation type. The TypeNone activation is a no-op.
See TypeValues for valid values.
func ApplyFromContext ¶
ApplyFromContext picks an activation function from the context using ParamActivation parameter, and applies it to x.
It defaults to "relu".
func Gelu ¶
func Gelu(x *Node) *Node
Gelu activation function, the original Gelu function.
It is defined as Gelu(x) = x * 0.5 * (1 + Erf(x / √2)).
The GELU activation function was introduced in "Gaussian Error Linear Units (GELUs)" [Hendrycks et al. 2016](https://arxiv.org/abs/1606.08415).
The exact version is slower in TPUs due to the "Erf" function, but some argue it is more stable. See discussion in: https://github.com/jax-ml/jax/issues/4428
func GeluApproximate ¶
func GeluApproximate(x *Node) *Node
GeluApproximate is a close approximation to the original Gelu function.
It is defined as Gelu(x) = x * 0.5 * (1 + Tanh(Sqrt(2/Pi) * (x+0.044715*x^3))).
The GELU activation function was introduced in "Gaussian Error Linear Units (GELUs)" [Hendrycks et al. 2016](https://arxiv.org/abs/1606.08415).
The exact version is slower in TPUs, some argue it is more stable. See discussion in: https://github.com/jax-ml/jax/issues/4428
func HardSwish ¶ added in v0.27.0
func HardSwish(x *Node) *Node
HardSwish activation function.
It returns x·ReLU6(x+3)/6.
See [1] for details and analysis.
[1]: "Evaluating Model Performance with Hard-Swish Activation Function Adjustments", https://arxiv.org/abs/2410.06879
func LeakyRelu ¶
func LeakyRelu(x *Node) *Node
LeakyRelu activation function. It allows a small gradient when the unit is not active (x < 0). The `alpha` parameter is fixed at 0.3.
It returns `x if x >= 0; alpha*x if x < 0`.
func LeakyReluWithAlpha ¶
func LeakyReluWithAlpha(x *Node, alpha float64) *Node
LeakyReluWithAlpha activation function. It allows a small gradient when the unit is not active (x < 0).
It returns `x if x >= 0; alpha*x if x < 0`.
func ParameterizedHardSwish ¶ added in v0.27.0
func ParameterizedHardSwish(x, scale, bias *Node) *Node
ParameterizedHardSwish activation function, it allows more flexibility in setting the HardSwish bias and scale parameters.
It returns x·Clip((x·scale+bias), 0, 1).
- x: operand to be "activated" - scale: scale parameter, scalar. - bias: bias parameter, scalar.
See HardSwish for the default parameters and [1] for details and analysis.
[1]: "Evaluating Model Performance with Hard-Swish Activation Function Adjustments", https://arxiv.org/abs/2410.06879
func Relu ¶
func Relu(x *Node) *Node
Relu activation function. It returns Max(x, 0), and is commonly used as an activation function in neural networks.
func Selu ¶
func Selu(x *Node) *Node
Selu stands for Scaled Exponential Linear Unit (SELU) activation function is defined as: . $SeluScale * x$ if $x > 0$ . $SeluScale * SeluAlpha * (e^x - 1)$ if $x < 0$
Ideally, it should be matched with a "LecunNormal initializer" and the dropout variant called "AlphaDropout" -- TODO, neither are implemented yet.
func Swish ¶
func Swish(x *Node) *Node
Swish activation (or SiLU) returns `x * Sigmoid(x)`.
The SiLU activation function was introduced in "Gaussian Error Linear Units (GELUs)" [Hendrycks et al. 2016](https://arxiv.org/abs/1606.08415) and "Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning" [Elfwing et al. 2017](https://arxiv.org/abs/1702.03118) and was independently discovered (and called swish) in "Searching for Apply Functions" [Ramachandran et al. 2017](https://arxiv.org/abs/1710.05941)
Here the beta parameter is fixed at 1.0.
func TypeStrings ¶
func TypeStrings() []string
TypeStrings returns a slice of all String values of the enum
Types ¶
type Type ¶
type Type int
Type is an enum for the supported activation functions.
It is converted to snake-format strings (e.g.: TypeLeakyRelu -> "leaky_relu"), and can be converted from string by using
func FromName ¶
FromName converts the name of an activation to its type. It panics with a helpful message if name is invalid.
And empty string is converted to TypeNone.
func TypeString ¶
TypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func (Type) IsAType ¶
IsAType returns "true" if the value is listed in the enum definition. "false" otherwise
func (Type) ToBackend ¶ added in v0.27.0
func (t Type) ToBackend() backends.ActivationType
ToBackend converts an activations.Type to the corresponding backends.ActivationType. Unsupported activation types map to backends.ActivationNone.