Skip to content

Instantly share code, notes, and snippets.

@alegoritma
Created September 11, 2021 10:48
Show Gist options
  • Select an option

  • Save alegoritma/fa1757cc41db71cba14953a96582f7ad to your computer and use it in GitHub Desktop.

Select an option

Save alegoritma/fa1757cc41db71cba14953a96582f7ad to your computer and use it in GitHub Desktop.
"""
requirements:
scikit-optimize
"""
from skopt import gbrt_minimize, gp_minimize, callbacks, load
from skopt.utils import use_named_args
from skopt.space import Real, Categorical, Integer
from functools import partial
"""
dimensions listesi evrenimizdeki hyperparametreleri içerir.
Bu hyperparametre boyutları gerçek sayı aralığı, kategorik değerler ya da
tam sayı aralığı olabilir. Bu olası değerler, ilgili kütüphanede sırasıyla
Real, Categorical ve Integer classları ile belirlenir.
Her biri için açıklamalar linkte bulunabilir:
https://scikit-optimize.github.io/stable/modules/classes.html#module-skopt.space.space
"""
dimensions = [
Real(0.001, 0.999, name="real param"),
Categorical(categories=["some_category", 1, 2], name="categorical param"),
Integer(1, 10, name="int param")
]
@use_named_args(dimensions=dimensions)
def f(**params):
"""
Burası hyperparametre uzayının bir bölgesindeki sonucu verecek olan fonksiyondur.
Fonksiyonun döndürdüğü skora bağlı olarak muhtemel en düşük sonucu veren bölge
tahmin edilmeye çalışılır. Bu bağlamda fonksiyonun döndüreceği sonuç
"en düşük en iyi" prensibinde olmalıdır. Örneğin bu eğitim sonucunda elde edilen
[validation loss] ya da 1-[validation accuracy] olabilir.
@use_named_args isimli wrapper, bu fonksiyona verilecek
parametreleri isimleri ile verir. Örneğin dimensions listesinde
bulunan Integer(5, 10, name="hidden_layer_count") boyutunun iterasyondaki
konumu bu fonksiyona hidden_layer_count parametresi olarak verilecektir.
Böylece fonksiyon f(hidden_layer_count, **params) şeklinde yazılıp
değişken doğrudan kullanılabilir ya da
hidden_layer_count = params["hidden_layer_count"]
şeklinde de kullanılabilir.
"""
def load_checkpoint(ckpt_path):
try:
res = load(OPT_CHKPOINT_PATH)
x0 = res.x_iters
y0 = res.func_vals
print("Continuing to old tuner search... Total iters done before:", len(x0))
except FileNotFoundError:
print("Starting a new tuning from beginning...")
x0 = None
y0 = None
return x0, y0
if __name__ == '__main__':
TUNER_VERSION_NAME = "v1"
OPT_CHKPOINT_PATH = f"ckpt-{TUNER_VERSION_NAME}.pkl"
checkpoint_callback = callbacks.CheckpointSaver(OPT_CHKPOINT_PATH)
x0, y0 = load_checkpoint(OPT_CHKPOINT_PATH )
gp_result = gp_minimize(
func=f,
dimensions=dimensions,
n_calls=1000,
x0=x0, y0=y0,
callback=[checkpoint_callback])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment