Gradient Descent + Matrix Determinants
Yeshwanth Nagaraj
Democratizing Math and Core AI // Levelling playfield for the future
Objective of the post is as follows :
What is Gradient Descent ?
Gradient descent is an optimization algorithm commonly used in machine learning and mathematical optimization to minimize a function iteratively. It is an iterative method that aims to find the minimum of a function by following the steepest descent direction.
The general idea behind gradient descent is to start with an initial guess for the optimal solution and then update the guess iteratively by taking steps proportional to the negative gradient of the function at that point. The negative gradient points in the direction of the steepest descent, meaning it is the direction in which the function decreases the fastest.
Here's a simplified description of the gradient descent algorithm:
The learning rate determines the step size taken at each iteration. It is a hyperparameter that needs to be carefully chosen, as a large learning rate can cause the algorithm to overshoot the minimum, while a small learning rate can lead to slow convergence.
Gradient descent is widely used in various machine learning algorithms
There are different variants of gradient descent, such as batch gradient descent, stochastic gradient descent
领英推荐
Does gradient descent always produces same results ?
No, gradient descent does not always produce the same result. The specific outcome of the gradient descent algorithm can vary based on several factors, including the initial guess, the learning rate, the stopping criterion, and the characteristics of the function being optimized.
Here are some reasons why gradient descent may not always produce the same result:
To mitigate some of these issues, techniques such as random initialization, learning rate schedules, adaptive learning rates, and regularization methods are often used in practice to improve the convergence and stability of gradient descent. Additionally, more advanced optimization algorithms
Code Please !
The following code creates 3x3 matrices using gradient descent ; Copy this to any python interpreter, either offline or online one . Make a first run check the matrix produced for first time ; Hit run again , Magical , isn't it !!
Remeber this !! we'll use this to explain awesome Generative AI concepts !! Keep the excitement on for day+=1 !! Sign off :)
import numpy as np
import scipy.optimize as opt
def objective_function(matrix, desired_determinant):
? ? current_determinant = np.linalg.det(matrix)
? ? return abs(current_determinant - desired_determinant)
def construct_matrix(desired_determinant, learning_rate, num_iterations):
? ? # Initialize the matrix
? ? matrix = np.random.rand(3, 3)
? ? # Define the objective function with the desired determinant
? ? objective = lambda x: objective_function(x.reshape((3, 3)), desired_determinant)
? ? # Perform gradient descent optimization
? ? for _ in range(num_iterations):
? ? ? ? gradient = opt.approx_fprime(matrix.flatten(), objective, epsilon=1e-8)
? ? ? ? matrix -= learning_rate * gradient.reshape(matrix.shape)
? ? return matrix
# Define the desired determinant
desired_determinant = 5
# Set the learning rate and number of iterations for gradient descent
learning_rate = 0.01
num_iterations = 1000
# Construct the matrix using gradient descent
matrix = construct_matrix(desired_determinant, learning_rate, num_iterations)
# Print the matrix and its determinant
print("Matrix:")
print(matrix)
print("Determinant:", np.linalg.det(matrix))
Researcher in Nonlinear Synchronization with Machine Learning in Julia
1 年It was very useful to me. Thanks for sharing and keep sharing more.
Democratizing Math and Core AI // Levelling playfield for the future
1 年Colab notebook for the code : https://colab.research.google.com/drive/1JPSLfCBq50oPkfWzMcu1x7TnLiGAvwvx?usp=sharing