A5EasyPart A · Core Kernels & Activation Functions
Softplus & LeakyReLU with Overflow Guards
Softplus with threshold clipping against overflow, alongside LeakyReLU negative slope leak.
⏱️ Time Complexity:
O(N)💾 Space Complexity:
O(N)💡
Core Mental Anchor / Mnemonic
log1p for small inputs; threshold cutoff to identity for large values
📐 Mathematical Derivation & Core Formula
### Threshold Truncation Mechanism
is the smooth surrogate of ReLU.
For , . Evaluating directly overflows FP32 at .
We clip with threshold : values above 20 pass through as identity, while smaller inputs evaluate via .
is the smooth surrogate of ReLU.
For , . Evaluating directly overflows FP32 at .
We clip with threshold : values above 20 pass through as identity, while smaller inputs evaluate via .
🔄 Tensor Dimensions & Shape Flow
(...) -> mask partition -> identity for large values / log1p(exp) for small values -> (...)
🛡️ Industrial Numerical Stability & Pitfalls
- Use np.log1p instead of np.log(1 + ...)
- Switch to identity mapping for beta * x > 20
💻 Industrial Code Implementation
import numpy as np
def stable_softplus(x: np.ndarray, beta: float = 1.0, threshold: float = 20.0) -> np.ndarray:
"""Softplus with numerical threshold truncation to prevent overflow."""
bx = beta * x
out = np.empty_like(x, dtype=np.float64)
linear_mask = bx > threshold
out[linear_mask] = x[linear_mask]
out[~linear_mask] = (1.0 / beta) * np.log1p(np.exp(bx[~linear_mask]))
return out
def leaky_relu(x: np.ndarray, negative_slope: float = 0.01) -> np.ndarray:
"""LeakyReLU activation."""
return np.where(x >= 0, x, x * negative_slope)
🧪 Runnable Assertions & Validation
Copy and run directly in Python / Jupyter to verify correctness:
import numpy as np
x = np.array([-100.0, 0.0, 50.0, 1000.0])
sp = stable_softplus(x)
assert not np.isinf(sp[-1]), "1000 should not overflow"
assert np.isclose(sp[-1], 1000.0), "Large numbers should follow identity"
assert np.isclose(sp[1], np.log(2.0)), "At 0, value should be ln(2)"
print("✓ Softplus & LeakyReLU assertion passed")🎯 Core Architecture Follow-up Q&A
Q1:What is the first derivative of Softplus?
, exactly the standard Sigmoid function.