Update app.py
Browse files
app.py
CHANGED
|
@@ -1,293 +1,170 @@
|
|
| 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 |
-
y.append(math.log(i))
|
| 172 |
-
else:
|
| 173 |
-
y.append(np.nan)
|
| 174 |
-
else:
|
| 175 |
-
y.append(np.nan)
|
| 176 |
-
except:
|
| 177 |
-
y.append(np.nan)
|
| 178 |
-
|
| 179 |
-
# Remove NaN values from x and y
|
| 180 |
-
x_valid = x[~np.isnan(y)]
|
| 181 |
-
y_valid = np.array(y)[~np.isnan(y)]
|
| 182 |
-
|
| 183 |
-
last_theta = theta_history[-1]
|
| 184 |
-
meeting_y = None
|
| 185 |
-
try:
|
| 186 |
-
meeting_y = eval(fn.replace('x', str(last_theta))) if 'x' in fn else 0
|
| 187 |
-
except:
|
| 188 |
-
pass
|
| 189 |
-
|
| 190 |
-
# Numerical derivative using central difference
|
| 191 |
-
epsilon = 1e-6
|
| 192 |
-
try:
|
| 193 |
-
derivative = (eval(fn.replace('x', str(last_theta + epsilon))) - eval(fn.replace('x', str(last_theta - epsilon)))) / (2 * epsilon)
|
| 194 |
-
except:
|
| 195 |
-
derivative = 0
|
| 196 |
-
slope = derivative
|
| 197 |
-
intercept = meeting_y - slope * last_theta if meeting_y is not None else 0
|
| 198 |
-
tangent_y = slope * x_valid + intercept
|
| 199 |
-
|
| 200 |
-
fig = go.Figure(data=[
|
| 201 |
-
# Function Line
|
| 202 |
-
go.Scatter(x=x_valid, y=y_valid, mode='lines', name='Function',
|
| 203 |
-
line=dict(color='blue')),
|
| 204 |
-
# Gradient Descent Points
|
| 205 |
-
go.Scatter(x=theta_history,
|
| 206 |
-
y=[eval(fn.replace('x', str(theta))) for theta in theta_history],
|
| 207 |
-
mode='markers', name='Gradient Descent',
|
| 208 |
-
marker=dict(color='red', size=10)), # All points are red
|
| 209 |
-
# Tangent Line
|
| 210 |
-
go.Scatter(x=x_valid, y=tangent_y, mode='lines', name='Tangent',
|
| 211 |
-
line=dict(color='orange')),
|
| 212 |
-
# Tangent Point (Red)
|
| 213 |
-
go.Scatter(x=[last_theta], y=[meeting_y], mode='markers', name='Tangent Point',
|
| 214 |
-
marker=dict(color='red', size=12))
|
| 215 |
-
])
|
| 216 |
-
|
| 217 |
-
# Update layout for styling
|
| 218 |
-
fig.update_layout(
|
| 219 |
-
annotations=[
|
| 220 |
-
dict(
|
| 221 |
-
xref='paper', yref='paper', x=0.05, y=0.1,
|
| 222 |
-
xanchor='left', yanchor='bottom',
|
| 223 |
-
text=f"<b>Next Iteration: {iteration}</b>",
|
| 224 |
-
showarrow=False,
|
| 225 |
-
font=dict(size=20, color='black'),
|
| 226 |
-
bgcolor="#f95d9b", borderpad=5, bordercolor="black", borderwidth=2
|
| 227 |
-
),
|
| 228 |
-
dict(
|
| 229 |
-
xref='paper', yref='paper', x=1, y=0,
|
| 230 |
-
xanchor='right', yanchor='bottom',
|
| 231 |
-
text=f"Current Point: ({last_theta:.6f}, {meeting_y if meeting_y is not None else 'N/A'})",
|
| 232 |
-
showarrow=False,
|
| 233 |
-
font=dict(size=14, color='black'),
|
| 234 |
-
bgcolor="#39a0ca", borderpad=5, bordercolor="black", borderwidth=2
|
| 235 |
-
)
|
| 236 |
-
],
|
| 237 |
-
xaxis_title='x-axis',
|
| 238 |
-
yaxis_title='y-axis',
|
| 239 |
-
hovermode='x unified',
|
| 240 |
-
xaxis=dict(
|
| 241 |
-
range=[-10, 10],
|
| 242 |
-
showgrid=True, gridcolor='black',
|
| 243 |
-
titlefont=dict(color='black'),
|
| 244 |
-
tickfont=dict(color='black') # Make x-axis numbers black
|
| 245 |
-
),
|
| 246 |
-
yaxis=dict(
|
| 247 |
-
range=[-10, 10],
|
| 248 |
-
showgrid=True, gridcolor='black',
|
| 249 |
-
titlefont=dict(color='black'),
|
| 250 |
-
tickfont=dict(color='black') # Make y-axis numbers black
|
| 251 |
-
),
|
| 252 |
-
paper_bgcolor='white', # White background
|
| 253 |
-
plot_bgcolor='white', # White plot background
|
| 254 |
-
legend=dict(
|
| 255 |
-
yanchor='top', xanchor='right', x=1, y=0.99,
|
| 256 |
-
font=dict(color='black')
|
| 257 |
-
),
|
| 258 |
-
title="Gradient Descent Visualization", titlefont=dict(color='black')
|
| 259 |
-
)
|
| 260 |
-
|
| 261 |
-
# Display the plot
|
| 262 |
-
st.plotly_chart(fig, use_container_width=True, config={'displayModeBar': False})
|
| 263 |
-
|
| 264 |
-
return last_theta, meeting_y
|
| 265 |
-
|
| 266 |
-
def main():
|
| 267 |
-
with right_col:
|
| 268 |
-
if 'iteration' not in st.session_state:
|
| 269 |
-
st.session_state.iteration = 0
|
| 270 |
-
st.session_state.theta_history = [start_point]
|
| 271 |
-
st.session_state.current_fn = st.session_state.text_input_value
|
| 272 |
-
|
| 273 |
-
theta_history = st.session_state.theta_history
|
| 274 |
-
iteration = st.session_state.iteration
|
| 275 |
-
current_fn = st.session_state.current_fn
|
| 276 |
-
|
| 277 |
-
if st.button("Next Iteration", key="next_iter"):
|
| 278 |
-
iteration += 1
|
| 279 |
-
theta_history = gradient_descent(current_fn, start_point, learn_rate, iteration)
|
| 280 |
-
st.session_state.iteration = iteration
|
| 281 |
-
st.session_state.theta_history = theta_history
|
| 282 |
-
|
| 283 |
-
# Plot the function and gradient descent
|
| 284 |
-
last_theta, meeting_y = plot(current_fn, theta_history, iteration)
|
| 285 |
-
|
| 286 |
-
# Display iteration and point details
|
| 287 |
-
st.markdown(f"## Iteration: {int(iteration)}")
|
| 288 |
-
st.markdown(f"The tangent is meeting the plot at point **({last_theta}, {meeting_y if meeting_y is not None else 'N/A'})**")
|
| 289 |
-
|
| 290 |
-
# Run the app
|
| 291 |
-
if __name__ == "__main__":
|
| 292 |
-
main()
|
| 293 |
-
|
|
|
|
| 1 |
+
Rohith Ramdass
|
| 2 |
+
rohith.r18
|
| 3 |
+
Idle
|
| 4 |
+
|
| 5 |
+
Kande Chandrika_IN1241221 — Today at 14:47
|
| 6 |
+
hi
|
| 7 |
+
Rohith Ramdass — Today at 14:48
|
| 8 |
+
hi
|
| 9 |
+
Kande Chandrika_IN1241221 — Today at 14:48
|
| 10 |
+
wr r u
|
| 11 |
+
Rohith Ramdass — Today at 14:48
|
| 12 |
+
i can se u
|
| 13 |
+
Kande Chandrika_IN1241221 — Today at 14:48
|
| 14 |
+
can u send ur space with code also
|
| 15 |
+
Rohith Ramdass — Today at 14:48
|
| 16 |
+
there u have place?
|
| 17 |
+
Kande Chandrika_IN1241221 — Today at 14:48
|
| 18 |
+
haa
|
| 19 |
+
Rohith Ramdass — Today at 14:49
|
| 20 |
+
cmg
|
| 21 |
+
Kande Chandrika_IN1241221 — Today at 14:49
|
| 22 |
+
ok
|
| 23 |
+
Rohith Ramdass — Today at 15:18
|
| 24 |
+
import cv2
|
| 25 |
+
import numpy as np
|
| 26 |
+
|
| 27 |
+
# Create a window and set its callback function
|
| 28 |
+
cv2.namedWindow('Painting')
|
| 29 |
+
cv2.setMouseCallback('Painting', lambda event, x, y, flags, param: mouse_event(event, x, y, flags, param))
|
| 30 |
+
Expand
|
| 31 |
+
message.txt
|
| 32 |
+
4 KB
|
| 33 |
+
Rohith Ramdass — Today at 15:33
|
| 34 |
+
import streamlit as st
|
| 35 |
+
|
| 36 |
+
# Title
|
| 37 |
+
st.title("Machine Learning Project")
|
| 38 |
+
|
| 39 |
+
# header
|
| 40 |
+
Expand
|
| 41 |
+
app1.py
|
| 42 |
+
4 KB
|
| 43 |
+
Kande Chandrika_IN1241221 — Today at 20:29
|
| 44 |
+
import streamlit as st
|
| 45 |
+
import numpy as np
|
| 46 |
+
import plotly.graph_objects as go
|
| 47 |
+
|
| 48 |
+
# Title of the app
|
| 49 |
+
st.title("Gradient Descent Visualizer with Tangent Lines")
|
| 50 |
+
Expand
|
| 51 |
+
message.txt
|
| 52 |
+
5 KB
|
| 53 |
+
Rohith Ramdass — Today at 20:43
|
| 54 |
+
Thanks
|
| 55 |
+
|
| 56 |
+
Kande Chandrika_IN1241221
|
| 57 |
+
k.chandrika.
|
| 58 |
+
import streamlit as st
|
| 59 |
+
import numpy as np
|
| 60 |
+
import plotly.graph_objects as go
|
| 61 |
+
|
| 62 |
+
# Title of the app
|
| 63 |
+
st.title("Gradient Descent Visualizer with Tangent Lines")
|
| 64 |
+
|
| 65 |
+
# Safe function evaluation
|
| 66 |
+
def safe_eval(func_str, x_val):
|
| 67 |
+
""" Safely evaluates the function at a given x value. """
|
| 68 |
+
allowed_names = {"x": x_val, "np": np} # Only allow x and numpy
|
| 69 |
+
return eval(func_str, {"__builtins__": None}, allowed_names)
|
| 70 |
+
|
| 71 |
+
# Function derivative using finite difference method
|
| 72 |
+
def derivative(func_str, x_val, h=1e-5):
|
| 73 |
+
""" Numerically compute the derivative of the function at x using finite differences. """
|
| 74 |
+
return (safe_eval(func_str, x_val + h) - safe_eval(func_str, x_val - h)) / (2 * h)
|
| 75 |
+
|
| 76 |
+
# Tangent Line Equation
|
| 77 |
+
def tangent_line(func_str, x_val, x_range):
|
| 78 |
+
""" Compute the tangent line at a given x value. """
|
| 79 |
+
y_val = safe_eval(func_str, x_val)
|
| 80 |
+
slope = derivative(func_str, x_val)
|
| 81 |
+
return slope * (x_range - x_val) + y_val
|
| 82 |
+
|
| 83 |
+
# Callback to reset session state
|
| 84 |
+
def reset_state():
|
| 85 |
+
st.session_state.x = st.session_state.starting_point
|
| 86 |
+
st.session_state.iteration = 0
|
| 87 |
+
st.session_state.x_vals = [st.session_state.starting_point]
|
| 88 |
+
st.session_state.y_vals = [safe_eval(st.session_state.func_input, st.session_state.starting_point)]
|
| 89 |
+
|
| 90 |
+
# Function input
|
| 91 |
+
st.header("Define Your Function")
|
| 92 |
+
func_input = st.text_input("Enter a function of 'x' (e.g., x**2 + x, sin(x), x**3 - 3*x + 2):", "x**2 + x", key="func_input", on_change=reset_state)
|
| 93 |
+
|
| 94 |
+
# Starting Point and Learning Rate
|
| 95 |
+
st.header("Gradient Descent Parameters")
|
| 96 |
+
starting_point = st.number_input("Starting Point", value=4.0, step=0.1, format="%.2f", key="starting_point", on_change=reset_state)
|
| 97 |
+
learning_rate = st.number_input("Learning Rate", value=0.1, step=0.01, format="%.2f", key="learning_rate", on_change=reset_state)
|
| 98 |
+
|
| 99 |
+
# Initialize session state variables if they don't exist
|
| 100 |
+
if "x" not in st.session_state:
|
| 101 |
+
st.session_state.x = starting_point
|
| 102 |
+
st.session_state.iteration = 0
|
| 103 |
+
st.session_state.x_vals = [starting_point]
|
| 104 |
+
st.session_state.y_vals = [safe_eval(func_input, starting_point)]
|
| 105 |
+
|
| 106 |
+
# "Next Iteration" button logic
|
| 107 |
+
if st.button("Next Iteration"):
|
| 108 |
+
try:
|
| 109 |
+
# Perform one iteration of gradient descent
|
| 110 |
+
grad = derivative(func_input, st.session_state.x)
|
| 111 |
+
st.session_state.x = st.session_state.x - learning_rate * grad
|
| 112 |
+
st.session_state.iteration += 1
|
| 113 |
+
|
| 114 |
+
# Save the new x and y values
|
| 115 |
+
st.session_state.x_vals.append(st.session_state.x)
|
| 116 |
+
st.session_state.y_vals.append(safe_eval(func_input, st.session_state.x))
|
| 117 |
+
except Exception as e:
|
| 118 |
+
st.error(f"Error: {str(e)}")
|
| 119 |
+
|
| 120 |
+
# Display iteration results
|
| 121 |
+
st.subheader("Gradient Descent Progress")
|
| 122 |
+
st.write(f"Iteration: {st.session_state.iteration}")
|
| 123 |
+
st.write(f"Current x: {st.session_state.x:.4f}")
|
| 124 |
+
st.write(f"Current f(x): {st.session_state.y_vals[-1]:.4f}")
|
| 125 |
+
|
| 126 |
+
# Plot the function, gradient descent points, and tangent line
|
| 127 |
+
x_plot = np.linspace(-10, 10, 400)
|
| 128 |
+
y_plot = [safe_eval(func_input, x) for x in x_plot]
|
| 129 |
+
|
| 130 |
+
fig = go.Figure()
|
| 131 |
+
|
| 132 |
+
# Add the function curve
|
| 133 |
+
fig.add_trace(go.Scatter(x=x_plot, y=y_plot, mode="lines", name="Function"))
|
| 134 |
+
|
| 135 |
+
# Add gradient descent points in red
|
| 136 |
+
fig.add_trace(go.Scatter(
|
| 137 |
+
x=st.session_state.x_vals,
|
| 138 |
+
y=st.session_state.y_vals,
|
| 139 |
+
mode="markers",
|
| 140 |
+
marker=dict(color="red", size=8),
|
| 141 |
+
name="Gradient Descent Points"
|
| 142 |
+
))
|
| 143 |
+
|
| 144 |
+
# Add the tangent line at the current point
|
| 145 |
+
current_x = st.session_state.x
|
| 146 |
+
current_y = safe_eval(func_input, current_x)
|
| 147 |
+
slope = derivative(func_input, current_x)
|
| 148 |
+
|
| 149 |
+
# Generate tangent line range
|
| 150 |
+
tangent_x = np.linspace(current_x - 2, current_x + 2, 100)
|
| 151 |
+
tangent_y = tangent_line(func_input, current_x, tangent_x)
|
| 152 |
+
|
| 153 |
+
# Plot the tangent line as a straight solid line
|
| 154 |
+
fig.add_trace(go.Scatter(
|
| 155 |
+
x=tangent_x,
|
| 156 |
+
y=tangent_y,
|
| 157 |
+
mode="lines",
|
| 158 |
+
line=dict(color="orange", width=3),
|
| 159 |
+
name="Tangent Line"
|
| 160 |
+
))
|
| 161 |
+
|
| 162 |
+
# Update layout
|
| 163 |
+
fig.update_layout(
|
| 164 |
+
xaxis_title="x",
|
| 165 |
+
yaxis_title="f(x)",
|
| 166 |
+
title="Gradient Descent Visualization with Tangent Line"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Display the plot
|
| 170 |
+
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|