Update hyperopt.py

This commit is contained in:
Italo 2022-02-05 17:36:19 +00:00
parent ef03f2f3d2
commit 992eac9efa

View File

@ -50,7 +50,7 @@ progressbar.streams.wrap_stdout()
logger = logging.getLogger(__name__)
INITIAL_POINTS = 64
INITIAL_POINTS = 32
# Keep no more than SKOPT_MODEL_QUEUE_SIZE models
# in the skopt model queue, to optimize memory consumption
@ -532,7 +532,8 @@ class Hyperopt:
# a chance to be evaluated.
print("No epochs evaluated yet, no best result.")
def plot_mse(self, res, ax, jobs):
def plot_mse(self, res, ax, jobs):
from sklearn.model_selection import cross_val_score
if len(res.x_iters) < 10:
return
@ -540,19 +541,26 @@ class Hyperopt:
self.mse_list = []
model = clone(res.models[-1])
i_subset = random.sample(range(len(res.x_iters)), 100) if len(res.x_iters) > 100 else range(len(res.x_iters))
# i_subset = random.sample(range(len(res.x_iters)), 100) if len(res.x_iters) > 100 else range(len(res.x_iters))
i_train = random.sample(i_subset, round(.8*len(i_subset))) # get 80% random indices
x_train = [x for i, x in enumerate(res.x_iters) if i in i_train]
y_train = [y for i, y in enumerate(res.func_vals) if i in i_train]
# i_train = random.sample(i_subset, round(.8*len(i_subset))) # get 80% random indices
# x_train = [x for i, x in enumerate(res.x_iters) if i in i_train]
# y_train = [y for i, y in enumerate(res.func_vals) if i in i_train]
i_test = [i for i in i_subset if i not in i_train] # get 20% random indices
x_test = [x for i, x in enumerate(res.x_iters) if i in i_test]
y_test = [y for i, y in enumerate(res.func_vals) if i in i_test]
model.fit(np.array(x_train), np.array(y_train))
y_pred, sigma = model.predict(np.array(x_test), return_std=True)
mse = np.mean((y_test - y_pred) ** 2)
self.mse_list.append(mse)
# i_test = [i for i in i_subset if i not in i_train] # get 20% random indices
# x_test = [x for i, x in enumerate(res.x_iters) if i in i_test]
# y_test = [y for i, y in enumerate(res.func_vals) if i in i_test]
model.fit(res.x_iters, res.func_vals)
# Perform a cross-validation estimate of the coefficient of determination using
# the cross_validation module using all CPUs available on the machine
# K = 5 # folds
R2 = cross_val_score(model, X=res.x_iters, y=res.func_vals, cv=5, n_jobs=jobs).mean()
print(f'R2: {R2}')
R2 = R2 if R2 > -5 else -5
self.mse_list.append(R2)
# y_pred, sigma = model.predict(np.array(x_test), return_std=True)
# mse = np.mean((y_test - y_pred) ** 2)
# self.mse_list.append(mse)
ax.plot(range(INITIAL_POINTS, INITIAL_POINTS + jobs * len(self.mse_list), jobs), self.mse_list, label='MSE', marker=".", markersize=12, lw=2)