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

b/m gradient calculation #7

Open
gtrevi opened this issue Apr 3, 2017 · 3 comments
Open

b/m gradient calculation #7

gtrevi opened this issue Apr 3, 2017 · 3 comments

Comments

@gtrevi
Copy link

gtrevi commented Apr 3, 2017

Hi,
the (2/N) factor could be pulled out of the for loop, since it's out of the sigma in the p-derivative equation, correct?
So something like this:

def step_gradient(b_current, m_current, points, learningRate): b_gradient = 0 m_gradient = 0 N = float(len(points)) for i in range(0, len(points)): x = points[i, 0] y = points[i, 1] b_gradient += -(y - ((m_current * x) + b_current)) # (2/N) outta here m_gradient += -x * (y - ((m_current * x) + b_current)) # (2/N) outta here new_b = b_current - (learningRate * ((2/N)*b_gradient)) # (2/N) to be used here new_m = m_current - (learningRate * ((2/N)*m_gradient)) # (2/N) to be used here return [new_b, new_m]

@kavinaravind
Copy link

Yes you're correct. The (2/N) should be pulled out of the for loop to match the equation. But since this value (2/N) is a constant, the end value will be the same. So the value of your code will be the same as Siraj's code.

@elmennani89
Copy link

yes, I think the only benifit of pulling out (2/N) is making the algo a little bit faster. making devision and multiplication 1 time instead of N times

@gtrevi
Copy link
Author

gtrevi commented Jul 30, 2017

yep, speed was my main point, but also, besides the 2/N in/out the "for" would give the same value, the code would be clearer in my opinion.

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

3 participants