Direct tensor transformation
Add nonlinear behavior inside a model while retaining explicit control over the function.
Keras · Module
keras.activations provides built-in nonlinear and linear functions for transforming tensors. Use them directly, pass them to layer activation arguments, or select built-ins by string name.
The module provides callable activation functions for transforming tensor values. Use a function directly, pass it to a layer's activation argument, or refer to a built-in function by its string identifier; most functions preserve the input shape and dtype.
keras.activations.<name>(x, ...)| Name | Type | Description |
|---|---|---|
x | Tensor | Input tensor or tensor-like value to transform. |
negative_slope | float | Negative slope used by ReLU for values below its threshold. |
max_value | float or None | Optional upper bound for ReLU outputs. |
threshold | float | Threshold used by ReLU. |
axis | int | Axis along which softmax is computed. |
Each activation returns a tensor containing the transformed values; the documentation specifies that an activation callable should return a tensor of the same shape and dtype as its input.
Activation layer or through a forward layer's activation argument."relu" instead of passing the callable.import keras
from keras import activations
x = keras.ops.array([-2.0, 0.0, 3.0])
y = activations.relu(x)
print(y)Applies the built-in ReLU function directly to a tensor-like value.
Add nonlinear behavior inside a model while retaining explicit control over the function.
Configure a Dense or other forward layer concisely.
Normalize class scores into values that form a distribution along a chosen dimension.
Define custom model logic using the same callable activation interface as built-ins.
Keras activations are callable functions such as relu, sigmoid, and softmax. They can be called directly or supplied to a layer through its activation argument.
Follow-up: How can a built-in activation be configured without passing the function object? Use its string identifier, such as "relu".
keras.activations.relu?ReLU uses configurable negative_slope, max_value, and threshold arguments. With defaults, it behaves as the element-wise maximum of zero and the input.
Follow-up: What does changing threshold do? It changes the point at which the rectifying behavior is applied, while the other parameters can control negative slope and output capping.
Softmax converts values into a probability distribution along the selected axis. Its axis argument defaults to the last axis, so multidimensional inputs can choose which dimension is normalized.
Follow-up: When would another activation be preferable? Use an activation whose output behavior matches the task, such as sigmoid for scalar bounded outputs or a linear function when no nonlinear transformation is wanted.
keras.layers.Activationkeras.layers.Densekeras.activations.celukeras.activations.elukeras.activations.gelukeras.activations.glukeras.activations.relukeras.activations.selu