simultaneously update theta0 and theta1 to calculate gradient descent in python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Gradient descent is a fundamental optimization technique widely used in machine learning to find the minimum of a function. It is particularly useful in linear regression for adjusting the parameters to minimize the cost function. This article focuses on the process of simultaneously updating the parameters and in gradient descent. We will explore how to implement this in Python and provide an in-depth understanding of the method's mechanics.
Gradient Descent Basics
Gradient descent works by iteratively updating parameters in the opposite direction of the gradient of the cost function. The cost function, in the case of linear regression, is typically the Mean Squared Error (MSE). The parameters, and , represent the intercept and the slope of the regression line, respectively.
For a hypothesis function , the cost function is expressed as:
where is the number of training examples, is the input, and is the actual output.
Partial Derivatives
To update the parameters, we need the partial derivatives of with respect to and :
The gradient descent update rules are as follows:
where is the learning rate.
Why Simultaneous Update Matters
To ensure the correctness of the gradient descent algorithm, the updates to and must be simultaneous. This means you compute both partial derivatives using the current values of and before changing either one. If you update first and then use the new to compute the gradient for , the result will be incorrect because the gradient for should have been evaluated at the original parameter values.
Python Implementation
Below is a Python implementation showcasing how to simultaneously update and in gradient descent.
The key detail is on the lines computing grad0 and grad1. Both gradients are calculated from the same (unmodified) theta0 and theta1. Only after both gradients are ready do we apply the updates. This is the simultaneous update pattern.
Incorrect (Non-Simultaneous) Update
For contrast, here is the wrong approach where is updated before computing the gradient for :
This bug is subtle because gradient descent may still converge, but it will follow a different (suboptimal) path and may converge to the wrong minimum or take many more iterations.
Practical Considerations
- Initialization: and are initialized to zeros. Random initialization also works but zero is standard for linear regression.
- Cost History: Tracking the cost over iterations helps determine whether the algorithm is converging. A decreasing cost curve is a good sign.
- Learning Rate: A too-small leads to slow convergence. A too-large causes the cost to oscillate or diverge. A common debugging technique is to plot cost vs. iteration and adjust until you see smooth, steady decrease.
- Stopping Criteria: You can stop when the change in cost between iterations drops below a threshold (for example, ), or after a fixed number of iterations.
- Vectorized Form: In practice, both parameters are stored in a single vector and updated in one step: . NumPy makes this efficient and naturally simultaneous since the gradient vector is computed before any assignment.
Summary
Simultaneous update of and is not just a stylistic choice. It is a correctness requirement of the gradient descent algorithm. The gradients must be evaluated at the same point in parameter space before any parameter is modified. In Python, this is straightforward: compute all gradients first, then apply all updates. Using NumPy's vectorized operations makes this pattern both natural and efficient.
Related reading
- Singleton/Synchronization in Clustered environment
- Sklearn Categorical Imputer?
- Sklearn Chi2 For Feature Selection
- Sklearn cross_val_score with multi input KerasClassifier
- Singletons vs. Application Context in Android?
- Skip lists, are they really performing as good as Pugh paper claim?
- Single quotes vs. double quotes in Python
- Skip first entry in for loop in python?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.