Optimisation and gradient descent algorithm Flashcards
explain simply how a machine learning model works?
predict—->calculate error—–>learn—–>predict
this is called an algorithm
what is an algorithm?
algorithm is a set of mathematical instruction for solving a problem
it is basically a word used by programmers when they don’t want to explain what they did
where did the term algorithm originate from?
Muhammad ibn Musa Al-Khwarizmi a ninth-century Persian mathematician who wrote a popular mathematics book of that time, when that book was translated to latin the translators where confused with his name and termed it algorithm
what is cost function in machine learining?
-a cost function is an important parameter in deciding how well a machine learning model fits into the dataset
-it the sum of squares of the difference between actual and fitted values
-we need a function that can find when the model is most accurate whole the way between undertrained and overtrained model
-by minimizing the value of cost function we will get an optimal solution
-Cost function is a measure of how wrong the model is in estimating the relationship between X(input) and Y(output) Parameter
-also called as loss function, error function etc..
what is Latex markdown?
it is a syntax to write down mathematical expressions
what is linspace in numpy?
it generates an array of linearly spaced numbers between a and b
what is subplot and how to implement it in matplotlib?
used to display two figures side by side
explain about cost function implementation in python
-represent function
-represent derivative of function
-at the minimum f(x) the slope of function will be zero which is found from the derivative plot
explain briefly about gradient descent algorithm?
Gradient Descent is an optimization algorithm used for minimizing the cost function in various machine learning algorithms.
visualize 3d model of cost function
downward convex
Implement an optimization algorithm in python
Gradient Descent
new_x = 3
previous_x = 0
step_multiplier = 0.1
precision = 0.00001
x_list = [new_x]
slope_list = [df(new_x)]
for n in range(500):
previous_x = new_x
gradient = df(previous_x)
new_x = previous_x - step_multiplier * gradient
step_size = abs(new_x - previous_x) # print(step_size) x_list.append(new_x) slope_list.append(df(new_x)) if step_size < precision: print('Loop ran this many times:', n) break
print(‘Local minimum occurs at:’, new_x)
print(‘Slope or df(x) value at this point is:’, df(new_x))
print(‘f(x) value or cost at this point is:’, f(new_x))
scatter function can plot with list of x true or false?
False
scatter function cannot plot list it can only plot arrays therefore we will have to convert list to array using numpy
what happens with gradient descent when there is a maxima ,local minima and global minima?
the gradient descent depends on the initial guess
if the initial guess is near the local minima then the algorithm won’t converge to global minima thereby giving us the wrong output
implement gradient descent by calling a function?
def gradient_descent(df,initial_guess,step_multiplier=0.02,precision=0.001):
new_x = initial_guess x_list = [new_x] slope_list = [df(new_x)] for n in range(500): previous_x = new_x gradient = df(previous_x) new_x = previous_x - step_multiplier * gradient step_size = abs(new_x - previous_x) x_list.append(new_x) slope_list.append(df(new_x)) if step_size < precision: print('Loop ran this many times:', n) break return new_x,x_list,slope_list
localmin,x_list,slope_list=gradient_descent(df,0,0.02,0.001)
print(localmin)
what is the difference between stochastic and batch gradient descent?
stochastic descent has the feature of randomness
it can deal with random initial guesses thereby trying to predict the correct minima better that batch