File size: 8,920 Bytes
925f165 d59b677 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 9bf8981 afba365 9bf8981 afba365 2c3c2a5 afba365 1d23952 afba365 703a15c 925f165 afba365 0824e67 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 925f165 afba365 cfecba1 afba365 cfecba1 afba365 cfecba1 d59b677 cfecba1 d59b677 afba365 cfecba1 afba365 cfecba1 d59b677 afba365 cfecba1 db6ba1d afba365 cfecba1 afba365 cfecba1 db6ba1d afba365 2685224 c3ffa81 afba365 c3ffa81 afba365 c3ffa81 afba365 52f9369 2685224 afba365 2685224 afba365 cfecba1 2685224 cfecba1 925f165 afba365 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
import streamlit as st
import numpy as np
import plotly.graph_objects as go
# Safe function evaluation
def safe_eval(func_str, x_val):
""" Safely evaluates the function at a given x value. """
allowed_names = {"x": x_val, "np": np}
try:
return eval(func_str, {"__builtins__": None}, allowed_names)
except Exception as e:
raise ValueError(f"Error evaluating the function: {e}")
# Function derivative using finite difference method
def derivative(func_str, x_val, h=1e-5):
""" Numerically compute the derivative of the function at x using finite differences. """
return (safe_eval(func_str, x_val + h) - safe_eval(func_str, x_val - h)) / (2 * h)
# Tangent line equation
def tangent_line(func_str, x_val, x_range):
""" Compute the tangent line at a given x value. """
y_val = safe_eval(func_str, x_val)
slope = derivative(func_str, x_val)
return slope * (x_range - x_val) + y_val
# Callback to reset session state
def reset_state():
st.session_state.x = st.session_state.starting_point
st.session_state.iteration = 0
st.session_state.x_vals = [st.session_state.starting_point]
st.session_state.y_vals = [safe_eval(st.session_state.func_input, st.session_state.starting_point)]
# Initialize session state variables
if "func_input" not in st.session_state:
st.session_state.func_input = "x**2 + x"
if "x" not in st.session_state:
st.session_state.x = 4.0
st.session_state.iteration = 0
st.session_state.x_vals = [4.0]
st.session_state.y_vals = [safe_eval(st.session_state.func_input, 4.0)]
# Full-width layout
st.set_page_config(layout="wide")
# CSS Styles for Borders, Font, Reduced Padding, and Custom Border Color
st.markdown(
"""
<style>
* {
font-family: Cambria, Arial, sans-serif !important;
}
h1, h2, h3, h4, h5 {
text-align: center;
margin-top: 0;
}
input, .stButton button, .stDownloadButton button {
border: 2px solid #ea445a;
border-radius: 5px;
padding: 10px;
}
.stInfo, .stSuccess {
border: 2px solid #ea445a;
border-radius: 5px;
padding: 10px;
}
.stButton {
margin-top: 10px;
}
/* Reduced Padding at the top */
.css-1d391kg {
padding-top: 0.5rem;
}
/* Centering the legend in the plot */
.stPlotlyChart {
display: block;
margin: 0 auto;
}
/* Adjusting for full width without scrolling */
.css-1lcbvhc {
padding-left: 0;
padding-right: 0;
}
/* Custom borders for input fields */
.stTextInput input, .stNumberInput input {
border: 2px solid #001A6E;
border-radius: 5px;
padding: 10px;
}
/* Tooltip styling */
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
}
.tooltip .tooltiptext {
visibility: hidden;
opacity: 0;
width: 300px;
background-color: #001A6E;
color: #fff;
text-align: center;
border-radius: 5px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%; /* Position the tooltip above */
left: 50%;
margin-left: -150px;
transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
""",
unsafe_allow_html=True,
)
# Page Layout
st.title("π Gradient Descent Visualization Tool π")
col1, col2 = st.columns([1, 2])
# Left Section: User Input
with col1:
st.subheader("π§ Define Your Function")
# Tooltip with instructions when hovering over the function input label
st.markdown(
"""
<div class="tooltip">
<label for="func_input">Enter a function of 'x':</label>
<span class="tooltiptext">
**How to input your function:**
- Please give the inputs as mentioned below
- x^n as x**n,
- sin(x) as np.sin(x)
- log(x) as np.log(x),
- e^x or exp(x) as np.exp(x).
</span>
</div>
""",
unsafe_allow_html=True
)
# Use text input for the user to define a function, but no value argument
func_input = st.text_input(
"π",
key="func_input",
on_change=reset_state
)
st.subheader("βοΈ Gradient Descent Parameters")
starting_point = st.number_input(
"Starting Point (Xβ)",
value=4.0,
step=0.1,
format="%.2f",
key="starting_point",
on_change=reset_state
)
learning_rate = st.number_input(
"Learning Rate (Ε)",
value=0.25,
step=0.01,
format="%.2f",
key="learning_rate",
on_change=reset_state
)
col3, col4 = st.columns(2)
with col3:
if st.button("π Set Up Function"):
reset_state()
with col4:
if st.button("βΆοΈ Next Iteration"):
try:
grad = derivative(st.session_state.func_input, st.session_state.x)
st.session_state.x = st.session_state.x - learning_rate * grad
st.session_state.iteration += 1
st.session_state.x_vals.append(st.session_state.x)
st.session_state.y_vals.append(safe_eval(st.session_state.func_input, st.session_state.x))
except Exception as e:
st.error(f"β οΈ Error: {str(e)}")
# Right Section: Visualization
with col2:
st.subheader("π Gradient Descent Visualization")
try:
# Plot the function and all current and previous gradient descent points
x_plot = np.linspace(-10, 10, 400)
y_plot = [safe_eval(st.session_state.func_input, x) for x in x_plot]
fig = go.Figure()
# Function curve
fig.add_trace(go.Scatter(
x=x_plot,
y=y_plot,
mode="lines+markers",
line=dict(color="blue", width=2),
marker=dict(size=4, color="blue", symbol="circle"),
name="Function"
))
# All gradient descent points (red points without coordinates)
fig.add_trace(go.Scatter(
x=st.session_state.x_vals,
y=st.session_state.y_vals,
mode="markers",
marker=dict(color="red", size=10),
name="Gradient Descent Points"
))
# Tangent line at the current gradient descent point
current_x = st.session_state.x
tangent_x = np.linspace(-10, 10, 200) # Adjusting range to cover entire plot
tangent_y = tangent_line(st.session_state.func_input, current_x, tangent_x)
fig.add_trace(go.Scatter(
x=tangent_x,
y=tangent_y,
mode="lines",
line=dict(color="orange", width=3),
name="Tangent Line"
))
# Dynamic zoom for better visibility
fig.update_layout(
xaxis=dict(
title="x-axis",
range=[-10, 10],
showline=True,
linecolor="white",
tickcolor="white",
tickfont=dict(color="white"),
ticks="outside",
),
yaxis=dict(
title="y-axis",
range=[min(y_plot) - 5, min(max(y_plot) + 5, 1000)], # Limiting the max y to 1000
showline=True,
linecolor="white",
tickcolor="white",
tickfont=dict(color="white"),
ticks="outside",
),
plot_bgcolor="black",
paper_bgcolor="black",
title="",
margin=dict(l=10, r=10, t=10, b=10),
width=800,
height=400,
showlegend=True,
legend=dict(
x=1.1,
y=0.5,
xanchor="left",
yanchor="middle",
orientation="v",
font=dict(size=12, color="white"),
bgcolor="black",
bordercolor="white",
borderwidth=2,
)
)
# Axis lines for quadrants
fig.add_shape(type="line", x0=-10, x1=10, y0=0, y1=0, line=dict(color="white", width=2)) # x-axis
fig.add_shape(type="line", x0=0, x1=0, y0=-100, y1=100, line=dict(color="white", width=2)) # y-axis
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"β οΈ Error in visualization: {str(e)}")
# Iteration stats and download
col5, col6 = st.columns(2)
col5.info(f"π§βπ» Iteration: {st.session_state.iteration}")
col6.success(f"β
Current x: {st.session_state.x:.4f}, Current f(x): {st.session_state.y_vals[-1]:.4f}") |