-
Notifications
You must be signed in to change notification settings - Fork 193
Add Example for Comet #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
86ba2a4
Add Initial Example for Comet.
ParagEkbote 8403704
Add example for comet callback and update
ParagEkbote 14564ca
Update example.
ParagEkbote 7388076
Update training example.
ParagEkbote 4a781db
update CI for example.
ParagEkbote 54d0351
make style.
ParagEkbote 5f70a5d
Update example and CI file.
ParagEkbote e4af51e
Update Example.
ParagEkbote d04be01
Merge branch 'optuna:main' into Add-Comet-Example
ParagEkbote 85eea17
Update example.
ParagEkbote 0c991a4
update examples.
ParagEkbote bb05145
update ci file.
ParagEkbote 68a4c2c
Update CI file to run offline.
ParagEkbote ec5b6ef
Remove api key requirements.
ParagEkbote 580a35b
update example.
ParagEkbote fec039c
make style.
ParagEkbote d17c70c
Remove CI file.
ParagEkbote 126cd12
Update README.md as per code review
ParagEkbote 26013a2
Update Example.
ParagEkbote 74650b7
update example.
ParagEkbote 9f26b53
Apply suggestions as per code review
ParagEkbote 37ddecf
Update example file.
ParagEkbote 81f7e91
rename file.
ParagEkbote 4b7fb6c
Apply suggestions from code review-2
ParagEkbote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """ | ||
| This script integrates Comet ML and Optuna to optimize a Random Forest Classifier | ||
| on the scikit-learn Breast Cancer dataset. It performs the following steps: | ||
|
|
||
| 1. Initializes a Comet ML experiment for logging. | ||
| 2. Loads the Breast Cancer dataset and splits it into training and testing sets. | ||
| 3. Defines an evaluation function using F1-score, precision, and recall. | ||
| 4. Implements an Optuna objective function to optimize hyperparameters | ||
| (n_estimators and max_depth) for the Random Forest model. | ||
ParagEkbote marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 5. Uses Optuna to run multiple trials and identify the best hyperparameters. | ||
| 6. Trains the final Random Forest model using the best-found hyperparameters. | ||
| 7. Logs training and testing metrics to Comet ML. | ||
|
|
||
| You can run this example as follows: | ||
| $ python comet_integration.py | ||
| """ | ||
|
|
||
| import optuna | ||
| from optuna_integration.comet import CometCallback | ||
|
|
||
| from sklearn.datasets import load_iris | ||
| from sklearn.ensemble import RandomForestClassifier | ||
| from sklearn.metrics import accuracy_score | ||
| from sklearn.model_selection import train_test_split | ||
|
|
||
|
|
||
| def objective(trial): | ||
| """Objective function for optimizing a RandomForestClassifier using Optuna.""" | ||
| data = load_iris() | ||
| x_train, x_valid, y_train, y_valid = train_test_split( | ||
| data["data"], data["target"], random_state=42 | ||
| ) | ||
| params = { | ||
| "min_samples_leaf": trial.suggest_int("min_samples_leaf", 2, 10), | ||
| "max_depth": trial.suggest_int("max_depth", 5, 20), | ||
| "min_samples_split": trial.suggest_int("min_samples_split", 2, 10), | ||
| } | ||
|
|
||
| clf = RandomForestClassifier(**params, random_state=42) | ||
| clf.fit(x_train, y_train) | ||
| pred = clf.predict(x_valid) | ||
| score = accuracy_score(y_valid, pred) | ||
|
|
||
| return score | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| study = optuna.create_study(direction="maximize") | ||
| comet_callback = CometCallback(study, project_name="comet-optuna-sklearn-example") | ||
|
|
||
| study.optimize(objective, n_trials=20, callbacks=[comet_callback]) | ||
|
|
||
| print(f"Number of finished trials: {len(study.trials)}\n") | ||
|
|
||
| print("Best trial:") | ||
| trial = study.best_trial | ||
|
|
||
| print(f" Value: {trial.value}\n") | ||
| print(" Params:") | ||
| for key, value in trial.params.items(): | ||
| print(f" {key}: {value}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| comet-ml | ||
| optuna | ||
| optuna-integration | ||
| scikit-learn |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.