Brute force hhyperparameters

This commit is contained in:
Matthias Guillitte
2026-09-25 17:24:13 +02:00
parent c5f95c3c1e
commit ecf749a7d8
2 changed files with 45 additions and 18 deletions

View File

@@ -38,18 +38,22 @@ def create_split(x, y, game_ids):
if __name__ == "__main__":
model_path = "C:/Users/Laurent/Desktop/chess-engine/src/training/decision_tree/cache/model.joblib"
model_path = "./src/training/decision_tree/cache/model.joblib"
if os.path.exists(model_path):
model = Model.load(model_path)
else:
x, y, game_ids = preprocessor.run("C:/Users/Laurent/Desktop/chess-engine/dataset/v1/output.csv")
x, y, game_ids = preprocessor.run("./dataset/output.csv")
split = create_split(x, y, game_ids)
depth = 35
leafs = int(depth * 3.5)
trained_model = []
is_hyperparameter_tuning = True
for depth in range(1, 10):
for leafs in range(1, int(depth * 5)):
print(f"Training model with depth={depth} and leafs={leafs}")
model = Model(
tree=RandomForestRegressor(
@@ -64,7 +68,28 @@ if __name__ == "__main__":
)
model.train()
model.evaluate()
[train_mae, train_mse, test_mae, test_mse] = model.evaluate()
# Verify it's not overfitting
if test_mae > 1.5 * train_mae:
print("Model is overfitting, skipping saving...")
else:
trained_model.append({
"depth": depth,
"leafs": leafs,
"train_mae": train_mae,
"train_mse": train_mse,
"test_mae": test_mae,
"test_mse": test_mse
})
best_model = min(trained_model, key=lambda x: x["test_mae"])
print("Best model so far:")
print("MAE:", best_model["test_mae"])
print("MSE:", best_model["test_mse"])
#model.save_model(model_path)
visualizer = Visualizer(model=model)

View File

@@ -65,6 +65,8 @@ class Model:
print("MAE:", test_mae)
print("MSE:", test_mse)
return [train_mae, train_mse, test_mae, test_mse]
def save_model(self, path):
joblib.dump({