Skip to content

Commit

Permalink
Merge pull request #547 from zurk/feauture/opt-logs
Browse files Browse the repository at this point in the history
Log optimizer verbose messages
  • Loading branch information
vmarkovtsev committed Jan 23, 2019
2 parents a3e1ff1 + 1474c76 commit a7589b2
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lookout/style/format/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import partial
from logging import getLogger
from threading import Thread
import time
from typing import Any, Mapping, Optional, Sequence, Tuple

from lookout.core.slogging import logs_are_structured
Expand Down Expand Up @@ -72,8 +73,9 @@ def optimize(self, X: csr_matrix, y: numpy.ndarray) -> Tuple[float, Mapping[str,
cost_function = use_named_args(self.dimensions)(partial(self._cost, X=X, y=y))

def _minimize() -> OptimizeResult:
callback = _VerboseLogCallback(self._log)
return gp_minimize(cost_function, self.dimensions, n_calls=self.n_iter,
random_state=self.random_state, verbose=True)
random_state=self.random_state, callback=callback)

if not logs_are_structured:
# fool the check in joblib - everything still works without it
Expand Down Expand Up @@ -101,3 +103,35 @@ def _cost(self, *, X: csr_matrix, y: numpy.ndarray, **params: Any) -> float:
base_model = base_model_class(**params_copy)
cv = StratifiedKFold(self.cv, random_state=self.random_state)
return -numpy.mean(cross_val_score(base_model, X, y, cv=cv, n_jobs=self.n_jobs))


class _VerboseLogCallback:
"""
Callback to control the verbosity and log output properly.
Adopted from skopt library, VerboseCallback class.
"""

def __init__(self, log):
"""
Init method.
:param log: logger Instance to log optimization steps.
"""
self._log = log
self.iter_no = 1
self._start_time = time.time()

def __call__(self, res):
"""
Call callback method.
:param res: The optimization as a OptimizeResult object.
:return: None
"""
self._log.debug(
"Iteration No: %.3d. Time taken: %0.4f. Function value obtained: %0.4f. Current"
" minimum: %0.4f.", self.iter_no, time.time() - self._start_time, res.func_vals[-1],
res.fun)
self.iter_no += 1
self._start_time = time.time()

0 comments on commit a7589b2

Please sign in to comment.