Loading problem...
In deep learning and neural network design, activation functions play a crucial role in introducing non-linearity into models. The sigmoid function is one of the classic activation functions, but its computation involves an expensive exponential operation. To address this computational overhead, practitioners often use the Piecewise Linear Sigmoid Approximation (sometimes called the "hard sigmoid")—a fast, hardware-friendly alternative.
The Piecewise Linear Sigmoid Approximation replaces the smooth S-curve of the standard sigmoid with a simple linear function in the active region and hard saturation at the boundaries. This makes it highly efficient for embedded systems, mobile devices, and scenarios where computational resources are limited.
Mathematical Definition:
The piecewise linear sigmoid is defined as:
$$ \text{piecewise_sigmoid}(x) = \begin{cases} 0 & \text{if } x \leq -2.5 \ 0.2x + 0.5 & \text{if } -2.5 < x < 2.5 \ 1 & \text{if } x \geq 2.5 \end{cases} $$
Key Characteristics:
Your Task:
Implement a function that computes the piecewise linear sigmoid approximation for a given input value. The function should correctly handle all three regions: the left saturation zone, the linear transition zone, and the right saturation zone.
x = 0.00.5The input 0.0 lies within the linear region of the piecewise sigmoid (between -2.5 and 2.5). Applying the linear formula:
piecewise_sigmoid(0.0) = 0.2 × 0.0 + 0.5 = 0.5
This represents the midpoint of the activation function, where the output is exactly halfway between 0 and 1.
x = 1.00.7The input 1.0 falls within the linear transition region (-2.5 < 1.0 < 2.5). Using the linear formula:
piecewise_sigmoid(1.0) = 0.2 × 1.0 + 0.5 = 0.2 + 0.5 = 0.7
The output of 0.7 indicates a moderately positive activation.
x = 3.01The input 3.0 exceeds the upper boundary of 2.5, placing it in the right saturation zone. When x ≥ 2.5, the output is clamped to the maximum value:
piecewise_sigmoid(3.0) = 1
This saturation behavior mimics the asymptotic approach of the standard sigmoid to 1 for large positive inputs, but with a hard cutoff.
Constraints