Skip to content

Commit

Permalink
feat: started gradient descent
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorW-code committed Mar 2, 2024
1 parent 4da1772 commit 23fc19a
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion ml_code/optimization_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
import pytorch

# Gradient Descent
# https://machinelearningmastery.com/gradient-descent-optimization-from-scratch/
# only works on differentiable functions
# requires - target function & derivative of target function
def gradient_descent(objective, derivative, bounds, n_iter, step_size):
# track all solutions
solutions, scores = list(), list()

# generate an initial point
solution = bounds[:, 0] + rand(len(bounds)) * (bounds[:, 1] - bounds[:, 0])

# run the gradient descent
for i in range(n_iter):

# calculate gradient
gradient = derivative(solution)

# take a step
solution = solution - step_size * gradient

# evaluate candidate point
solution_eval = objective(solution)

# store solution
solutions.append(solution)

scores.append(solution_eval)
# report progress
print('>%d f(%s) = %.5f' % (i, solution, solution_eval))

return [solutions, scores]


# Stochastic Gradient Descent

Expand All @@ -14,4 +47,4 @@

# Adam

# Nadam
# Nadam

0 comments on commit 23fc19a

Please sign in to comment.