activation

package
v0.28.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 8 Imported by: 7 modules, 21 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	SeluAlpha = 1.67326324
	SeluScale = 1.05070098
)
View Source
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

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

func ApplyFromScope(scope *model.Scope, x *Node) *Node

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

const (
	TypeNone Type = iota
	TypeRelu
	TypeSigmoid
	TypeHardSigmoid
	TypeLeakyRelu
	TypeSelu
	TypeSwish
	TypeHardSwish

	// TypeSilu is an alias to TypeSwish
	TypeSilu

	TypeTanh

	TypeGelu
	TypeGeluApprox

	TypeSwiGLU
)

func FromName

func FromName(activationName string) Type

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

func TypeString(s string) (Type, error)

TypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func TypeValues

func TypeValues() []Type

TypeValues returns all values of the enum

func (Type) HiddenDimMultiplier

func (act Type) HiddenDimMultiplier() int

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

func (i Type) IsAType() bool

IsAType returns "true" if the value is listed in the enum definition. "false" otherwise

func (Type) String

func (i Type) String() string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL