-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from franneck94/dev
Dev
- Loading branch information
Showing
11 changed files
with
377 additions
and
297 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
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,3 +1 @@ | ||
from .model_selection import GridSearch | ||
from .model_selection import RandomSearch | ||
from .version import __version__ |
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,199 +1,12 @@ | ||
from typing import Any | ||
from typing import Callable | ||
from typing import Dict | ||
from typing import Mapping | ||
from .search import GridSearch | ||
from .search import RandomSearch | ||
|
||
import numpy as np | ||
import tensorflow as tf | ||
from sklearn.model_selection import ParameterGrid | ||
from sklearn.model_selection import ParameterSampler | ||
|
||
# TODO: Import GridSearchCV and RandomSearchCV when finished | ||
|
||
class BaseSearch: | ||
"""RandomSearch for a given parameter distribution. | ||
|
||
Args: | ||
model_fn (Callable): Function that builds and compiles a | ||
tf.keras.Model or tf.keras.Sequential object. | ||
verbose (int, optional): Whether to show information in terminal. | ||
Defaults to 0. | ||
kwargs (Any): Keyword arguments for the model_fn function. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
model_fn: Callable, | ||
verbose: int = 0, | ||
**kwargs: Any | ||
) -> None: | ||
self.model_fn = model_fn | ||
self.verbose = verbose | ||
self.model_fn_kwargs = kwargs | ||
self.results_ = { | ||
"best_score": -np.inf, | ||
"best_params": {}, | ||
"val_scores": [], | ||
"params": [], | ||
} | ||
|
||
def summary(self) -> None: | ||
"""Prints the summary of the search to the console. | ||
""" | ||
best_params_str = (f"Best score: {self.results_['best_score']} " | ||
f"using params: {self.results_['best_params']}") | ||
dashed_line = "".join(map(lambda x: "-", best_params_str)) | ||
print(f"\n{dashed_line}\n{best_params_str}\n{dashed_line}") | ||
|
||
scores = self.results_["val_scores"] | ||
params = self.results_["params"] | ||
|
||
for idx, (score, param) in enumerate(zip(scores, params)): | ||
print(f"Idx: {idx} - Score: {score} with param: {param}") | ||
|
||
print(f"{dashed_line}\n") | ||
|
||
|
||
class GridSearch(BaseSearch): | ||
def __init__( | ||
self, | ||
model_fn: Callable, | ||
parameter_grid: Mapping, | ||
n_iter: int = 10, | ||
verbose: int = 0, | ||
**kwargs: Any | ||
) -> None: | ||
"""RandomSearch for a given parameter distribution. | ||
Args: | ||
model_fn (Callable): Function that builds and compiles a | ||
tf.keras.Model or tf.keras.Sequential object. | ||
parameter_grid (Dict[str, Iterable]): Dict of str, iterable | ||
hyperparameter, where the str is the parameter name of the. | ||
n_iter (int, optional): Number of random models. Defaults to 10. | ||
verbose (int, optional): Whether to show information in terminal. | ||
Defaults to 0. | ||
kwargs (Any): Keyword arguments for the model_fn function. | ||
""" | ||
super().__init__( | ||
model_fn, | ||
verbose, | ||
**kwargs | ||
) | ||
self.parameter_grid = ParameterGrid(parameter_grid) | ||
self.n_iter = n_iter | ||
|
||
def fit( | ||
self, | ||
train_dataset: tf.data.Dataset, | ||
val_dataset: tf.data.Dataset, | ||
**kwargs: Any | ||
) -> None: | ||
"""Runs the exhaustive grid search over the parameter grid. | ||
Args: | ||
train_dataset (tf.data.Dataset): tf.data.Dataset object for the | ||
training. | ||
val_dataset (tf.data.Dataset): tf.data.Dataset object for | ||
the validation. | ||
kwargs (Any): Keyword arguments for the fit method of the | ||
tf.keras.models.Model or tf.keras.models.Sequential model. | ||
""" | ||
for idx, grid_combination in enumerate(self.parameter_grid): | ||
if self.verbose: | ||
print(f"Running Comb: {idx}") | ||
model = self.model_fn( | ||
**grid_combination, | ||
**self.model_fn_kwargs | ||
) | ||
|
||
model.fit( | ||
train_dataset, | ||
validation_data=val_dataset, | ||
**kwargs, | ||
) | ||
|
||
val_metric = model.evaluate( | ||
val_dataset, | ||
verbose=0 | ||
)[1] | ||
self.results_["val_scores"].append(val_metric) | ||
self.results_["params"].append(grid_combination) | ||
|
||
best_run_idx = np.argmax(self.results_["val_scores"]) | ||
self.results_["best_score"] = self.results_["val_scores"][best_run_idx] | ||
self.results_["best_params"] = self.results_["params"][best_run_idx] | ||
|
||
|
||
class RandomSearch(BaseSearch): | ||
def __init__( | ||
self, | ||
model_fn: Callable, | ||
param_distributions: Dict[str, Callable], | ||
n_iter: int = 10, | ||
verbose: int = 0, | ||
**kwargs: Any | ||
) -> None: | ||
"""RandomSearch for a given parameter distribution. | ||
Args: | ||
model_fn (Callable): Function that builds and compiles a | ||
tf.keras.Model or tf.keras.Sequential object. | ||
param_distributions (Dict[str, Callable]): Dict of str, callable | ||
pairs, where the str is the parameter name of the. | ||
n_iter (int, optional): Number of random models. Defaults to 10. | ||
verbose (int, optional): Whether to show information in terminal. | ||
Defaults to 0. | ||
kwargs (Any): Keyword arguments for the model_fn function. | ||
""" | ||
super().__init__( | ||
model_fn, | ||
verbose, | ||
**kwargs | ||
) | ||
self.param_distributions = param_distributions | ||
self.n_iter = n_iter | ||
self.random_sampler = ParameterSampler( | ||
self.param_distributions, | ||
n_iter=self.n_iter | ||
) | ||
|
||
def fit( | ||
self, | ||
train_dataset: tf.data.Dataset, | ||
val_dataset: tf.data.Dataset, | ||
**kwargs: Any | ||
) -> None: | ||
"""Runs the random search over the parameter distributions. | ||
Args: | ||
train_dataset (tf.data.Dataset): tf.data.Dataset object for the | ||
training. | ||
val_dataset (tf.data.Dataset): tf.data.Dataset object for | ||
the validation. | ||
kwargs (Any): Keyword arguments for the fit method of the | ||
tf.keras.models.Model or tf.keras.models.Sequential model. | ||
""" | ||
for idx, random_combination in enumerate(self.random_sampler): | ||
if self.verbose: | ||
print(f"Running Comb: {idx}") | ||
model = self.model_fn( | ||
**random_combination, | ||
**self.model_fn_kwargs | ||
) | ||
|
||
model.fit( | ||
train_dataset, | ||
validation_data=val_dataset, | ||
**kwargs, | ||
) | ||
|
||
val_metric = model.evaluate( | ||
val_dataset, | ||
verbose=0 | ||
)[1] | ||
self.results_["val_scores"].append(val_metric) | ||
self.results_["params"].append(random_combination) | ||
|
||
best_run_idx = np.argmax(self.results_["val_scores"]) | ||
self.results_["best_score"] = self.results_["val_scores"][best_run_idx] | ||
self.results_["best_params"] = self.results_["params"][best_run_idx] | ||
__all__ = [ | ||
"GridSearch", | ||
"RandomSearch" | ||
# TODO: Add GridSearchCV and RandomSearchCV when finished | ||
] |
Oops, something went wrong.