-
Notifications
You must be signed in to change notification settings - Fork 289
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
[ADD] Optimise on custom metric #486
Open
ravinkohli
wants to merge
5
commits into
automl:development
Choose a base branch
from
ravinkohli:add_custom_metric
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
552d3fc
add_metric and fix precommit
ravinkohli 989cac4
add test case for add metric
ravinkohli 8ac0564
add example for add metric
ravinkohli 3d0afa7
fix bug with importlib_metadata package version
ravinkohli 1b1f07b
silly bug in passing version
ravinkohli 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,96 @@ | ||
""" | ||
====================== | ||
Tabular Classification | ||
====================== | ||
|
||
The following example shows how to optimize | ||
AutoPyTorch on a custom metric | ||
""" | ||
import os | ||
import tempfile as tmp | ||
import warnings | ||
|
||
os.environ['JOBLIB_TEMP_FOLDER'] = tmp.gettempdir() | ||
os.environ['OMP_NUM_THREADS'] = '1' | ||
os.environ['OPENBLAS_NUM_THREADS'] = '1' | ||
os.environ['MKL_NUM_THREADS'] = '1' | ||
|
||
warnings.simplefilter(action='ignore', category=UserWarning) | ||
warnings.simplefilter(action='ignore', category=FutureWarning) | ||
|
||
import sklearn.datasets | ||
import sklearn.model_selection | ||
|
||
from autoPyTorch.api.tabular_classification import TabularClassificationTask | ||
from autoPyTorch.metrics import CLASSIFICATION_METRICS | ||
from autoPyTorch.pipeline.components.training.metrics.base import make_metric | ||
from autoPyTorch.pipeline.components.training.metrics.utils import add_metric | ||
|
||
|
||
############################################################################ | ||
# Data Loading | ||
# ============ | ||
X, y = sklearn.datasets.fetch_openml(data_id=40981, return_X_y=True, as_frame=True) | ||
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split( | ||
X, | ||
y, | ||
random_state=1, | ||
) | ||
|
||
|
||
############################################################################### | ||
# Define and add custom score function | ||
# ==================================== | ||
def score_function(y_test, y_pred): | ||
return sum(y_pred==y_test) / y_pred.shape[0] | ||
|
||
print("#"*80) | ||
print(f"Current metrics available for classification: {list(CLASSIFICATION_METRICS.keys())}") | ||
custom_metric = make_metric(name="custom_metric", score_func=score_function, worst_possible_result=0, greater_is_better=True) | ||
|
||
add_metric(metric=custom_metric, task_type="tabular_classification") | ||
print("#"*80) | ||
print(f"Metrics available for classification after adding custom metric: {list(CLASSIFICATION_METRICS.keys())}") | ||
|
||
|
||
############################################################################ | ||
# Build and fit a classifier | ||
# ========================== | ||
api = TabularClassificationTask( | ||
# To maintain logs of the run, you can uncomment the | ||
# Following lines | ||
# temporary_directory='./tmp/autoPyTorch_example_tmp_01', | ||
# output_directory='./tmp/autoPyTorch_example_out_01', | ||
# delete_tmp_folder_after_terminate=False, | ||
# delete_output_folder_after_terminate=False, | ||
seed=42, | ||
) | ||
|
||
###################################################################################### | ||
# Search for an ensemble of machine learning algorithms optimised on the custom metric | ||
# ==================================================================================== | ||
api.search( | ||
X_train=X_train, | ||
y_train=y_train, | ||
X_test=X_test.copy(), | ||
y_test=y_test.copy(), | ||
dataset_name='Australian', | ||
optimize_metric='custom_metric', | ||
total_walltime_limit=300, | ||
func_eval_time_limit_secs=50, | ||
memory_limit=None, | ||
) | ||
|
||
############################################################################ | ||
# Print the final ensemble performance | ||
# ==================================== | ||
|
||
y_pred = api.predict(X_test) | ||
score = api.score(y_pred, y_test) | ||
print(score) | ||
|
||
# Print statistics from search | ||
print(api.sprint_statistics()) | ||
|
||
# Print the final ensemble built by AutoPyTorch | ||
print(api.show_models()) |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Time Series Tasks, an additional kwargs must be attached:
https://github.com/sktime/sktime/blob/main/sktime/performance_metrics/forecasting/_functions.py#L179
Should we create a new example for this, or do we add another docstring here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add it to the same example. We can divide this example into two parts- for tabular tasks and for time series tasks