Skip to content

Commit

Permalink
[Feat]:
Browse files Browse the repository at this point in the history
- Implementation for line search to get the optimal learning rate @ each step
  • Loading branch information
ahmedelsayed968 committed Apr 22, 2024
1 parent 1c579b1 commit fe7d07b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions optimization/steepest_gp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import tensorflow as tf
import random
import pandas as pd

def line_search(X,eta,loss_fn):
epsilon = 1e-6
lr = tf.Variable(random.random())
with tf.GradientTape(persistent=True) as tape1:
loss = loss_fn(X)
dloss_dx = tape1.gradient(loss,X)

lrs = [lr.numpy()]
# Q = loss_fn[X-delta_x * lr]
while True:
with tf.GradientTape(persistent=True) as derivative1:
with tf.GradientTape(persistent=True) as derivative2:
Q = loss_fn(X-lr*dloss_dx)
Q_dash = derivative2.gradient(Q,lr)
Q_double_dash = derivative1.gradient(Q_dash,lr)

magnitude_gradient = tf.norm(Q_dash)
print(magnitude_gradient)
if magnitude_gradient < eta:
return lr,lrs
lr.assign_sub(Q_dash/(epsilon+Q_double_dash))
lrs.append(lr.numpy())

if __name__ == '__main__':
pass

0 comments on commit fe7d07b

Please sign in to comment.