Brute force hhyperparameters
This commit is contained in:
@@ -38,33 +38,58 @@ def create_split(x, y, game_ids):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
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):
|
if os.path.exists(model_path):
|
||||||
model = Model.load(model_path)
|
model = Model.load(model_path)
|
||||||
|
|
||||||
else:
|
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)
|
split = create_split(x, y, game_ids)
|
||||||
|
|
||||||
|
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}")
|
||||||
|
|
||||||
depth = 35
|
model = Model(
|
||||||
leafs = int(depth * 3.5)
|
tree=RandomForestRegressor(
|
||||||
|
n_estimators=25,
|
||||||
|
max_depth=depth,
|
||||||
|
max_features=0.35,
|
||||||
|
min_samples_leaf=leafs,
|
||||||
|
random_state=42
|
||||||
|
),
|
||||||
|
feature_names=preprocessor.get_feature_names(),
|
||||||
|
train_test_split=split
|
||||||
|
)
|
||||||
|
|
||||||
model = Model(
|
model.train()
|
||||||
tree=RandomForestRegressor(
|
[train_mae, train_mse, test_mae, test_mse] = model.evaluate()
|
||||||
n_estimators=25,
|
|
||||||
max_depth=depth,
|
# Verify it's not overfitting
|
||||||
max_features=0.35,
|
if test_mae > 1.5 * train_mae:
|
||||||
min_samples_leaf=leafs,
|
print("Model is overfitting, skipping saving...")
|
||||||
random_state=42
|
else:
|
||||||
),
|
trained_model.append({
|
||||||
feature_names=preprocessor.get_feature_names(),
|
"depth": depth,
|
||||||
train_test_split=split
|
"leafs": leafs,
|
||||||
)
|
"train_mae": train_mae,
|
||||||
|
"train_mse": train_mse,
|
||||||
model.train()
|
"test_mae": test_mae,
|
||||||
model.evaluate()
|
"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)
|
#model.save_model(model_path)
|
||||||
|
|
||||||
visualizer = Visualizer(model=model)
|
visualizer = Visualizer(model=model)
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ class Model:
|
|||||||
print("Test:")
|
print("Test:")
|
||||||
print("MAE:", test_mae)
|
print("MAE:", test_mae)
|
||||||
print("MSE:", test_mse)
|
print("MSE:", test_mse)
|
||||||
|
|
||||||
|
return [train_mae, train_mse, test_mae, test_mse]
|
||||||
|
|
||||||
|
|
||||||
def save_model(self, path):
|
def save_model(self, path):
|
||||||
|
|||||||
Reference in New Issue
Block a user