Q20Machine Learning
Question
Discuss Logistic Regression cost function derivation and how gradient descent minimizes it.
Answer
A rigorous mathematical derivation of the Logistic Regression Cost Function. Violently details the failure of Mean Squared Error, the necessity of Maximum Likelihood Estimation to derive Log-Loss, and the exact Gradient Descent calculus required to minimize it.
Despite its name, Logistic Regression is an absolute foundational Binary Classification algorithm. A standard Linear Regression outputs a line . For classification, this output must be violently squashed strictly into a probability range . This is achieved by passing through the Sigmoid (Logistic) activation function:
If an engineer foolishly attempts to use the standard Linear Regression Cost Function (MSE) for Logistic Regression, the math fails catastrophically. Because the Sigmoid function is highly non-linear, inserting it into the MSE equation creates a horrific, non-convex cost function surface. It will contain thousands of "Local Minima". Gradient Descent will instantly get trapped, mathematically failing to ever find the true global minimum.
To solve this, the architecture utilizes Maximum Likelihood Estimation (MLE).
The probability of the target (where ) given the input can be mathematically written as a single Bernoulli distribution equation:
- If the true label , the right half mathematically vanishes (), leaving only the probability .
- If the true label , the left half vanishes, leaving .
To find the optimal weights , we must maximize the total likelihood of the entire dataset. Because multiplying thousands of probabilities causes a catastrophic floating-point underflow in the CPU, we apply a mathematical logarithm to convert the multiplication into addition (Log-Likelihood). We then multiply by to convert the maximization problem into a standard Minimization problem (Gradient Descent requires a loss to minimize).
This strictly derives the absolute Binary Cross-Entropy (Log-Loss) Cost Function for a single data point:
This newly derived function is mathematically guaranteed to be perfectly Convex (a single smooth bowl shape), completely eradicating the Local Minima crisis.
To minimize the cost, Gradient Descent mathematically calculates the partial derivative (gradient) of the massive cost function with respect to every single weight . Through the brutal application of the Chain Rule of calculus across the Log and Sigmoid functions, the derivative beautifully collapses into an incredibly simple mathematical equation:
The algorithm then violently updates the weights using the learning rate :
This loop executes relentlessly until the gradient mathematically reaches zero (Convergence at the absolute global minimum).