Choose an activation
An activation is available in three forms: a function on a tensor (x.relu6()), an entry of
the network DSL (relu6()), and a module (ReLU6()). All three compute the same values.
The activations
| Activation | Definition | Typical use |
|---|---|---|
|
|
The default for hidden layers |
|
|
Where units must not go silent |
|
as |
Convolutional networks; one slope, or one per channel |
|
|
Activations with a mean closer to zero |
|
|
Self-normalizing dense networks |
|
|
Networks for mobile devices and for quantization |
|
|
A sigmoid without an exponential |
|
|
MobileNetV3-style networks |
|
|
Gates and binary outputs |
|
|
Recurrent networks, outputs in |
|
|
Modern convolutional networks |
|
|
Transformers |
|
|
A smooth ReLU; outputs that must be positive |
|
|
A smooth alternative to ReLU and SiLU |
|
|
Class probabilities |
In a network
val model = definition<FP32, Float> {
network(ctx) {
input(64)
dense(128)
hardSwish()
dense(10)
softmax()
}
}
An activation that has no DSL entry of its own can be given as a function:
activation { it.softplus(beta = 2f) }.
PReLU
PReLU has a parameter, so it is a module. numParameters is 1 for one slope for the whole
input, or the number of channels of an input [N, C, …]. Every slope starts at init.
val prelu = PReLU<FP32, Float>(numParameters = 16, init = 0.25f)
val y = prelu.forward(x, ctx)
The slope is a trainable parameter: an optimizer that holds the module’s parameters updates it.
How they are built and checked
relu6, hardSigmoid, hardSwish, softplus, mish, selu and prelu are composed from
operations that exist already (clamp, exp, log, tanh, elu, relu). They need no
kernel of their own: every backend, the gradient tape and the graph export handle them as the
operations they are made of.
Their values and gradients are held to PyTorch (torch.nn.functional) by suite TS-018 of
skainet-ground-truth: at the points where a formula changes, on random values, and for the
gradient of the PReLU slope.
softplus returns x where beta * x is above 20. That is the same value in a 32-bit float,
and the exponential is not evaluated there, so a large input cannot overflow.