santosh7/Gradient-descent-visualiser
0
1import streamlit as st2import numpy as np3import matplotlib.pyplot as plt4 5# Functions and their derivatives6functions = {7 "sin(x)": (np.sin, np.cos),8 "x^2": (lambda x: x**2, lambda x: 2*x),9 "x": (lambda x: x, lambda x: np.ones_like(x)),10 "x^3": (lambda x: np.power(x, 3), lambda x: 3 * np.power(x, 2)),11 "e^x": (np.exp, np.exp)12}13 14# Gradient Descent Simulation15def gradient_descent_step(func, derivative, x, learning_rate):16 grad = derivative(x)17 x = x - learning_rate * grad18 return x, func(x)19 20# Function to plot the function and gradient descent points21def plot_gradient_descent(func, derivative, points, learning_rate):22 x_vals = np.linspace(-10, 10, 400)23 y_vals = func(x_vals)24 25 plt.figure(figsize=(10, 6))26 plt.plot(x_vals, y_vals, label=f"f(x)")27 28 for i, (x, y) in enumerate(points):29 # Plot the point30 plt.scatter(x, y, color='red')31 # Plot the tangent line32 slope = derivative(x)33 tangent_line = slope * (x_vals - x) + y34 plt.plot(x_vals, tangent_line, '--', color='gray', alpha=0.5, label=f"Tangent at iteration {i}" if i == 0 else "")35 36 plt.title(f"Gradient Descent with Learning Rate {learning_rate}")37 plt.xlabel("x")38 plt.ylabel("f(x)")39 plt.axhline(0, color='black', linewidth=0.5)40 plt.axvline(0, color='black', linewidth=0.5)41 plt.grid(True)42 plt.legend()43 44 st.pyplot(plt)45 46# Streamlit app47st.title("Learning Rate Optimization")48 49st.sidebar.image('Innomatics-Logo1.png', use_column_width=True)50# Initialize session state variables if not already initialized51if 'current_iteration' not in st.session_state:52 st.session_state.current_iteration = 053if 'points' not in st.session_state:54 st.session_state.points = []55if 'x' not in st.session_state:56 st.session_state.x = None57 58# User input for selecting the function59function_name = st.sidebar.selectbox("Select a function to plot", list(functions.keys()))60 61# Generate starting points including 0.9962starting_points = np.round(np.linspace(-10, 10, 21), 2).tolist() # 21 points between -10 and 10 with 2 decimal precision63if 0.99 not in starting_points:64 starting_points.append(0.99)65starting_points = sorted(starting_points) # Ensure the list is sorted66starting_point = st.sidebar.selectbox("Select Starting Point", starting_points, index=starting_points.index(5.0))67 68# Generate learning rates including 0.4469learning_rates = np.round(np.linspace(0.001, 1.0, 100), 3).tolist() # 100 points between 0.001 and 1.070if 0.44 not in learning_rates:71 learning_rates.append(0.44)72learning_rates = sorted(learning_rates) # Ensure the list is sorted73default_learning_rate = 0.174closest_index = int(np.argmin(np.abs(np.array(learning_rates) - default_learning_rate))) # Convert to int75learning_rate = st.sidebar.selectbox("Select Learning Rate", learning_rates, index=closest_index)76 77# Selectbox for number of iterations78iterations_list = list(range(1, 51)) # Generates numbers from 1 to 5079iterations = st.sidebar.selectbox("Select Number of Iterations", iterations_list, index=iterations_list.index(10))80 81# Submit button82submit_button = st.sidebar.button(label='Submit')83 84# Handle form submission85if submit_button:86 st.session_state.current_iteration = 087 st.session_state.points = [(starting_point, functions[function_name][0](starting_point))]88 st.session_state.x = starting_point89 90# Next iteration button91if st.sidebar.button("Next Iteration"):92 if st.session_state.current_iteration < iterations:93 st.session_state.current_iteration += 194 x, y = gradient_descent_step(functions[function_name][0], functions[function_name][1], st.session_state.x, learning_rate)95 st.session_state.x = x96 st.session_state.points.append((x, y))97 98# Plot the result after every iteration99if st.session_state.points:100 plot_gradient_descent(functions[function_name][0], functions[function_name][1], st.session_state.points, learning_rate)101 