Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,24 +4,30 @@ import plotly.graph_objects as go
|
|
| 4 |
|
| 5 |
# Title of the app
|
| 6 |
st.set_page_config(page_title="Interactive Gradient Descent Visualizer", layout="wide")
|
| 7 |
-
st.markdown("<h1 style='text-align: center;'
|
| 8 |
|
| 9 |
-
# Custom CSS for
|
| 10 |
st.markdown("""
|
| 11 |
<style>
|
| 12 |
body {
|
| 13 |
-
background
|
| 14 |
-
color:
|
| 15 |
}
|
| 16 |
.stButton>button {
|
| 17 |
-
background
|
| 18 |
-
color:
|
| 19 |
-
border
|
| 20 |
-
|
|
|
|
| 21 |
font-size: 16px;
|
|
|
|
| 22 |
}
|
| 23 |
.stButton>button:hover {
|
| 24 |
-
background
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
</style>
|
| 27 |
""", unsafe_allow_html=True)
|
|
@@ -66,10 +72,10 @@ if "current_index" not in st.session_state:
|
|
| 66 |
if "learning_rate" not in st.session_state:
|
| 67 |
st.session_state.learning_rate = 0.1
|
| 68 |
|
| 69 |
-
# Create two-column grid layout
|
| 70 |
-
left_col, right_col = st.columns([1, 2])
|
| 71 |
|
| 72 |
-
# Left side content
|
| 73 |
with left_col:
|
| 74 |
st.markdown("### ๐งฎ Input Your Function")
|
| 75 |
function_input = st.text_input(
|
|
@@ -97,7 +103,7 @@ with left_col:
|
|
| 97 |
|
| 98 |
st.markdown("### ๐๏ธ Controls")
|
| 99 |
|
| 100 |
-
if st.button("๐ Run Descent Step"
|
| 101 |
try:
|
| 102 |
gradient = compute_derivative(function_input, st.session_state.x_current)
|
| 103 |
st.session_state.x_current -= st.session_state.learning_rate * gradient
|
|
@@ -111,42 +117,42 @@ with left_col:
|
|
| 111 |
if st.button("๐ Reset"):
|
| 112 |
reset_session_state()
|
| 113 |
|
| 114 |
-
# Right side content
|
| 115 |
with right_col:
|
| 116 |
st.markdown("### ๐ Gradient Descent Visualization")
|
| 117 |
-
|
| 118 |
-
#
|
| 119 |
-
col1, col2, col3 = st.columns(
|
| 120 |
with col1:
|
| 121 |
-
if st.button("
|
| 122 |
st.session_state.current_index -= 1
|
| 123 |
with col2:
|
| 124 |
-
st.markdown(f"
|
| 125 |
with col3:
|
| 126 |
-
if st.button("
|
| 127 |
st.session_state.current_index += 1
|
| 128 |
-
|
| 129 |
try:
|
| 130 |
selected_x, selected_y = st.session_state.history[st.session_state.current_index]
|
| 131 |
st.markdown(f"๐งพ **x Value:** `{selected_x:.4f}`")
|
| 132 |
st.markdown(f"๐ **f(x):** `{selected_y:.4f}`")
|
| 133 |
except IndexError:
|
| 134 |
st.warning("No iteration data available. Please run a descent step first.")
|
| 135 |
-
|
| 136 |
# Prepare data for visualization
|
| 137 |
-
x_range = np.linspace(-10, 10, 500)
|
| 138 |
y_range = [evaluate_function(function_input, x) for x in x_range]
|
| 139 |
-
|
| 140 |
-
# Plot function curve
|
| 141 |
fig = go.Figure()
|
| 142 |
fig.add_trace(go.Scatter(
|
| 143 |
x=x_range,
|
| 144 |
y=y_range,
|
| 145 |
mode='lines',
|
| 146 |
name='Function',
|
| 147 |
-
line=dict(color='
|
| 148 |
))
|
| 149 |
-
|
| 150 |
# Add current point
|
| 151 |
x_current, y_current = st.session_state.history[st.session_state.current_index]
|
| 152 |
fig.add_trace(go.Scatter(
|
|
@@ -154,9 +160,9 @@ with right_col:
|
|
| 154 |
y=[y_current],
|
| 155 |
mode='markers',
|
| 156 |
name='Current Point',
|
| 157 |
-
marker=dict(size=
|
| 158 |
))
|
| 159 |
-
|
| 160 |
# Add tangent line
|
| 161 |
tangent_y = calculate_tangent(function_input, x_current, x_range)
|
| 162 |
fig.add_trace(go.Scatter(
|
|
@@ -164,16 +170,16 @@ with right_col:
|
|
| 164 |
y=tangent_y,
|
| 165 |
mode='lines',
|
| 166 |
name='Tangent Line',
|
| 167 |
-
line=dict(dash='dash', color='
|
| 168 |
))
|
| 169 |
-
|
| 170 |
# Layout adjustments
|
| 171 |
fig.update_layout(
|
| 172 |
title="Gradient Descent Progress ๐",
|
| 173 |
xaxis_title="x",
|
| 174 |
yaxis_title="f(x)",
|
| 175 |
-
template="
|
| 176 |
height=600
|
| 177 |
)
|
| 178 |
-
|
| 179 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 4 |
|
| 5 |
# Title of the app
|
| 6 |
st.set_page_config(page_title="Interactive Gradient Descent Visualizer", layout="wide")
|
| 7 |
+
st.markdown("<h1 style='text-align: center; color: #00FA9A;'>โจ Gradient Descent Visualizer โจ</h1>", unsafe_allow_html=True)
|
| 8 |
|
| 9 |
+
# Custom CSS for enhanced UI
|
| 10 |
st.markdown("""
|
| 11 |
<style>
|
| 12 |
body {
|
| 13 |
+
background: linear-gradient(to right, #141E30, #243B55);
|
| 14 |
+
color: #E0FFFF;
|
| 15 |
}
|
| 16 |
.stButton>button {
|
| 17 |
+
background: linear-gradient(to right, #00C6FF, #0072FF);
|
| 18 |
+
color: white;
|
| 19 |
+
border: none;
|
| 20 |
+
border-radius: 10px;
|
| 21 |
+
padding: 10px 15px;
|
| 22 |
font-size: 16px;
|
| 23 |
+
font-weight: bold;
|
| 24 |
}
|
| 25 |
.stButton>button:hover {
|
| 26 |
+
background: linear-gradient(to right, #0072FF, #00C6FF);
|
| 27 |
+
}
|
| 28 |
+
.iteration-controls button {
|
| 29 |
+
width: 100%;
|
| 30 |
+
margin: 5px 0;
|
| 31 |
}
|
| 32 |
</style>
|
| 33 |
""", unsafe_allow_html=True)
|
|
|
|
| 72 |
if "learning_rate" not in st.session_state:
|
| 73 |
st.session_state.learning_rate = 0.1
|
| 74 |
|
| 75 |
+
# Create two-column grid layout
|
| 76 |
+
left_col, right_col = st.columns([1, 2])
|
| 77 |
|
| 78 |
+
# Left side content
|
| 79 |
with left_col:
|
| 80 |
st.markdown("### ๐งฎ Input Your Function")
|
| 81 |
function_input = st.text_input(
|
|
|
|
| 103 |
|
| 104 |
st.markdown("### ๐๏ธ Controls")
|
| 105 |
|
| 106 |
+
if st.button("๐ Run Descent Step"):
|
| 107 |
try:
|
| 108 |
gradient = compute_derivative(function_input, st.session_state.x_current)
|
| 109 |
st.session_state.x_current -= st.session_state.learning_rate * gradient
|
|
|
|
| 117 |
if st.button("๐ Reset"):
|
| 118 |
reset_session_state()
|
| 119 |
|
| 120 |
+
# Right side content
|
| 121 |
with right_col:
|
| 122 |
st.markdown("### ๐ Gradient Descent Visualization")
|
| 123 |
+
|
| 124 |
+
# Iteration control buttons
|
| 125 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 126 |
with col1:
|
| 127 |
+
if st.button("โฎ๏ธ Previous") and st.session_state.current_index > 0:
|
| 128 |
st.session_state.current_index -= 1
|
| 129 |
with col2:
|
| 130 |
+
st.markdown(f"<p style='text-align: center;'>Iteration: <strong>{st.session_state.current_index}</strong></p>", unsafe_allow_html=True)
|
| 131 |
with col3:
|
| 132 |
+
if st.button("โญ๏ธ Next") and st.session_state.current_index < st.session_state.iter_count:
|
| 133 |
st.session_state.current_index += 1
|
| 134 |
+
|
| 135 |
try:
|
| 136 |
selected_x, selected_y = st.session_state.history[st.session_state.current_index]
|
| 137 |
st.markdown(f"๐งพ **x Value:** `{selected_x:.4f}`")
|
| 138 |
st.markdown(f"๐ **f(x):** `{selected_y:.4f}`")
|
| 139 |
except IndexError:
|
| 140 |
st.warning("No iteration data available. Please run a descent step first.")
|
| 141 |
+
|
| 142 |
# Prepare data for visualization
|
| 143 |
+
x_range = np.linspace(-10, 10, 500)
|
| 144 |
y_range = [evaluate_function(function_input, x) for x in x_range]
|
| 145 |
+
|
| 146 |
+
# Plot function curve
|
| 147 |
fig = go.Figure()
|
| 148 |
fig.add_trace(go.Scatter(
|
| 149 |
x=x_range,
|
| 150 |
y=y_range,
|
| 151 |
mode='lines',
|
| 152 |
name='Function',
|
| 153 |
+
line=dict(color='#FFD700') # Golden color for curve
|
| 154 |
))
|
| 155 |
+
|
| 156 |
# Add current point
|
| 157 |
x_current, y_current = st.session_state.history[st.session_state.current_index]
|
| 158 |
fig.add_trace(go.Scatter(
|
|
|
|
| 160 |
y=[y_current],
|
| 161 |
mode='markers',
|
| 162 |
name='Current Point',
|
| 163 |
+
marker=dict(size=12, color='#FF4500') # Red-orange for current point
|
| 164 |
))
|
| 165 |
+
|
| 166 |
# Add tangent line
|
| 167 |
tangent_y = calculate_tangent(function_input, x_current, x_range)
|
| 168 |
fig.add_trace(go.Scatter(
|
|
|
|
| 170 |
y=tangent_y,
|
| 171 |
mode='lines',
|
| 172 |
name='Tangent Line',
|
| 173 |
+
line=dict(dash='dash', color='#1E90FF') # Dodger blue for tangent line
|
| 174 |
))
|
| 175 |
+
|
| 176 |
# Layout adjustments
|
| 177 |
fig.update_layout(
|
| 178 |
title="Gradient Descent Progress ๐",
|
| 179 |
xaxis_title="x",
|
| 180 |
yaxis_title="f(x)",
|
| 181 |
+
template="plotly_dark",
|
| 182 |
height=600
|
| 183 |
)
|
| 184 |
+
|
| 185 |
st.plotly_chart(fig, use_container_width=True)
|