Explicit activation stage
Add a named activation as an explicit layer after another layer in a model.
Keras · Class
Keras layers.Activation applies a selected activation function as a layer. Choose a built-in activation by name or provide a callable.
Activation wraps a chosen activation function as a Keras layer. The function can be selected by its built-in string name or supplied directly as a callable, and the layer applies it to the input while preserving the input shape.
keras.layers.Activation(activation, **kwargs)| Name | Type | Description |
|---|---|---|
activation | callable or string | Activation function, supplied as a callable or as the name of an activation in the keras.activations namespace. |
**kwargs | keyword arguments | Base layer keyword arguments, such as name and dtype. |
A tensor produced by applying the selected activation function to the input; the documented example shows the transformed output.
relu.keras.activations namespace; otherwise pass a callable.import keras
import numpy as np
layer = keras.layers.Activation("relu")
output = layer(np.array([-3.0, -1.0, 0.0, 2.0]))
print(output)Creates a ReLU activation layer and applies it element-wise to a NumPy array.
Add a named activation as an explicit layer after another layer in a model.
Use a built-in activation such as ReLU without writing the function manually.
Apply a user-defined tensor transformation supplied as a callable.
The constructor accepts an activation and arbitrary base-layer keyword arguments. The activation may be a callable or the name of an activation in the keras.activations namespace.
Follow-up: How would you express the same operation in a Dense layer? Pass the activation through Dense's activation argument instead of adding a separate Activation layer.
The layer applies the selected activation function to its input output. For example, a ReLU layer maps negative values to zero, as shown by the documented example.
Follow-up: Can the activation be custom? Yes, provide a callable that accepts a tensor and returns the transformed tensor.
Activation is useful when the activation should be represented as its own layer. Keras also supports passing activations through the activation argument of forward layers such as Dense.
Follow-up: When is the separate layer especially useful? It makes the transformation explicit in a sequential architecture or lets it be inserted after another layer.
keras.activations.relukeras.layers.Densekeras.layers.ReLU