Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brief note for "Chapter 8. Gradient Descent" #125

Open
juanma-rm opened this issue Jul 24, 2023 · 0 comments
Open

Brief note for "Chapter 8. Gradient Descent" #125

juanma-rm opened this issue Jul 24, 2023 · 0 comments

Comments

@juanma-rm
Copy link

Just a minor comment extending the reason of using grad = [2 * error * x, 2 * error] in function linear_gradient ("Chapter 8. Gradient Descent" section "Using Gradient Descent to Fit Models"). Find the note included below in the python comments. I had to spend a bit of time to find out why the gradient was calculated like that, so I hope anyone finds it useful.

def linear_gradient(x: float, y: float, theta: Vector) -> Vector:
    slope, intercept = theta
    predicted = slope * x + intercept   # The prediction of the model.
    error = (predicted - y)             # error is (predicted - actual)
    squared_error = error ** 2          # We'll minimize squared error (e_sq), which depends on the current guess values for slope (m) and intercept (n).
                                        # e_sq(m,n) = error^2 (y_predicted - y_actual)^2 = (m*x + n - y_actual)^2 
    grad = [2 * error * x, 2 * error]   # using its gradient, whose partial derivatives are d(e_sq)/dm = 2*error*m and d(e_sq)/dn = 2*error ( applying derivative rule for exponential: (f^2)' = 2*f*f' )
    return grad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant