Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,191 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def safe_eval(func_str, x_val):
|
| 7 |
-
""" Safely evaluates the function at a given x value. """
|
| 8 |
-
allowed_names = {"x": x_val, "np": np}
|
| 9 |
-
try:
|
| 10 |
-
return eval(func_str, {"__builtins__": None}, allowed_names)
|
| 11 |
-
except Exception as e:
|
| 12 |
-
raise ValueError(f"Error evaluating the function: {e}")
|
| 13 |
-
|
| 14 |
-
# Function derivative using finite difference method
|
| 15 |
-
def derivative(func_str, x_val, h=1e-5):
|
| 16 |
-
""" Numerically compute the derivative of the function at x using finite differences. """
|
| 17 |
-
return (safe_eval(func_str, x_val + h) - safe_eval(func_str, x_val - h)) / (2 * h)
|
| 18 |
-
|
| 19 |
-
# Tangent line equation
|
| 20 |
-
def tangent_line(func_str, x_val, x_range):
|
| 21 |
-
""" Compute the tangent line at a given x value. """
|
| 22 |
-
y_val = safe_eval(func_str, x_val)
|
| 23 |
-
slope = derivative(func_str, x_val)
|
| 24 |
-
return slope * (x_range - x_val) + y_val
|
| 25 |
-
|
| 26 |
-
# Callback to reset session state
|
| 27 |
-
def reset_state():
|
| 28 |
-
st.session_state.x = st.session_state.starting_point
|
| 29 |
-
st.session_state.iteration = 0
|
| 30 |
-
st.session_state.x_vals = [st.session_state.starting_point]
|
| 31 |
-
st.session_state.y_vals = [safe_eval(st.session_state.func_input, st.session_state.starting_point)]
|
| 32 |
-
|
| 33 |
-
# Initialize session state variables
|
| 34 |
-
if "func_input" not in st.session_state:
|
| 35 |
-
st.session_state.func_input = "x**2 + x"
|
| 36 |
-
if "x" not in st.session_state:
|
| 37 |
-
st.session_state.x = 4.0
|
| 38 |
-
st.session_state.iteration = 0
|
| 39 |
-
st.session_state.x_vals = [4.0]
|
| 40 |
-
st.session_state.y_vals = [safe_eval(st.session_state.func_input, 4.0)]
|
| 41 |
-
if "learning_rate" not in st.session_state:
|
| 42 |
-
st.session_state.learning_rate = 0.25
|
| 43 |
-
|
| 44 |
-
# Full-width layout
|
| 45 |
-
st.set_page_config(layout="wide")
|
| 46 |
-
|
| 47 |
-
# CSS Styles for Borders, Font, Reduced Padding, and Custom Border Color
|
| 48 |
-
st.markdown(
|
| 49 |
-
"""
|
| 50 |
-
<style>
|
| 51 |
-
* {
|
| 52 |
-
font-family: Cambria, Arial, sans-serif !important;
|
| 53 |
-
}
|
| 54 |
-
h1, h2, h3, h4, h5 {
|
| 55 |
-
text-align: center;
|
| 56 |
-
margin-top: 0;
|
| 57 |
-
}
|
| 58 |
-
input, .stButton button, .stDownloadButton button {
|
| 59 |
-
border: 2px solid #ea445a;
|
| 60 |
-
border-radius: 5px;
|
| 61 |
-
padding: 10px;
|
| 62 |
-
}
|
| 63 |
-
.stInfo, .stSuccess {
|
| 64 |
-
border: 2px solid #ea445a;
|
| 65 |
-
border-radius: 5px;
|
| 66 |
-
padding: 10px;
|
| 67 |
-
}
|
| 68 |
-
.stButton {
|
| 69 |
-
margin-top: 10px;
|
| 70 |
-
}
|
| 71 |
-
/* Reduced Padding at the top */
|
| 72 |
-
.css-1d391kg {
|
| 73 |
-
padding-top: 0.5rem;
|
| 74 |
-
}
|
| 75 |
-
/* Centering the legend in the plot */
|
| 76 |
-
.stPlotlyChart {
|
| 77 |
-
display: block;
|
| 78 |
-
margin: 0 auto;
|
| 79 |
-
}
|
| 80 |
-
/* Adjusting for full width without scrolling */
|
| 81 |
-
.css-1lcbvhc {
|
| 82 |
-
padding-left: 0;
|
| 83 |
-
padding-right: 0;
|
| 84 |
-
}
|
| 85 |
-
/* Custom borders for input fields */
|
| 86 |
-
.stTextInput input, .stNumberInput input {
|
| 87 |
-
border: 2px solid #001A6E;
|
| 88 |
-
border-radius: 5px;
|
| 89 |
-
padding: 10px;
|
| 90 |
-
}
|
| 91 |
-
/* Tooltip styling */
|
| 92 |
-
.tooltip {
|
| 93 |
-
position: relative;
|
| 94 |
-
display: inline-block;
|
| 95 |
-
cursor: pointer;
|
| 96 |
-
}
|
| 97 |
-
.tooltip .tooltiptext {
|
| 98 |
-
visibility: hidden;
|
| 99 |
-
opacity: 0;
|
| 100 |
-
width: 300px;
|
| 101 |
-
background-color: #001A6E;
|
| 102 |
-
color: #fff;
|
| 103 |
-
text-align: center;
|
| 104 |
-
border-radius: 5px;
|
| 105 |
-
padding: 5px;
|
| 106 |
-
position: absolute;
|
| 107 |
-
z-index: 1;
|
| 108 |
-
bottom: 125%; /* Position the tooltip above */
|
| 109 |
-
left: 50%;
|
| 110 |
-
margin-left: -150px;
|
| 111 |
-
transition: opacity 0.3s;
|
| 112 |
-
}
|
| 113 |
-
.tooltip:hover .tooltiptext {
|
| 114 |
-
visibility: visible;
|
| 115 |
-
opacity: 1;
|
| 116 |
-
}
|
| 117 |
-
</style>
|
| 118 |
-
""",
|
| 119 |
-
unsafe_allow_html=True,
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
# Page Layout
|
| 123 |
-
st.title("🌟 Gradient Descent Interactive Tool 🌟")
|
| 124 |
-
|
| 125 |
-
col1, col2 = st.columns([1, 2])
|
| 126 |
-
|
| 127 |
-
# Left Section: User Input
|
| 128 |
-
with col1:
|
| 129 |
-
st.subheader("🔧 Define Your Function")
|
| 130 |
-
|
| 131 |
-
st.markdown(
|
| 132 |
-
"""
|
| 133 |
-
<div class="tooltip">
|
| 134 |
-
<label for="func_input">Enter a function of 'x':</label>
|
| 135 |
-
<span class="tooltiptext">
|
| 136 |
-
**How to input your function:**
|
| 137 |
-
- Please give the inputs as mentioned below
|
| 138 |
-
- x^n as x**n,
|
| 139 |
-
- sin(x) as np.sin(x)
|
| 140 |
-
- log(x) as np.log(x),
|
| 141 |
-
- e^x or exp(x) as np.exp(x).
|
| 142 |
-
</span>
|
| 143 |
-
</div>
|
| 144 |
-
""",
|
| 145 |
-
unsafe_allow_html=True
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
func_input = st.text_input(
|
| 149 |
-
"👇",
|
| 150 |
-
key="func_input",
|
| 151 |
-
on_change=reset_state
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
st.subheader("⚙️ Gradient Descent Parameters")
|
| 155 |
-
starting_point = st.number_input(
|
| 156 |
-
"Starting Point (X₀)",
|
| 157 |
-
value=4.0,
|
| 158 |
-
step=0.1,
|
| 159 |
-
format="%.2f",
|
| 160 |
-
key="starting_point",
|
| 161 |
-
on_change=reset_state
|
| 162 |
-
)
|
| 163 |
-
new_learning_rate = st.number_input(
|
| 164 |
-
"Learning Rate (ŋ)",
|
| 165 |
-
value=st.session_state.learning_rate,
|
| 166 |
-
step=0.01,
|
| 167 |
-
format="%.2f"
|
| 168 |
-
)
|
| 169 |
-
# Update the learning rate without resetting progress
|
| 170 |
-
if new_learning_rate != st.session_state.learning_rate:
|
| 171 |
-
st.session_state.learning_rate = new_learning_rate
|
| 172 |
-
|
| 173 |
-
col3, col4 = st.columns(2)
|
| 174 |
-
with col3:
|
| 175 |
-
if st.button("🔄 Set Up Function"):
|
| 176 |
-
reset_state()
|
| 177 |
-
with col4:
|
| 178 |
-
if st.button("▶️ Next Iteration"):
|
| 179 |
-
try:
|
| 180 |
-
grad = derivative(st.session_state.func_input, st.session_state.x)
|
| 181 |
-
st.session_state.x = st.session_state.x - st.session_state.learning_rate * grad
|
| 182 |
-
st.session_state.iteration += 1
|
| 183 |
-
st.session_state.x_vals.append(st.session_state.x)
|
| 184 |
-
st.session_state.y_vals.append(safe_eval(st.session_state.func_input, st.session_state.x))
|
| 185 |
-
except Exception as e:
|
| 186 |
-
st.error(f"⚠️ Error: {str(e)}")
|
| 187 |
-
|
| 188 |
-
# Right Section: Visualization
|
| 189 |
with col2:
|
| 190 |
st.subheader("📊 Gradient Descent Visualization")
|
| 191 |
try:
|
|
@@ -196,9 +11,9 @@ with col2:
|
|
| 196 |
|
| 197 |
# Function curve
|
| 198 |
fig.add_trace(go.Scatter(
|
| 199 |
-
x=x_plot,
|
| 200 |
-
y=y_plot,
|
| 201 |
-
mode="lines",
|
| 202 |
line=dict(color="blue", width=2),
|
| 203 |
name="Function"
|
| 204 |
))
|
|
@@ -224,9 +39,30 @@ with col2:
|
|
| 224 |
name="Tangent Line"
|
| 225 |
))
|
| 226 |
|
|
|
|
| 227 |
fig.update_layout(
|
| 228 |
-
xaxis=dict(
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
title="Gradient Descent Visualization",
|
| 231 |
width=800,
|
| 232 |
height=400,
|
|
@@ -237,7 +73,3 @@ with col2:
|
|
| 237 |
|
| 238 |
except Exception as e:
|
| 239 |
st.error(f"⚠️ Error in visualization: {str(e)}")
|
| 240 |
-
|
| 241 |
-
col5, col6 = st.columns(2)
|
| 242 |
-
col5.info(f"🧑💻 Iteration: {st.session_state.iteration}")
|
| 243 |
-
col6.success(f"✅ Current x: {st.session_state.x:.4f}, Current f(x): {st.session_state.y_vals[-1]:.4f}")
|
|
|
|
| 1 |
+
# Gradient Descent Visualization with Updated Axes and Color Scheme
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# Visualization Section in Streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
with col2:
|
| 5 |
st.subheader("📊 Gradient Descent Visualization")
|
| 6 |
try:
|
|
|
|
| 11 |
|
| 12 |
# Function curve
|
| 13 |
fig.add_trace(go.Scatter(
|
| 14 |
+
x=x_plot,
|
| 15 |
+
y=y_plot,
|
| 16 |
+
mode="lines",
|
| 17 |
line=dict(color="blue", width=2),
|
| 18 |
name="Function"
|
| 19 |
))
|
|
|
|
| 39 |
name="Tangent Line"
|
| 40 |
))
|
| 41 |
|
| 42 |
+
# Update layout to show negative y-axis and white axes
|
| 43 |
fig.update_layout(
|
| 44 |
+
xaxis=dict(
|
| 45 |
+
title="x-axis",
|
| 46 |
+
zeroline=True,
|
| 47 |
+
zerolinecolor="white",
|
| 48 |
+
zerolinewidth=2,
|
| 49 |
+
showgrid=True,
|
| 50 |
+
gridcolor="lightgray",
|
| 51 |
+
color="white"
|
| 52 |
+
),
|
| 53 |
+
yaxis=dict(
|
| 54 |
+
title="y-axis",
|
| 55 |
+
zeroline=True,
|
| 56 |
+
zerolinecolor="white",
|
| 57 |
+
zerolinewidth=2,
|
| 58 |
+
showgrid=True,
|
| 59 |
+
gridcolor="lightgray",
|
| 60 |
+
range=[min(y_plot) - 10, max(y_plot) + 10], # Adjust to show negative y-axis
|
| 61 |
+
color="white"
|
| 62 |
+
),
|
| 63 |
+
plot_bgcolor="black",
|
| 64 |
+
paper_bgcolor="black",
|
| 65 |
+
font=dict(color="white"),
|
| 66 |
title="Gradient Descent Visualization",
|
| 67 |
width=800,
|
| 68 |
height=400,
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
st.error(f"⚠️ Error in visualization: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|