Add a nonlinear activation after an affine transformation.
Hidden layers in a neural network
TensorFlow · Function
Computes the rectified-linear activation, replacing negative tensor elements with zero and leaving nonnegative elements unchanged. The result has the same dtype as the input.
The function applies the rectified-linear transformation element by element: negative values become zero, while nonnegative values remain unchanged. It returns a tensor with the same data type as the input and optionally accepts an operation name.
tf.nn.relu(
features: Annotated[Any, TV_Relu_T], name=None
) -> Annotated[Any, TV_Relu_T]| Name | Type | Description |
|---|---|---|
features | Tensor | A Tensor containing the values to which the rectified-linear operation is applied; supported types are listed by the documentation. |
name | string | Optional name for the operation. |
A Tensor with the same type as features.
name.import tensorflow as tf
features = tf.constant([-2.0, 0.0, 3.0])
activated = tf.nn.relu(features)
print(activated.numpy()) # [0. 0. 3.]Creates a floating-point tensor and replaces its negative value with zero while preserving zero and the positive value.
Hidden layers in a neural network
Tensor preprocessing
Activation inspection
tf.nn.relu compute?tf.nn.relu computes max(features, 0) element by element. Consequently, negative entries produce zero, while zero and positive entries pass through unchanged.
Follow-up: How does it differ from tf.nn.relu6? relu6 additionally caps the result at 6, whereas relu has no upper bound.
tf.nn.relu?The features argument must be a Tensor with one of the dtypes listed in the documentation, including common floating-point and integer types. The optional name argument supplies a name for the operation.
Follow-up: What should be checked when a call fails due to dtype? Compare the tensor's dtype with the documented supported-type list and cast it if appropriate.
tf.nn.relu return?The function returns a Tensor whose type matches features. It applies the transformation independently to tensor elements, so the output keeps the input shape while changing values according to the rectifier.
Follow-up: Why can this be used as a neural-network activation? It introduces a nonlinear mapping that preserves positive responses and suppresses negative ones.
tf.nn.relu6tf.nn.leaky_relutf.nn