commit 5e2d9287f703b656b49f596adae506e4b355f462
parent d31a740d5bbc19434b2a813799b230bec8537507
Author: Andrew <andrewlaack1@gmail.com>
Date: Thu, 18 Apr 2024 18:22:39 -0500
Added learning rate parameter and improved output formatting
Diffstat:
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/GradientDescentThirdDegreePolynomialV2.py b/GradientDescentThirdDegreePolynomialV2.py
@@ -1,10 +1,16 @@
+import sys
import random
-print("ax^3 + bx^2 + cx + d")
+RECURSION_LIMIT = 1500
+
+
+sys.setrecursionlimit(RECURSION_LIMIT)
+print("ax^3 + bx^2 + cx + d")
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
d = float(input("Enter d: "))
+learningRate = float(input("Learning Rate: "))
def calculateValue(x):
@@ -12,7 +18,7 @@ def calculateValue(x):
def printResult(x, y):
- print("x: " + str(x) + " y: " + str(y))
+ print("x: " + str(x) + "\ty: " + str(y))
def limit(x):
@@ -26,17 +32,17 @@ def limit(x):
def descend(x, depth):
- print(x)
- if depth > 100:
+ # Need - 15 because recursion includes other function calls...
+ if depth >= RECURSION_LIMIT - 15:
return x
lim = limit(x)
- print(lim)
+ printResult(x, calculateValue(x))
depth += 1
if lim > 0:
- return descend(x - .001, depth)
+ return descend(x - learningRate * lim, depth)
else:
- return descend(x + .001, depth)
+ return descend(x + learningRate * lim, depth)
currSearch = random.random() * 10