tf.keras.ops.ptp

Return the peak-to-peak (max - min) value of tensor elements over a given axis.

The peak-to-peak value is defined as the difference between the maximum and minimum values along the specified axis.

x Input tensor.
axis Axis or axes along which the peak-to-peak value is computed. The default, axis=None, will compute the peak-to-peak value over all elements in the input tensor.
keepdims If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

A tensor containing the peak-to-peak values of x over the given axis or axes.

Examples:

x = keras.ops.array([[1., 3., 2.],
                     [4., 0., 5.]])
# Peak-to-peak over all elements
keras.ops.ptp(x)
5.0
# Peak-to-peak along axis 1
keras.ops.ptp(x, axis=1)
array([2., 5.], dtype=float32)
# Peak-to-peak over multiple axes
x = keras.ops.reshape(x, (1, 2, 3))
keras.ops.ptp(x, axis=(1, 2))
array([5.], dtype=float32)
# Keep reduced dimensions
keras.ops.ptp(x, axis=2, keepdims=True)
array([[[2.],
        [5.]]], dtype=float32)