#96 · Deep Learning · Easy
⊣ Solve on deep-ml.comImplement the Hard Sigmoid activation function. Hard Sigmoid is a piecewise linear approximation of the sigmoid function that is computationally cheaper: max(0, min(1, 0.2x + 0.5)).
import numpy as np
def hard_sigmoid(x: np.ndarray) -> np.ndarray:
return np.clip(0.2 * x + 0.5, 0, 1)x <= -2.5: output is 0x >= 2.5: output is 1-2.5 < x < 2.5: output is 0.2x + 0.5 (linear interpolation)np.clip efficiently handles the clamping to [0, 1].1 / (1 + exp(-x)) since it avoids the exponential function.