Skip to content

Instantly share code, notes, and snippets.

@souravs17031999
Created June 11, 2019 12:20
Show Gist options
  • Save souravs17031999/0097b21d430f7ea75223f0912733041f to your computer and use it in GitHub Desktop.
Save souravs17031999/0097b21d430f7ea75223f0912733041f to your computer and use it in GitHub Desktop.
start_n = 5 # setting the initial point
lr = 0.001 # setting the learning rate
precision = 0.000001 #setting the initial precision
dr = lambda x: 4 * x**3 - 9 * x**2 # set the gradient of function required
n = 1000000 #no of iterations
next_n = start_n
iter = 0 # set initial count to be 0
for i in range(n):
current_n = next_n
next_n = current_n - lr*dr(current_n) # moving in the negative of direction of gradient calculated
print(next_n)
iter += 1 # incrementing initial count
if(abs(current_n - next_n) <= precision): # stop when required precision reached
break
print(f"minimum {next_n}, total iterations: {iter}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment