new version of optimization_demo with comments
@@ -3,27 +3,24 @@ import os
os.chdir(os.path.pardir)
os.chdir(os.path.pardir)
-
+#%% load the libraries
from models.LDA import LDA_Model
from dataset.dataset import Dataset
from optimization.optimizer import Optimizer
from skopt.space.space import Real
from evaluation_metrics.coherence_metrics import Coherence
-import time
-
-
-# Load dataset
+#%% Load dataset
dataset = Dataset()
dataset.load("preprocessed_datasets/M10/M10_lemmatized_0")
-# Load model
+#%% Load model
model = LDA_Model()
-# Set model hyperparameters (not optimized by BO)
+#%% Set model hyperparameters (not optimized by BO)
model.hyperparameters.update({ "num_topics": 25, "iterations": 200 })
model.partitioning(False)
-# Choose of the metric function to optimize
+#%% Choose of the metric function to optimize
metric_parameters = {
'texts': dataset.get_corpus(),
'topk': 10,
@@ -31,7 +28,7 @@ metric_parameters = {
}
npmi = Coherence(metric_parameters)
-# Create search space for optimization
+#%% Create search space for optimization
search_space = {
"alpha": Real(low=0.001, high=5.0),
"eta": Real(low=0.001, high=5.0)
@@ -45,26 +42,26 @@ optimizer = Optimizer(
search_space,
plot_model=True,
plot_best_seen=True,
- save_path="results/simple_RF/",
+ save_path="results/simple_GP/",
save_name="resultsBO",
save_models=False,
- number_of_call=5,
+ number_of_call=6,
n_random_starts=3,
optimization_type='Maximize',
model_runs=5,
initial_point_generator="random", #work only for version skopt 8.0
- surrogate_model="RF")
+ surrogate_model="GP")
-# Optimize the function npmi using Bayesian Optimization
-start_time = time.time()
+#%% Optimize the function npmi using Bayesian Optimization
BestObject = optimizer.optimize()
-end_time = time.time()
-total_time = end_time - start_time # Total time to optimize
-#%%Restart
-PreviousResult=BestObject.load("results/simple_RF/resultsBO.json")
+#%% Save the results to a csv
+BestObject.save_to_csv("results.csv")
+
+#%%To restart an optimization you must load the previous results
+PreviousResult=BestObject.load("results/simple_GP/resultsBO.json")
-# Initialize optimizer
+#%% Initialize again the optimizer
optimizer = Optimizer(
model,
dataset,
@@ -72,16 +69,13 @@ optimizer = Optimizer(
search_space,
plot_model=True,
plot_best_seen=True,
- save_path="results/simple_RF/",
+ save_path="results/simple_GP/",
save_name="resultsBO2",
save_models=False,
- number_of_call=5,
+ number_of_call=6,
optimization_type='Maximize',
- model_runs=5,
+ model_runs=2,
surrogate_model="GP")
-# # #..and launch again the optimization
-start_time = time.time()
-BestObject2 = optimizer.restart_optimize(PreviousResult.copy())
-end_time = time.time()
-total_time = end_time - start_time # Total time to optimize
\ No newline at end of file
+#%%..and launch again the optimization
+BestObject2 = optimizer.restart_optimize(PreviousResult)
