Compute the derivatives of common activation functions (Sigmoid, Tanh, ReLU, and Leaky ReLU) at a given input value.
import math
def activation_derivatives(x: float) -> dict:
# Sigmoid
sig = 1.0 / (1.0 + math.exp(-x))
d_sigmoid = sig * (1.0 - sig)
# Tanh
t = math.tanh(x)
d_tanh = 1.0 - t ** 2
# ReLU
d_relu = 1.0 if x > 0 else 0.0
# Leaky ReLU (alpha = 0.01)
alpha = 0.01
d_leaky_relu = 1.0 if x > 0 else alpha
return {
"sigmoid": round(d_sigmoid, 4),
"tanh": round(d_tanh, 4),
"relu": round(d_relu, 4),
"leaky_relu": round(d_leaky_relu, 4),
}