Date: January 18, 2024

Topic: Optimizers

Recall

Optimizers try to find the minimum value of a function through gradient descent

Notes

Optimizers

Using optimizers

  1. Provide a function to minimize (e.g., $f(x) = x^2 +0.5$)
  2. Provide an initial guess
  3. Call the optimizer

Gradient descent

Untitled


Minimizers can be used with the scipy.optimize library

Minimizing with Python

Untitled

import scipy.optimize as SPO

def f(x):
	Y = (X-1.5)**2 + 0.5
  return Y

# First, we pass in a guess
x_guess = 2.0

# Then, call the optimizer and pass in the function defined by f(x)
min_result = spo.minimize(f, x_guess, method='SLSQP', options={'disp': True})

# Print the minima coords
print(min_result.x) # x_coord
print(min_result.fun) # y_coord

# Plot function values, mark minima
Xplot = np.linspace(0.5, 2.5, 21)
Yplot = f(Xplot)
plt.plot(Xplot, Yplot)
plt.plot(min_result.x, min_result.fun, 'ro')
plt.title("Minima of an objective function")
plt.show()

Given a line drawn by any 2 points on a graph, if the line is above the graph then it is a convex problem

Convex Problems

Untitled


Optimizers need a problem to minimize, hence for linear regression, we can provide it with the sum of squared residuals.

Building a parametrized model

Minimizing Error

Untitled



<aside> 📌 SUMMARY: Minimization can be done in many dimensions, and one popular thing to minimize is the sum of squared errors to find a good line fit

</aside>