Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,55 +82,55 @@ with right_col:
|
|
| 82 |
st.markdown(f"**Current Function Value (f(x)):** {st.session_state.y_points[-1]:.4f}")
|
| 83 |
st.markdown("---")
|
| 84 |
|
| 85 |
-
# Generate plot data
|
| 86 |
-
x_vals = np.linspace(-10, 10, 400)
|
| 87 |
-
y_vals = [evaluate_function(function_input, x) for x in x_vals]
|
| 88 |
-
|
| 89 |
-
# Create the plot
|
| 90 |
-
plot = go.Figure()
|
| 91 |
-
|
| 92 |
-
# Add function plot
|
| 93 |
-
plot.add_trace(
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
# Add gradient descent points
|
| 98 |
-
plot.add_trace(
|
| 99 |
-
go.Scatter(
|
| 100 |
-
x=st.session_state.x_points,
|
| 101 |
-
y=st.session_state.y_points,
|
| 102 |
-
mode="markers",
|
| 103 |
-
marker=dict(color="red", size=10, symbol="diamond"),
|
| 104 |
-
name="Descent Steps",
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
go.Scatter(
|
| 117 |
-
x=tangent_x,
|
| 118 |
-
y=tangent_y,
|
| 119 |
-
mode="lines",
|
| 120 |
-
line=dict(color="blue", width=2, dash="dash"),
|
| 121 |
-
name="Tangent Line",
|
| 122 |
)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
st.markdown(f"**Current Function Value (f(x)):** {st.session_state.y_points[-1]:.4f}")
|
| 83 |
st.markdown("---")
|
| 84 |
|
| 85 |
+
# Generate plot data
|
| 86 |
+
x_vals = np.linspace(-10, 10, 400)
|
| 87 |
+
y_vals = [evaluate_function(function_input, x) for x in x_vals]
|
| 88 |
+
|
| 89 |
+
# Create the plot
|
| 90 |
+
plot = go.Figure()
|
| 91 |
+
|
| 92 |
+
# Add function plot
|
| 93 |
+
plot.add_trace(
|
| 94 |
+
go.Scatter(x=x_vals, y=y_vals, mode="lines", line=dict(color="green", width=3), name="Function Curve")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
# Add gradient descent points
|
| 98 |
+
plot.add_trace(
|
| 99 |
+
go.Scatter(
|
| 100 |
+
x=st.session_state.x_points,
|
| 101 |
+
y=st.session_state.y_points,
|
| 102 |
+
mode="markers",
|
| 103 |
+
marker=dict(color="red", size=10, symbol="diamond"),
|
| 104 |
+
name="Descent Steps",
|
| 105 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
+
|
| 108 |
+
# Add tangent line
|
| 109 |
+
current_x = st.session_state.x_current
|
| 110 |
+
current_y = evaluate_function(function_input, current_x)
|
| 111 |
+
slope = compute_derivative(function_input, current_x)
|
| 112 |
+
tangent_x = np.linspace(current_x - 2, current_x + 2, 100)
|
| 113 |
+
tangent_y = calculate_tangent(function_input, current_x, tangent_x)
|
| 114 |
+
|
| 115 |
+
plot.add_trace(
|
| 116 |
+
go.Scatter(
|
| 117 |
+
x=tangent_x,
|
| 118 |
+
y=tangent_y,
|
| 119 |
+
mode="lines",
|
| 120 |
+
line=dict(color="blue", width=2, dash="dash"),
|
| 121 |
+
name="Tangent Line",
|
| 122 |
+
)
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Update plot layout
|
| 126 |
+
plot.update_layout(
|
| 127 |
+
title="Interactive Gradient Descent with Tangent Visualization",
|
| 128 |
+
xaxis_title="x",
|
| 129 |
+
yaxis_title="f(x)",
|
| 130 |
+
template="plotly_dark",
|
| 131 |
+
legend=dict(bgcolor="rgba(255,255,255,0.5)", bordercolor="gray", borderwidth=1),
|
| 132 |
+
height=200, # Reduce the graph height for better fitting
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Display the plot in the right side (with more space now)
|
| 136 |
+
st.plotly_chart(plot, use_container_width=True)
|