File size: 10,456 Bytes
b5d7faf |
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# Import necessary libraries
import streamlit as st
import numpy as np
import plotly.graph_objects as go
import math
# Full-page layout
st.set_page_config(layout="wide", page_title="Gradient Descent Visualizer")
# Main Title
st.title("")
st.title("Gradient Descent Visualizer")
# CSS for full-page layout and styling (no scrollbars)
st.markdown("""
<style>
body {
font-family: 'serif'; /* Serif font for a mathematical feel */
background-color: #161748; /* Dark background */
color: white;
width:100%:
height:100%;
overflow: hidden; /* Hide scrollbars */
}
.block-container {
padding: 1rem; /* Padding for page container */
margin: 0; /* Remove margin */
max-width: 100%; /* Full page width */
}
.stButton>button {
background-color: #000000;
color: #ff5e6c;
border-radius: 8px;
border: 2px solid #dbb6ee;
}
.stTextInput>div>div>input {
color: white;
background-color: #161748;
# border: 2px solid #dbb6ee;
border-radius: 8px;
}
.stNumberInput>div>div>input {
color: white;
background-color: #161748;
border: 2px solid #dbb6ee;
border-radius: 8px;
}
.stPlotlyChart {
border: 2px solid #dbb6ee;
border-radius: 15px;
margin: 0;
padding: 0;
}
.iteration-info {
color: black;
font-size: 18px;
font-weight: bold;
background-color: #39a0ca;
padding: 6px;
border-radius: 8px;
display: inline-block;
}
</style>
""", unsafe_allow_html=True)
# Divide the layout into two columns
left_col, right_col = st.columns(2)
# Left column for inputs and buttons
with left_col:
st.markdown("<div class='component-container'></div>", unsafe_allow_html=True) # Border for input section
st.markdown("## Function")
if 'text_input_value' not in st.session_state:
st.session_state.text_input_value = "x**2 + 3*x + 5"
# Function buttons
st.write("Functions you should try (click to auto format):")
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
if st.button("x^2", key="x2"):
st.session_state.text_input_value = "x**2"
with col2:
if st.button("x^3", key="x3"):
st.session_state.text_input_value = "x**3"
with col3:
if st.button("sin(x)", key="sinx"):
st.session_state.text_input_value = "math.sin(x)"
with col4:
if st.button("sin(1/x)", key="sin1x"):
st.session_state.text_input_value = "math.sin(1/x)"
with col5:
if st.button("log(x)", key="logx"):
st.session_state.text_input_value = "math.log(x)"
# Custom function input
st.text_input("## Enter a function of your choice :", value=st.session_state.text_input_value, key="text_input")
# Starting point input
start_point = st.number_input("## Start point :", value=2)
# Learning rate input
learn_rate = st.number_input("## Learning Rate (η) :", value=0.25)
# Setup button
if st.button("Set Up"):
st.session_state.iteration = 0
st.session_state.theta_history = [start_point]
st.session_state.current_fn = st.session_state.text_input_value
st.write("Setup complete! Click 'Next Iteration' to start.")
# Gradient descent function with error handling
def gradient_descent(fn, start_point, learning_rate, num_iterations):
theta = start_point
theta_history = [theta]
# Define function gradients manually
def get_gradient(fn, x):
epsilon = 1e-6
try:
if "x**2" in fn:
return 2 * x # derivative of x^2
elif "x**3" in fn:
return 3 * x**2 # derivative of x^3
elif "sin(x)" in fn:
return math.cos(x) # derivative of sin(x)
elif "sin(1/x)" in fn:
return -math.cos(1/x) / (x**2) # derivative of sin(1/x)
elif "log(x)" in fn:
return 1 / x # derivative of log(x)
else:
return 0 # default to 0 if function is unsupported
except:
return 0 # Handle undefined behavior
for _ in range(num_iterations):
gradient = get_gradient(fn, theta)
theta = theta - learning_rate * gradient
if abs(theta) > 1e10:
theta = np.sign(theta) * 1e10
theta_history.append(theta)
return theta_history
def plot(fn, theta_history, iteration):
# Convert history to float values
theta_history = [float(theta) for theta in theta_history]
if not theta_history:
st.write("No iterations yet. Please click 'Next Iteration'.")
return
x = np.linspace(-10, 10, 100)
y = []
# Handle edge cases for invalid function evaluations
for i in x:
try:
if "x**2" in fn:
y.append(i**2)
elif "x**3" in fn:
y.append(i**3)
elif "sin(x)" in fn:
y.append(math.sin(i))
elif "sin(1/x)" in fn:
if i != 0:
y.append(math.sin(1/i))
else:
y.append(np.nan)
elif "log(x)" in fn:
if i > 0:
y.append(math.log(i))
else:
y.append(np.nan)
else:
y.append(np.nan)
except:
y.append(np.nan)
# Remove NaN values from x and y
x_valid = x[~np.isnan(y)]
y_valid = np.array(y)[~np.isnan(y)]
last_theta = theta_history[-1]
meeting_y = None
try:
meeting_y = eval(fn.replace('x', str(last_theta))) if 'x' in fn else 0
except:
pass
# Numerical derivative using central difference
epsilon = 1e-6
try:
derivative = (eval(fn.replace('x', str(last_theta + epsilon))) - eval(fn.replace('x', str(last_theta - epsilon)))) / (2 * epsilon)
except:
derivative = 0
slope = derivative
intercept = meeting_y - slope * last_theta if meeting_y is not None else 0
tangent_y = slope * x_valid + intercept
fig = go.Figure(data=[
# Function Line
go.Scatter(x=x_valid, y=y_valid, mode='lines', name='Function',
line=dict(color='blue')),
# Gradient Descent Points
go.Scatter(x=theta_history,
y=[eval(fn.replace('x', str(theta))) for theta in theta_history],
mode='markers', name='Gradient Descent',
marker=dict(color='red', size=10)), # All points are red
# Tangent Line
go.Scatter(x=x_valid, y=tangent_y, mode='lines', name='Tangent',
line=dict(color='orange')),
# Tangent Point (Red)
go.Scatter(x=[last_theta], y=[meeting_y], mode='markers', name='Tangent Point',
marker=dict(color='red', size=12))
])
# Update layout for styling
fig.update_layout(
annotations=[
dict(
xref='paper', yref='paper', x=0.05, y=0.1,
xanchor='left', yanchor='bottom',
text=f"<b>Next Iteration: {iteration}</b>",
showarrow=False,
font=dict(size=20, color='black'),
bgcolor="#f95d9b", borderpad=5, bordercolor="black", borderwidth=2
),
dict(
xref='paper', yref='paper', x=1, y=0,
xanchor='right', yanchor='bottom',
text=f"Current Point: ({last_theta:.6f}, {meeting_y if meeting_y is not None else 'N/A'})",
showarrow=False,
font=dict(size=14, color='black'),
bgcolor="#39a0ca", borderpad=5, bordercolor="black", borderwidth=2
)
],
xaxis_title='x-axis',
yaxis_title='y-axis',
hovermode='x unified',
xaxis=dict(
range=[-10, 10],
showgrid=True, gridcolor='black',
titlefont=dict(color='black'),
tickfont=dict(color='black') # Make x-axis numbers black
),
yaxis=dict(
range=[-10, 10],
showgrid=True, gridcolor='black',
titlefont=dict(color='black'),
tickfont=dict(color='black') # Make y-axis numbers black
),
paper_bgcolor='white', # White background
plot_bgcolor='white', # White plot background
legend=dict(
yanchor='top', xanchor='right', x=1, y=0.99,
font=dict(color='black')
),
title="Gradient Descent Visualization", titlefont=dict(color='black')
)
# Display the plot
st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False})
return last_theta, meeting_y
def main():
with right_col:
if 'iteration' not in st.session_state:
st.session_state.iteration = 0
st.session_state.theta_history = [start_point]
st.session_state.current_fn = st.session_state.text_input_value
theta_history = st.session_state.theta_history
iteration = st.session_state.iteration
current_fn = st.session_state.current_fn
if st.button("Next Iteration", key="next_iter"):
iteration += 1
theta_history = gradient_descent(current_fn, start_point, learn_rate, iteration)
st.session_state.iteration = iteration
st.session_state.theta_history = theta_history
# Plot the function and gradient descent
last_theta, meeting_y = plot(current_fn, theta_history, iteration)
# Display iteration and point details
st.markdown(f"## Iteration: {int(iteration)}")
st.markdown(f"The tangent is meeting the plot at point **({last_theta}, {meeting_y if meeting_y is not None else 'N/A'})**")
# Run the app
if __name__ == "__main__":
main()
|