Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import plotly.graph_objs as go
|
| 4 |
+
import sympy as sp
|
| 5 |
+
|
| 6 |
+
# Streamlit Page Configuration
|
| 7 |
+
st.set_page_config(page_title="Gradient Descent Visualizer", layout="wide")
|
| 8 |
+
|
| 9 |
+
# Sidebar Inputs
|
| 10 |
+
st.sidebar.header("Gradient Descent Settings")
|
| 11 |
+
func_input = st.sidebar.text_input("Enter a function (use 'x'):", "x**2")
|
| 12 |
+
learning_rate = st.sidebar.number_input("Learning Rate", min_value=0.001, max_value=1.0, value=0.1, step=0.01)
|
| 13 |
+
initial_x = st.sidebar.number_input("Initial X", min_value=-10.0, max_value=10.0, value=5.0, step=0.1)
|
| 14 |
+
|
| 15 |
+
# Reset Session State When Function Changes
|
| 16 |
+
if "previous_func" not in st.session_state or st.session_state.previous_func != func_input:
|
| 17 |
+
st.session_state.current_x = initial_x
|
| 18 |
+
st.session_state.iteration = 0
|
| 19 |
+
st.session_state.path = [(initial_x, 0)]
|
| 20 |
+
st.session_state.previous_func = func_input
|
| 21 |
+
|
| 22 |
+
# Symbolic Computation
|
| 23 |
+
x = sp.symbols('x')
|
| 24 |
+
try:
|
| 25 |
+
func = sp.sympify(func_input)
|
| 26 |
+
derivative = sp.diff(func, x)
|
| 27 |
+
func_np = sp.lambdify(x, func, 'numpy')
|
| 28 |
+
derivative_np = sp.lambdify(x, derivative, 'numpy')
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Invalid function: {e}")
|
| 31 |
+
st.stop()
|
| 32 |
+
|
| 33 |
+
# Gradient Descent Step
|
| 34 |
+
def step_gradient_descent(current_x, lr):
|
| 35 |
+
grad = derivative_np(current_x)
|
| 36 |
+
next_x = current_x - lr * grad
|
| 37 |
+
return next_x, grad
|
| 38 |
+
|
| 39 |
+
# Perform Next Iteration
|
| 40 |
+
if st.sidebar.button("Next Iteration"):
|
| 41 |
+
next_x, _ = step_gradient_descent(st.session_state.current_x, learning_rate)
|
| 42 |
+
st.session_state.path.append((st.session_state.current_x, func_np(st.session_state.current_x)))
|
| 43 |
+
st.session_state.current_x = next_x
|
| 44 |
+
st.session_state.iteration += 1
|
| 45 |
+
|
| 46 |
+
# Calculate Actual Minima
|
| 47 |
+
critical_points = sp.solve(derivative, x)
|
| 48 |
+
actual_minima = [p.evalf() for p in critical_points if derivative_np(p) == 0 and sp.diff(derivative, x).evalf(subs={x: p}) > 0]
|
| 49 |
+
|
| 50 |
+
# Generate Graph Data
|
| 51 |
+
x_vals = np.linspace(-15, 15, 1000)
|
| 52 |
+
y_vals = func_np(x_vals)
|
| 53 |
+
|
| 54 |
+
# Plotly Visualization
|
| 55 |
+
fig = go.Figure()
|
| 56 |
+
|
| 57 |
+
# Function Plot
|
| 58 |
+
fig.add_trace(go.Scatter(
|
| 59 |
+
x=x_vals, y=y_vals, mode='lines',
|
| 60 |
+
line=dict(color='blue', width=2),
|
| 61 |
+
hoverinfo='none'
|
| 62 |
+
))
|
| 63 |
+
|
| 64 |
+
# Gradient Descent Path
|
| 65 |
+
path = st.session_state.path
|
| 66 |
+
x_path, y_path = zip(*[(pt[0], func_np(pt[0])) for pt in path])
|
| 67 |
+
fig.add_trace(go.Scatter(
|
| 68 |
+
x=x_path, y=y_path, mode='markers+lines',
|
| 69 |
+
marker=dict(color='red', size=8),
|
| 70 |
+
line=dict(color='red', width=2),
|
| 71 |
+
hoverinfo='none'
|
| 72 |
+
))
|
| 73 |
+
|
| 74 |
+
# Highlight Current Point
|
| 75 |
+
fig.add_trace(go.Scatter(
|
| 76 |
+
x=[st.session_state.current_x], y=[func_np(st.session_state.current_x)],
|
| 77 |
+
mode='markers', marker=dict(color='orange', size=12),
|
| 78 |
+
name="Current Point", hoverinfo='none'
|
| 79 |
+
))
|
| 80 |
+
|
| 81 |
+
# Highlight Actual Minima
|
| 82 |
+
if actual_minima:
|
| 83 |
+
minima_x = [float(p) for p in actual_minima]
|
| 84 |
+
minima_y = [func_np(p) for p in minima_x]
|
| 85 |
+
fig.add_trace(go.Scatter(
|
| 86 |
+
x=minima_x, y=minima_y,
|
| 87 |
+
mode='markers', marker=dict(color='green', size=14, symbol='star'),
|
| 88 |
+
name="Actual Minima", hoverinfo='text',
|
| 89 |
+
text=[f"x = {x_val:.4f}, f(x) = {y_val:.4f}" for x_val, y_val in zip(minima_x, minima_y)]
|
| 90 |
+
))
|
| 91 |
+
|
| 92 |
+
# Add Cross-Axes (X and Y lines)
|
| 93 |
+
fig.add_trace(go.Scatter(
|
| 94 |
+
x=[-15, 15], y=[0, 0], mode='lines',
|
| 95 |
+
line=dict(color='black', width=1, dash='dash'),
|
| 96 |
+
hoverinfo='none'
|
| 97 |
+
))
|
| 98 |
+
fig.add_trace(go.Scatter(
|
| 99 |
+
x=[0, 0], y=[-15, 15], mode='lines',
|
| 100 |
+
line=dict(color='black', width=1, dash='dash'),
|
| 101 |
+
hoverinfo='none'
|
| 102 |
+
))
|
| 103 |
+
|
| 104 |
+
# Layout Configuration
|
| 105 |
+
fig.update_layout(
|
| 106 |
+
title="Gradient Descent Visualization",
|
| 107 |
+
xaxis=dict(
|
| 108 |
+
title="X",
|
| 109 |
+
zeroline=True, zerolinewidth=1, zerolinecolor='black',
|
| 110 |
+
tickvals=np.arange(-15, 16, 5),
|
| 111 |
+
range=[-15, 15]
|
| 112 |
+
),
|
| 113 |
+
yaxis=dict(
|
| 114 |
+
title="f(X)",
|
| 115 |
+
zeroline=True, zerolinewidth=1, zerolinecolor='black',
|
| 116 |
+
tickvals=np.arange(-15, 16, 5),
|
| 117 |
+
range=[-15, 15]
|
| 118 |
+
),
|
| 119 |
+
showlegend=False,
|
| 120 |
+
hovermode="closest",
|
| 121 |
+
dragmode="pan", # Corrected line: removed extra space
|
| 122 |
+
autosize=True,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Fullscreen and Export Options
|
| 126 |
+
st.markdown("### Gradient Descent Visualization")
|
| 127 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 128 |
+
|
| 129 |
+
# Display Current Point
|
| 130 |
+
st.write(f"**Current Point (x):** {st.session_state.current_x:.4f}")
|
| 131 |
+
|
| 132 |
+
# Display Iteration History below the graph
|
| 133 |
+
st.write("### Iteration History:")
|
| 134 |
+
for i, (x_val, _) in enumerate(st.session_state.path):
|
| 135 |
+
st.write(f"Iteration {i+1}: x = {x_val:.4f}")
|