Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,112 +31,107 @@ def reset_session_state():
|
|
| 31 |
st.session_state.x_points = [st.session_state.initial_point]
|
| 32 |
st.session_state.y_points = [evaluate_function(st.session_state.math_function, st.session_state.initial_point)]
|
| 33 |
|
| 34 |
-
# Input section for function
|
| 35 |
-
st.header("Input Your Function")
|
| 36 |
-
st.markdown("Define a mathematical function (e.g., `x**2`, `np.sin(x)`, `x**3 - 3*x + 2`):")
|
| 37 |
-
function_input = st.text_input("Enter Function:", "x**2 + x", key="math_function", on_change=reset_session_state)
|
| 38 |
-
st.markdown("---")
|
| 39 |
-
|
| 40 |
-
# Gradient descent parameters
|
| 41 |
-
st.header("Set Parameters for Gradient Descent")
|
| 42 |
-
st.markdown("Configure the starting point and learning rate:")
|
| 43 |
col1, col2 = st.columns(2)
|
| 44 |
with col1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
initial_point = st.number_input(
|
| 46 |
"Initial Value of x", value=4.0, step=0.1, format="%.2f", key="initial_point", on_change=reset_session_state
|
| 47 |
)
|
| 48 |
-
with col2:
|
| 49 |
learning_rate = st.number_input(
|
| 50 |
"Learning Rate", value=0.1, step=0.01, format="%.2f", key="learning_rate", on_change=reset_session_state
|
| 51 |
)
|
| 52 |
-
st.markdown("---")
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
st.
|
| 57 |
-
st.session_state
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
st.
|
| 75 |
-
st.markdown(f"**Current x Value:** {st.session_state.x_current:.4f}")
|
| 76 |
-
st.markdown(f"**Current Function Value (f(x)):** {st.session_state.y_points[-1]:.4f}")
|
| 77 |
-
st.markdown("---")
|
| 78 |
-
|
| 79 |
-
# Create two columns: left for inputs and right for visualization
|
| 80 |
-
col1, col2 = st.columns([1, 2]) # Adjust width of the columns as needed
|
| 81 |
-
|
| 82 |
-
# Left column with function inputs and progress
|
| 83 |
-
with col1:
|
| 84 |
-
st.header("Gradient Descent Progress")
|
| 85 |
st.markdown(f"**Iteration:** {st.session_state.iter_count}")
|
| 86 |
st.markdown(f"**Current x Value:** {st.session_state.x_current:.4f}")
|
| 87 |
st.markdown(f"**Current Function Value (f(x)):** {st.session_state.y_points[-1]:.4f}")
|
| 88 |
st.markdown("---")
|
| 89 |
-
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
plot = go.Figure()
|
| 98 |
-
|
| 99 |
-
# Add function plot
|
| 100 |
-
plot.add_trace(
|
| 101 |
-
go.Scatter(x=x_vals, y=y_vals, mode="lines", line=dict(color="green", width=3), name="Function Curve")
|
| 102 |
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
)
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
| 31 |
st.session_state.x_points = [st.session_state.initial_point]
|
| 32 |
st.session_state.y_points = [evaluate_function(st.session_state.math_function, st.session_state.initial_point)]
|
| 33 |
|
| 34 |
+
# Input section for function and parameters (Left Side)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
col1, col2 = st.columns(2)
|
| 36 |
with col1:
|
| 37 |
+
st.header("Input Your Function")
|
| 38 |
+
st.markdown("Define a mathematical function (e.g., `x**2`, `np.sin(x)`, `x**3 - 3*x + 2`):")
|
| 39 |
+
function_input = st.text_input("Enter Function:", "x**2 + x", key="math_function", on_change=reset_session_state)
|
| 40 |
+
st.markdown("---")
|
| 41 |
+
|
| 42 |
+
st.header("Set Parameters for Gradient Descent")
|
| 43 |
+
st.markdown("Configure the starting point and learning rate:")
|
| 44 |
initial_point = st.number_input(
|
| 45 |
"Initial Value of x", value=4.0, step=0.1, format="%.2f", key="initial_point", on_change=reset_session_state
|
| 46 |
)
|
|
|
|
| 47 |
learning_rate = st.number_input(
|
| 48 |
"Learning Rate", value=0.1, step=0.01, format="%.2f", key="learning_rate", on_change=reset_session_state
|
| 49 |
)
|
| 50 |
+
st.markdown("---")
|
| 51 |
|
| 52 |
+
# Gradient Descent Updates (Right Side)
|
| 53 |
+
with col2:
|
| 54 |
+
st.header("Gradient Descent Updates")
|
| 55 |
+
if "x_current" not in st.session_state:
|
| 56 |
+
st.session_state.x_current = initial_point
|
| 57 |
+
st.session_state.iter_count = 0
|
| 58 |
+
st.session_state.x_points = [initial_point]
|
| 59 |
+
st.session_state.y_points = [evaluate_function(function_input, initial_point)]
|
| 60 |
+
|
| 61 |
+
if st.button("Perform Gradient Descent Step", type="primary"):
|
| 62 |
+
try:
|
| 63 |
+
gradient = compute_derivative(function_input, st.session_state.x_current)
|
| 64 |
+
st.session_state.x_current -= learning_rate * gradient
|
| 65 |
+
st.session_state.iter_count += 1
|
| 66 |
+
st.session_state.x_points.append(st.session_state.x_current)
|
| 67 |
+
st.session_state.y_points.append(evaluate_function(function_input, st.session_state.x_current))
|
| 68 |
+
except Exception as e:
|
| 69 |
+
st.error(f"Error: {str(e)}")
|
| 70 |
+
|
| 71 |
+
# Gradient Descent Progress Section with Different Style
|
| 72 |
+
st.subheader("Gradient Descent Progress")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
st.markdown(f"**Iteration:** {st.session_state.iter_count}")
|
| 74 |
st.markdown(f"**Current x Value:** {st.session_state.x_current:.4f}")
|
| 75 |
st.markdown(f"**Current Function Value (f(x)):** {st.session_state.y_points[-1]:.4f}")
|
| 76 |
st.markdown("---")
|
| 77 |
+
|
| 78 |
+
# Styling the updates section
|
| 79 |
+
st.markdown(
|
| 80 |
+
f'<div style="background-color:#f0f0f0; padding: 10px; border-radius: 5px;">'
|
| 81 |
+
f'<strong>Iteration: </strong>{st.session_state.iter_count} <br>'
|
| 82 |
+
f'<strong>Current x Value: </strong>{st.session_state.x_current:.4f} <br>'
|
| 83 |
+
f'<strong>Current f(x): </strong>{st.session_state.y_points[-1]:.4f}</div>',
|
| 84 |
+
unsafe_allow_html=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# Generate plot data
|
| 88 |
+
x_vals = np.linspace(-10, 10, 400)
|
| 89 |
+
y_vals = [evaluate_function(function_input, x) for x in x_vals]
|
| 90 |
+
|
| 91 |
+
# Create the plot
|
| 92 |
+
plot = go.Figure()
|
| 93 |
+
|
| 94 |
+
# Add function plot
|
| 95 |
+
plot.add_trace(
|
| 96 |
+
go.Scatter(x=x_vals, y=y_vals, mode="lines", line=dict(color="green", width=3), name="Function Curve")
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Add gradient descent points
|
| 100 |
+
plot.add_trace(
|
| 101 |
+
go.Scatter(
|
| 102 |
+
x=st.session_state.x_points,
|
| 103 |
+
y=st.session_state.y_points,
|
| 104 |
+
mode="markers",
|
| 105 |
+
marker=dict(color="red", size=10, symbol="diamond"),
|
| 106 |
+
name="Descent Steps",
|
| 107 |
)
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Add tangent line
|
| 111 |
+
current_x = st.session_state.x_current
|
| 112 |
+
current_y = evaluate_function(function_input, current_x)
|
| 113 |
+
slope = compute_derivative(function_input, current_x)
|
| 114 |
+
tangent_x = np.linspace(current_x - 2, current_x + 2, 100)
|
| 115 |
+
tangent_y = calculate_tangent(function_input, current_x, tangent_x)
|
| 116 |
+
|
| 117 |
+
plot.add_trace(
|
| 118 |
+
go.Scatter(
|
| 119 |
+
x=tangent_x,
|
| 120 |
+
y=tangent_y,
|
| 121 |
+
mode="lines",
|
| 122 |
+
line=dict(color="blue", width=2, dash="dash"),
|
| 123 |
+
name="Tangent Line",
|
| 124 |
)
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Update plot layout
|
| 128 |
+
plot.update_layout(
|
| 129 |
+
title="Interactive Gradient Descent with Tangent Visualization",
|
| 130 |
+
xaxis_title="x",
|
| 131 |
+
yaxis_title="f(x)",
|
| 132 |
+
template="plotly_dark",
|
| 133 |
+
legend=dict(bgcolor="rgba(255,255,255,0.5)", bordercolor="gray", borderwidth=1),
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Display the plot in the left side
|
| 137 |
+
st.plotly_chart(plot)
|