@@ -124,7 +124,7 @@ class UpdateMethod(enum.Enum):
124
124
125
125
def filter_top_directions (
126
126
perturbations : FloatArray2D , function_values : FloatArray ,
127
- est_type : EstimatorType ,
127
+ estimator_type : EstimatorType ,
128
128
num_top_directions : int ) -> Tuple [FloatArray , FloatArray ]:
129
129
"""Select the subset of top-performing perturbations.
130
130
@@ -134,7 +134,7 @@ def filter_top_directions(
134
134
p, -p in the even/odd entries, so the directions p_1,...,p_n
135
135
will be ordered (p_1, -p_1, p_2, -p_2,...)
136
136
function_values: np array of reward values (maximization)
137
- est_type : (forward_fd | antithetic)
137
+ estimator_type : (forward_fd | antithetic)
138
138
num_top_directions: the number of top directions to include
139
139
For antithetic, the total number of perturbations will
140
140
be 2* this number, because we count p, -p as a single
@@ -148,16 +148,16 @@ def filter_top_directions(
148
148
"""
149
149
if not num_top_directions > 0 :
150
150
return (perturbations , function_values )
151
- if est_type == EstimatorType .FORWARD_FD :
151
+ if estimator_type == EstimatorType .FORWARD_FD :
152
152
top_index = np .argsort (- function_values )
153
- elif est_type == EstimatorType .ANTITHETIC :
153
+ elif estimator_type == EstimatorType .ANTITHETIC :
154
154
top_index = np .argsort (- np .abs (function_values [0 ::2 ] -
155
155
function_values [1 ::2 ]))
156
156
top_index = top_index [:num_top_directions ]
157
- if est_type == EstimatorType .FORWARD_FD :
157
+ if estimator_type == EstimatorType .FORWARD_FD :
158
158
perturbations = perturbations [top_index ]
159
159
function_values = function_values [top_index ]
160
- elif est_type == EstimatorType .ANTITHETIC :
160
+ elif estimator_type == EstimatorType .ANTITHETIC :
161
161
perturbations = np .concatenate (
162
162
(perturbations [2 * top_index ], perturbations [2 * top_index + 1 ]),
163
163
axis = 0 )
@@ -245,11 +245,11 @@ class StatefulOptimizer(BlackboxOptimizer):
245
245
Class contains common methods for handling the state.
246
246
"""
247
247
248
- def __init__ (self , est_type : EstimatorType , normalize_fvalues : bool ,
248
+ def __init__ (self , estimator_type : EstimatorType , normalize_fvalues : bool ,
249
249
hyperparameters_update_method : UpdateMethod ,
250
250
extra_params : Optional [Sequence [int ]]):
251
251
252
- self .est_type = est_type
252
+ self .estimator_type = estimator_type
253
253
self .normalize_fvalues = normalize_fvalues
254
254
self .hyperparameters_update_method = hyperparameters_update_method
255
255
if hyperparameters_update_method == UpdateMethod .STATE_NORMALIZATION :
@@ -321,7 +321,7 @@ class MonteCarloBlackboxOptimizer(StatefulOptimizer):
321
321
322
322
def __init__ (self ,
323
323
precision_parameter : float ,
324
- est_type : EstimatorType ,
324
+ estimator_type : EstimatorType ,
325
325
normalize_fvalues : bool ,
326
326
hyperparameters_update_method : UpdateMethod ,
327
327
extra_params : Optional [Sequence [int ]],
@@ -342,8 +342,8 @@ def __init__(self,
342
342
self .precision_parameter = precision_parameter
343
343
self .num_top_directions = num_top_directions
344
344
self .gradient_ascent_optimizer = gradient_ascent_optimizer
345
- super ().__init__ (est_type , normalize_fvalues , hyperparameters_update_method ,
346
- extra_params )
345
+ super ().__init__ (estimator_type , normalize_fvalues ,
346
+ hyperparameters_update_method , extra_params )
347
347
348
348
# TODO: Issue #285
349
349
def run_step (self , perturbations : FloatArray2D , function_values : FloatArray ,
@@ -358,14 +358,14 @@ def run_step(self, perturbations: FloatArray2D, function_values: FloatArray,
358
358
function_values = np .array (normalized_values [:- 1 ])
359
359
current_value = normalized_values [- 1 ]
360
360
top_ps , top_fs = filter_top_directions (perturbations , function_values ,
361
- self .est_type ,
361
+ self .estimator_type ,
362
362
self .num_top_directions )
363
363
gradient = np .zeros (dim )
364
364
for i , perturbation in enumerate (top_ps ):
365
365
function_value = top_fs [i ]
366
- if self .est_type == EstimatorType .FORWARD_FD :
366
+ if self .estimator_type == EstimatorType .FORWARD_FD :
367
367
gradient_sample = (function_value - current_value ) * perturbation
368
- elif self .est_type == EstimatorType .ANTITHETIC :
368
+ elif self .estimator_type == EstimatorType .ANTITHETIC :
369
369
gradient_sample = function_value * perturbation
370
370
gradient_sample /= self .precision_parameter ** 2
371
371
gradient += gradient_sample
@@ -374,7 +374,7 @@ def run_step(self, perturbations: FloatArray2D, function_values: FloatArray,
374
374
# in that code, the denominator for antithetic was num_top_directions.
375
375
# we maintain compatibility for now so that the same hyperparameters
376
376
# currently used in Toaster will have the same effect
377
- if self .est_type == EstimatorType .ANTITHETIC and \
377
+ if self .estimator_type == EstimatorType .ANTITHETIC and \
378
378
len (top_ps ) < len (perturbations ):
379
379
gradient *= 2
380
380
# Use the gradient ascent optimizer to compute the next parameters with the
@@ -396,7 +396,7 @@ class SklearnRegressionBlackboxOptimizer(StatefulOptimizer):
396
396
def __init__ (self ,
397
397
regression_method : RegressionType ,
398
398
regularizer : float ,
399
- est_type : EstimatorType ,
399
+ estimator_type : EstimatorType ,
400
400
normalize_fvalues : bool ,
401
401
hyperparameters_update_method : UpdateMethod ,
402
402
extra_params : Optional [Sequence [int ]],
@@ -422,8 +422,8 @@ def __init__(self,
422
422
else :
423
423
raise ValueError ('Optimization procedure option not available' )
424
424
self .gradient_ascent_optimizer = gradient_ascent_optimizer
425
- super ().__init__ (est_type , normalize_fvalues , hyperparameters_update_method ,
426
- extra_params )
425
+ super ().__init__ (estimator_type , normalize_fvalues ,
426
+ hyperparameters_update_method , extra_params )
427
427
428
428
def run_step (self , perturbations : FloatArray2D , function_values : FloatArray ,
429
429
current_input : FloatArray , current_value : float ) -> FloatArray :
@@ -439,11 +439,11 @@ def run_step(self, perturbations: FloatArray2D, function_values: FloatArray,
439
439
440
440
matrix = None
441
441
b_vector = None
442
- if self .est_type == EstimatorType .FORWARD_FD :
442
+ if self .estimator_type == EstimatorType .FORWARD_FD :
443
443
matrix = np .array (perturbations )
444
444
b_vector = (
445
445
function_values - np .array ([current_value ] * len (function_values )))
446
- elif self .est_type == EstimatorType .ANTITHETIC :
446
+ elif self .estimator_type == EstimatorType .ANTITHETIC :
447
447
matrix = np .array (perturbations [::2 ])
448
448
function_even_values = np .array (function_values .tolist ()[::2 ])
449
449
function_odd_values = np .array (function_values .tolist ()[1 ::2 ])
@@ -495,20 +495,20 @@ def normalize_function_values(
495
495
496
496
497
497
def monte_carlo_gradient (precision_parameter : float ,
498
- est_type : EstimatorType ,
498
+ estimator_type : EstimatorType ,
499
499
perturbations : FloatArray2D ,
500
500
function_values : FloatArray ,
501
501
current_value : float ,
502
502
energy : Optional [float ] = 0 ) -> FloatArray :
503
503
"""Calculates Monte Carlo gradient.
504
504
505
505
There are several ways of estimating the gradient. This is specified by the
506
- attribute self.est_type . Currently, forward finite difference (FFD) and
506
+ attribute self.estimator_type . Currently, forward finite difference (FFD) and
507
507
antithetic are supported.
508
508
509
509
Args:
510
510
precision_parameter: sd of Gaussian perturbations
511
- est_type : 'forward_fd' (FFD) or 'antithetic'
511
+ estimator_type : 'forward_fd' (FFD) or 'antithetic'
512
512
perturbations: the simulated perturbations
513
513
function_values: reward from perturbations (possibly normalized)
514
514
current_value: estimated reward at current point (possibly normalized)
@@ -522,11 +522,11 @@ def monte_carlo_gradient(precision_parameter: float,
522
522
"""
523
523
dim = len (perturbations [0 ])
524
524
b_vector = None
525
- if est_type == EstimatorType .FORWARD_FD :
525
+ if estimator_type == EstimatorType .FORWARD_FD :
526
526
b_vector = (function_values -
527
527
np .array ([current_value ] * len (function_values ))) / (
528
528
precision_parameter * precision_parameter )
529
- elif est_type == EstimatorType .ANTITHETIC :
529
+ elif estimator_type == EstimatorType .ANTITHETIC :
530
530
b_vector = function_values / (2.0 * precision_parameter *
531
531
precision_parameter )
532
532
else :
@@ -543,15 +543,15 @@ def monte_carlo_gradient(precision_parameter: float,
543
543
return gradient
544
544
545
545
546
- def sklearn_regression_gradient (clf : LinearModel , est_type : EstimatorType ,
546
+ def sklearn_regression_gradient (clf : LinearModel , estimator_type : EstimatorType ,
547
547
perturbations : FloatArray2D ,
548
548
function_values : FloatArray ,
549
549
current_value : float ) -> FloatArray :
550
550
"""Calculates gradient by function difference regression.
551
551
552
552
Args:
553
553
clf: an object (SkLearn linear model) which fits Ax = b
554
- est_type : 'forward_fd' (FFD) or 'antithetic'
554
+ estimator_type : 'forward_fd' (FFD) or 'antithetic'
555
555
perturbations: the simulated perturbations
556
556
function_values: reward from perturbations (possibly normalized)
557
557
current_value: estimated reward at current point (possibly normalized)
@@ -565,11 +565,11 @@ def sklearn_regression_gradient(clf: LinearModel, est_type: EstimatorType,
565
565
matrix = None
566
566
b_vector = None
567
567
dim = perturbations [0 ].size
568
- if est_type == EstimatorType .FORWARD_FD :
568
+ if estimator_type == EstimatorType .FORWARD_FD :
569
569
matrix = np .array (perturbations )
570
570
b_vector = (
571
571
function_values - np .array ([current_value ] * len (function_values )))
572
- elif est_type == EstimatorType .ANTITHETIC :
572
+ elif estimator_type == EstimatorType .ANTITHETIC :
573
573
matrix = np .array (perturbations [::2 ])
574
574
function_even_values = np .array (function_values .tolist ()[::2 ])
575
575
function_odd_values = np .array (function_values .tolist ()[1 ::2 ])
@@ -903,14 +903,14 @@ class TrustRegionOptimizer(StatefulOptimizer):
903
903
schedule that would have to be tuned.
904
904
"""
905
905
906
- def __init__ (self , precision_parameter : float , est_type : EstimatorType ,
906
+ def __init__ (self , precision_parameter : float , estimator_type : EstimatorType ,
907
907
normalize_fvalues : bool ,
908
908
hyperparameters_update_method : UpdateMethod ,
909
909
extra_params : Optional [Sequence [int ]], tr_params : Mapping [str ,
910
910
Any ]):
911
911
self .precision_parameter = precision_parameter
912
- super ().__init__ (est_type , normalize_fvalues , hyperparameters_update_method ,
913
- extra_params )
912
+ super ().__init__ (estimator_type , normalize_fvalues ,
913
+ hyperparameters_update_method , extra_params )
914
914
915
915
self .accepted_quadratic_model = None
916
916
self .accepted_function_value = None
@@ -1147,12 +1147,12 @@ def update_quadratic_model(self, perturbations: FloatArray2D,
1147
1147
current_value = normalized_values [1 ]
1148
1148
self .normalized_current_value = current_value
1149
1149
if self .params ['grad_type' ] == GradientType .REGRESSION :
1150
- new_gradient = sklearn_regression_gradient (self .clf , self .est_type ,
1150
+ new_gradient = sklearn_regression_gradient (self .clf , self .estimator_type ,
1151
1151
perturbations , function_values ,
1152
1152
current_value )
1153
1153
else :
1154
1154
new_gradient = monte_carlo_gradient (self .precision_parameter ,
1155
- self .est_type , perturbations ,
1155
+ self .estimator_type , perturbations ,
1156
1156
function_values , current_value )
1157
1157
new_gradient *= - 1 # TR subproblem solver performs minimization
1158
1158
if not is_update :
0 commit comments