2021-12-03 12:45:16 -05:00
|
|
|
import sys
|
2021-11-23 14:26:39 -05:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2021-12-23 18:44:53 -05:00
|
|
|
def toy_metric(
|
|
|
|
X_test,
|
|
|
|
y_test,
|
|
|
|
estimator,
|
|
|
|
labels,
|
|
|
|
X_train,
|
|
|
|
y_train,
|
|
|
|
weight_test=None,
|
|
|
|
weight_train=None,
|
|
|
|
config=None,
|
|
|
|
groups_test=None,
|
|
|
|
groups_train=None,
|
|
|
|
):
|
|
|
|
return 0, {
|
|
|
|
"val_loss": 0,
|
|
|
|
"train_loss": 0,
|
|
|
|
"pred_time": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-03 12:45:16 -05:00
|
|
|
@pytest.mark.skipif(sys.platform == "darwin", reason="do not run on mac os")
|
2021-12-23 18:44:53 -05:00
|
|
|
def test_custom_metric():
|
2021-11-23 14:26:39 -05:00
|
|
|
from flaml import AutoML
|
2021-12-03 12:45:16 -05:00
|
|
|
import requests
|
2021-11-23 14:26:39 -05:00
|
|
|
from datasets import load_dataset
|
|
|
|
|
2021-12-03 12:45:16 -05:00
|
|
|
try:
|
|
|
|
train_dataset = (
|
|
|
|
load_dataset("glue", "mrpc", split="train").to_pandas().iloc[0:4]
|
|
|
|
)
|
|
|
|
dev_dataset = load_dataset("glue", "mrpc", split="train").to_pandas().iloc[0:4]
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
return
|
2021-11-23 14:26:39 -05:00
|
|
|
|
|
|
|
custom_sent_keys = ["sentence1", "sentence2"]
|
|
|
|
label_key = "label"
|
|
|
|
|
|
|
|
X_train = train_dataset[custom_sent_keys]
|
|
|
|
y_train = train_dataset[label_key]
|
|
|
|
|
|
|
|
X_val = dev_dataset[custom_sent_keys]
|
|
|
|
y_val = dev_dataset[label_key]
|
|
|
|
|
|
|
|
automl = AutoML()
|
|
|
|
|
2021-12-23 18:44:53 -05:00
|
|
|
# testing when max_iter=1 and do retrain only without hpo
|
2021-11-23 14:26:39 -05:00
|
|
|
|
|
|
|
automl_settings = {
|
|
|
|
"gpu_per_trial": 0,
|
|
|
|
"max_iter": 1,
|
|
|
|
"time_budget": 5,
|
|
|
|
"task": "seq-classification",
|
|
|
|
"metric": toy_metric,
|
|
|
|
"log_file_name": "seqclass.log",
|
|
|
|
}
|
|
|
|
|
|
|
|
automl_settings["custom_hpo_args"] = {
|
|
|
|
"model_path": "google/electra-small-discriminator",
|
|
|
|
"output_dir": "data/output/",
|
|
|
|
"ckpt_per_epoch": 5,
|
|
|
|
"fp16": False,
|
|
|
|
}
|
|
|
|
|
|
|
|
automl.fit(
|
|
|
|
X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
|
|
|
|
)
|
2021-12-23 18:44:53 -05:00
|
|
|
|
|
|
|
# testing calling custom metric in TransformersEstimator._compute_metrics_by_dataset_name
|
|
|
|
|
|
|
|
automl_settings["max_iter"] = 3
|
|
|
|
automl.fit(
|
|
|
|
X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
|
|
|
|
)
|
|
|
|
|
2021-11-23 14:26:39 -05:00
|
|
|
del automl
|