Skip to content

Commit

Permalink
feat: add the beginning of backtracking line search
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoSempere committed Mar 15, 2023
1 parent 3716566 commit 167ae2a
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,50 @@ const find_beta_from_ci = ({ci_lower, ci_upper}) => {
let result = (f_h - f)/h
return result
}

// backtracking line search
// <https://en.wikipedia.org/wiki/Backtracking_line_search>
// Once we know the direction, how far to go along it?
const get_optimal_step_size_a = ({a,b, d, is_a}) => {
let dir = d_a > 0 ? 1 : -1

let step_size_min = 0
let loss_s_min = is_a ? loss(a + step_size_min * dir, b) : loss(a, b + step_size_min * dir)

let step_size_max = 0.1
let loss_s_max = is_a ? loss(a + step_size_max * dir, b) : loss(a, b + step_size_max * dir)


for(let i=0; i<20; i++){
if(loss_s_min < loss_s_max){
step_size_max = (step_size_max + step_size_min) / 2
loss_s_max = is_a ? loss(a + step_size_max * dir, b) : loss(a, b + step_size_max * dir)
}else{
step_size_min = (step_size_max + step_size_min) / 2
loss_s_min = is_a ? loss(a + step_size_min * dir, b) : loss(a, b + step_size_min * dir)
}
}
return (step_size_min + step_size_max)/2
}

// gradient descent step
const gradient_descent = (a_init,b_init) => {
let epsilon = 2**(-14) // 1/16384
let n_a = 2
let n_b = 2
let a = a_init
let b = b_init
let max_steps = 2000
for(let i = 0; i<max_steps; i++){
// gradient step for a
let dir_a = - df_da(a,b)
// console.log(dir_a)
let stepsize_a = 0.0005 // 1/n_a
let step_a = stepsize_a * dir_a
let step_a = stepsize_a // * dir_a
a = Math.max(a + step_a, 0)
n_a = n_a + 1

// gradient step for b
let dir_b = - df_db(a,b)
let stepsize_b = 0.0005 // 1/n_b
let step_b = stepsize_b * dir_b
let step_b = stepsize_b // * dir_b
b = Math.max(b + step_b,0)
n_b = n_b + 1
// console.log(`a: ${a}, b: ${b}`)
}
return [a, b]
Expand Down

0 comments on commit 167ae2a

Please sign in to comment.