Skip to content

Instantly share code, notes, and snippets.

@pranavsuri
Created February 14, 2018 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranavsuri/8d2613b19014cbcf13aba16883e09836 to your computer and use it in GitHub Desktop.
Save pranavsuri/8d2613b19014cbcf13aba16883e09836 to your computer and use it in GitHub Desktop.
How to find the minima using Gradient Descent? Read blog post at http://bit.ly/2z7eWmQ.
from sympy import *
x, y = symbols('x y')
# Objective Function
f = x*y + 2*(62.5/x) + 2*(62.5/y)
# Differentiating - computing the gradient.
fpx = f.diff(x)
fpy = f.diff(y)
grad = [fpx,fpy]
# Starting point
theta0 = 20
theta1 = 20
# Algorithm parameters
alpha = 0.01
epsilon = 0.00000001
iterations = 0
maxIterations = 1000
printData = True
check = 0
while True:
# Simultaneously update unknown variables
tempTheta0 = theta0 - alpha * N(fpx.subs(x, theta0).subs(y, theta1))
tempTheta1 = theta1 - alpha * N(fpy.subs(y, theta1).subs(x, theta0))
iterations += 1
if iterations > maxIterations:
print("Too many iterations. Adjust alpha.")
printData = False
break
if abs(tempTheta0 - theta0) < epsilon and abs(tempTheta1 - theta1) < epsilon:
break
theta0 = tempTheta0
theta1 = tempTheta1
z = 62.5/(theta0*theta1)
if printData:
print("x = ", theta0, sep = " ")
print("y = ", theta1, sep = " ")
print("z = ", z, sep = " ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment