-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for Bayesian Optimization Algo (#406)
* added tests for acquisition function and models * added tests for global_optimizer * added tests for boa * minor linting * tests for algorithm manager * added discrete parameter to study config * covered all parameter types * moved python script to testing folder * added python tests to unit tests * remembered to uncomment existing tests * fixed path to test script * moved python tests to separate job in workflow * added run command to test script
- Loading branch information
1 parent
61451ef
commit c87d583
Showing
25 changed files
with
419 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
pkg/suggestion/bayesianoptimization/src/acquisition_func.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" module for acquisition function""" | ||
import numpy as np | ||
from scipy.stats import norm | ||
|
||
|
||
class AcquisitionFunc: | ||
""" | ||
Class for acquisition function with options for expected improvement, | ||
probability of improvement, or lower confident bound. | ||
""" | ||
|
||
def __init__(self, model, current_optimal, mode="ei", trade_off=0.01): | ||
""" | ||
:param mode: pi: probability of improvement, ei: expected improvement, lcb: lower confident bound | ||
:param trade_off: a parameter to control the trade off between exploiting and exploring | ||
:param model_type: gp: gaussian process, rf: random forest | ||
""" | ||
self.model = model | ||
self.current_optimal = current_optimal | ||
self.mode = mode | ||
self.trade_off = trade_off | ||
|
||
def compute(self, X_test): | ||
y_mean, y_std, y_variance = self.model.predict(X_test) | ||
|
||
z = (y_mean - self.current_optimal - self.trade_off) / y_std | ||
|
||
if self.mode == "ei": | ||
if y_std.any() < 0.000001: | ||
return 0, y_mean, y_variance | ||
result = y_std * (z * norm.cdf(z) + norm.pdf(z)) | ||
elif self.mode == "pi": | ||
result = norm.cdf(z) | ||
else: | ||
result = - (y_mean - self.trade_off * y_std) | ||
return np.squeeze(result), np.squeeze(y_mean), np.squeeze(y_variance) |
61 changes: 0 additions & 61 deletions
61
pkg/suggestion/bayesianoptimization/src/acquisition_func/acquisition_func.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import numpy as np | ||
import forestci as fci | ||
from sklearn.ensemble import RandomForestRegressor | ||
|
||
|
||
class RandomForestModel: | ||
def __init__(self, n_estimators, max_features): | ||
n_estimators = n_estimators or 50 | ||
max_features = max_features or "auto" | ||
|
||
def __init__(self, n_estimators=50, max_features="auto"): | ||
self.rf = RandomForestRegressor( | ||
n_estimators=n_estimators, | ||
max_features=max_features, | ||
) | ||
self.X_train = None | ||
|
||
def fit(self, X_train, y_train): | ||
print(X_train.shape, y_train.shape) | ||
self.X_train = X_train | ||
self.rf.fit(X_train, y_train) | ||
|
||
def predict(self, X_test): | ||
y_mean = self.rf.predict(X_test) | ||
y_variance = fci.random_forest_error(self.rf, self.X_train, X_test) | ||
y_std = np.sqrt(y_variance) | ||
return y_mean, y_std, y_variance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
import logging | ||
from logging import getLogger, StreamHandler | ||
|
||
|
||
FORMAT = '%(asctime)-15s StudyID %(studyid)s %(message)s' | ||
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO") | ||
|
||
|
||
def get_logger(name=__name__): | ||
logger = getLogger(name) | ||
logging.basicConfig(format=FORMAT) | ||
handler = StreamHandler() | ||
logger.setLevel(LOG_LEVEL) | ||
logger.addHandler(handler) | ||
logger.propagate = False | ||
return logger |
Oops, something went wrong.