GradientDescentCode.md (1263B)
1 # Gradient Descent Implementation 2 3 This approach implements a [LearningRate](LearningRate.md) parameter to narrow in upon a local minimum of the given third degree polynomial. 4 5 ## Code 6 ```python 7 import sys 8 import random 9 10 RECURSION_LIMIT = 1500 11 12 sys.setrecursionlimit(RECURSION_LIMIT) 13 print("ax^3 + bx^2 + cx + d") 14 a = float(input("Enter a: ")) 15 b = float(input("Enter b: ")) 16 c = float(input("Enter c: ")) 17 d = float(input("Enter d: ")) 18 learningRate = float(input("Learning Rate: ")) 19 20 def calculateValue(x): 21 return x**3 * a + x**2 * b + x * c + d 22 23 def printResult(x, y): 24 print("x: " + str(x) + "\ty: " + str(y)) 25 26 def limit(x): 27 rightX = x + .00000001 28 leftX = x - .00000001 29 30 rightY = calculateValue(rightX) 31 leftY = calculateValue(leftX) 32 33 return ((rightY - leftY) / .00000002) 34 35 def descend(x, depth): 36 # Need - 15 because recursion includes other function calls... 37 if depth >= RECURSION_LIMIT - 15: 38 return x 39 lim = limit(x) 40 printResult(x, calculateValue(x)) 41 depth += 1 42 43 if lim > 0: 44 return descend(x - learningRate * lim, depth) 45 else: 46 return descend(x + learningRate * lim, depth) 47 48 currSearch = random.random() * 10 49 xVal = descend(currSearch, 0) 50 51 printResult(xVal, calculateValue(xVal)) 52 ```