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 ApplyFromScope that applies an activation based on the hyperparameter ParamActivation defined in a model.
Index ¶
- Constants
- Variables
- func Apply(activation Type, x *Node) *Node
- func ApplyFromScope(scope *model.Scope, x *Node) *Node
- func Gelu(x *Node) *Node
- func GeluApproximate(x *Node) *Node
- func HardSigmoid(x *Node) *Node
- func HardSigmoidWith(x *Node, alpha, beta float64) *Node
- func HardSwish(x *Node) *Node
- func HardSwishWith(x, scale, bias *Node) *Node
- func LeakyRelu(x *Node) *Node
- func LeakyReluWith(x *Node, alpha float64) *Node
- func Relu(x *Node) *Node
- func Selu(x *Node) *Node
- func SwiGLU(x *Node) *Node
- func Swish(x *Node) *Node
- func TypeStrings() []string
- type Type
Constants ¶
const ( SeluAlpha = 1.67326324 SeluScale = 1.05070098 )
const ( // ParamActivation scope hyperparameter defines the activation to use, for models using ApplyFromScope. // 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 ApplyFromScope ¶
ApplyFromScope picks an activation function from the scope 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 HardSigmoid ¶
func HardSigmoid(x *Node) *Node
HardSigmoid activation function.
It returns max(0, min(1, alpha*x + beta)) with default alpha=0.2, beta=0.5.
func HardSigmoidWith ¶
func HardSigmoidWith(x *Node, alpha, beta float64) *Node
HardSigmoidWith activation function with configurable alpha and beta.
It returns max(0, min(1, alpha*x + beta)).
func HardSwish ¶
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 HardSwishWith ¶
func HardSwishWith(x, scale, bias *Node) *Node
HardSwishWith 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 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 LeakyReluWith ¶
func LeakyReluWith(x *Node, alpha float64) *Node
LeakyReluWith 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 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 SwiGLU ¶
func SwiGLU(x *Node) *Node
SwiGLU activation takes a pre-projected tensor of size [..., 2 * hiddenDim] and applies the Swish-Gated Linear Unit activation.
It uses half for the "gate" (x_gate), and the second for the "value" (x_value). The result is the product of the two:
SwiGLU(x) = Swish(x_gate) * x_value
Where Swish(x) = x * sigmoid(x).
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) HiddenDimMultiplier ¶
HiddenDimMultiplier returns the multiplier of the hidden dimension for the given activation. This is 2 for SwiGLU, and 1 for all other activations.
func (Type) IsAType ¶
IsAType returns "true" if the value is listed in the enum definition. "false" otherwise
func (Type) ToBackend ¶
func (t Type) ToBackend() compute.ActivationConfig
ToBackend converts an activations.Type to the corresponding compute.ActivationConfig. Unsupported activation types map to compute.ActivationNone.