Add Gradient Descent Gradio app
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def f(x, func_name="Quadratic"):
|
| 6 |
+
if func_name == "Quadratic":
|
| 7 |
+
return (x - 2)**2 + 1
|
| 8 |
+
elif func_name == "Quartic":
|
| 9 |
+
return x**4 - 3*(x**2) + 2
|
| 10 |
+
|
| 11 |
+
def grad_f(x, func_name="Quadratic"):
|
| 12 |
+
if func_name == "Quadratic":
|
| 13 |
+
return 2*(x - 2)
|
| 14 |
+
elif func_name == "Quartic":
|
| 15 |
+
return 4*(x**3) - 6*x
|
| 16 |
+
|
| 17 |
+
def run_gd(func_name, x0, lr, steps, x_min, x_max):
|
| 18 |
+
xs = [float(x0)]
|
| 19 |
+
ys = [float(f(x0, func_name))]
|
| 20 |
+
x = float(x0)
|
| 21 |
+
|
| 22 |
+
for _ in range(int(steps)):
|
| 23 |
+
g = float(grad_f(x, func_name))
|
| 24 |
+
x = x - float(lr) * g
|
| 25 |
+
xs.append(x)
|
| 26 |
+
ys.append(float(f(x, func_name)))
|
| 27 |
+
|
| 28 |
+
grid = np.linspace(float(x_min), float(x_max), 400)
|
| 29 |
+
vals = f(grid, func_name)
|
| 30 |
+
|
| 31 |
+
fig1 = plt.figure()
|
| 32 |
+
plt.plot(grid, vals)
|
| 33 |
+
plt.scatter(xs, ys, s=30)
|
| 34 |
+
plt.plot(xs, ys, linestyle="--")
|
| 35 |
+
plt.title(f"Gradient Descent Path on {func_name}")
|
| 36 |
+
plt.xlabel("x")
|
| 37 |
+
plt.ylabel("f(x)")
|
| 38 |
+
plt.grid(True)
|
| 39 |
+
|
| 40 |
+
fig2 = plt.figure()
|
| 41 |
+
plt.plot(range(len(ys)), ys)
|
| 42 |
+
plt.title("Objective Value Over Iterations")
|
| 43 |
+
plt.xlabel("iteration")
|
| 44 |
+
plt.ylabel("f(x)")
|
| 45 |
+
plt.grid(True)
|
| 46 |
+
|
| 47 |
+
final = f"Final x = {xs[-1]:.6f}, f(x) = {ys[-1]:.6f}"
|
| 48 |
+
return fig1, fig2, final
|
| 49 |
+
|
| 50 |
+
demo = gr.Interface(
|
| 51 |
+
fn=run_gd,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Dropdown(["Quadratic", "Quartic"], value="Quadratic", label="Function"),
|
| 54 |
+
gr.Slider(-10, 10, value=8, step=0.1, label="Initial x0"),
|
| 55 |
+
gr.Slider(0.001, 1.0, value=0.1, step=0.001, label="Learning rate (lr)"),
|
| 56 |
+
gr.Slider(1, 200, value=30, step=1, label="Steps"),
|
| 57 |
+
gr.Slider(-15, 0, value=-5, step=0.5, label="Plot x_min"),
|
| 58 |
+
gr.Slider(0, 15, value=10, step=0.5, label="Plot x_max"),
|
| 59 |
+
],
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.Plot(label="Function + GD path"),
|
| 62 |
+
gr.Plot(label="Loss curve"),
|
| 63 |
+
gr.Textbox(label="Result"),
|
| 64 |
+
],
|
| 65 |
+
title="Gradient Descent Visualizer (from scratch)",
|
| 66 |
+
description="Adjust learning rate, starting point, and steps to see how gradient descent moves. Update rule is implemented manually."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
demo.launch()
|