@@ -48,17 +48,19 @@ const find_beta_from_ci = ({ci_lower, ci_upper}) => {
48
48
// backtracking line search
49
49
// <https://en.wikipedia.org/wiki/Backtracking_line_search>
50
50
// Once we know the direction, how far to go along it?
51
- const get_optimal_step_size_a = ( { a, b, d, is_a} ) => {
52
- let dir = d_a > 0 ? 1 : - 1
51
+ let outer_step_size_max = 0.05
52
+ let n_backtracking = 20
53
+ let local_minima_indicator = 2 * outer_step_size_max * ( 1 / ( 2 ** n_backtracking ) )
54
+ const get_optimal_step_size_a = ( { a, b, dir, is_a} ) => {
53
55
54
56
let step_size_min = 0
55
57
let loss_s_min = is_a ? loss ( a + step_size_min * dir , b ) : loss ( a , b + step_size_min * dir )
56
58
57
- let step_size_max = 0.1
59
+ let step_size_max = outer_step_size_max
58
60
let loss_s_max = is_a ? loss ( a + step_size_max * dir , b ) : loss ( a , b + step_size_max * dir )
59
61
60
62
61
- for ( let i = 0 ; i < 20 ; i ++ ) {
63
+ for ( let i = 0 ; i < n_backtracking ; i ++ ) {
62
64
if ( loss_s_min < loss_s_max ) {
63
65
step_size_max = ( step_size_max + step_size_min ) / 2
64
66
loss_s_max = is_a ? loss ( a + step_size_max * dir , b ) : loss ( a , b + step_size_max * dir )
@@ -77,18 +79,22 @@ const find_beta_from_ci = ({ci_lower, ci_upper}) => {
77
79
let max_steps = 2000
78
80
for ( let i = 0 ; i < max_steps ; i ++ ) {
79
81
// gradient step for a
80
- let dir_a = - df_da ( a , b )
82
+ let dir_a = df_da ( a , b ) > 0 ? - 1 : 1
81
83
// console.log(dir_a)
82
- let stepsize_a = 0.0005 // 1/n_a
84
+ // let stepsize_a = 0.0005 // 1/n_a
85
+ let stepsize_a = get_optimal_step_size_a ( { a, b, dir : dir_a , is_a : true } )
83
86
let step_a = stepsize_a // * dir_a
84
87
a = Math . max ( a + step_a , 0 )
85
88
86
89
// gradient step for b
87
- let dir_b = - df_db ( a , b )
88
- let stepsize_b = 0.0005 // 1/n_b
90
+ let dir_b = df_db ( a , b ) > 0 ? - 1 : 1
91
+ // let stepsize_b = 0.0005 // 1/n_b
92
+ let stepsize_b = get_optimal_step_size_a ( { a, b, dir : dir_b , is_a : false } )
89
93
let step_b = stepsize_b // * dir_b
90
94
b = Math . max ( b + step_b , 0 )
95
+ // console.log(`stepsize_a: ${stepsize_a}, stepsize_b: ${stepsize_b}`)
91
96
// console.log(`a: ${a}, b: ${b}`)
97
+ if ( stepsize_a + stepsize_b < local_minima_indicator ) break ;
92
98
}
93
99
return [ a , b ]
94
100
}
@@ -98,7 +104,7 @@ const find_beta_from_ci = ({ci_lower, ci_upper}) => {
98
104
let best_loss = Infinity
99
105
let best_result = null
100
106
// for(let i=0; i<num_initializations; i++){
101
- while ( best_loss > 0.001 ) {
107
+ while ( best_loss > 0.0045 ) {
102
108
let a_init = Math . random ( ) * 5
103
109
let b_init = Math . random ( ) * 5
104
110
let new_result = gradient_descent ( a_init , b_init )
0 commit comments