mirror of
https://github.com/microsoft/autogen.git
synced 2026-02-03 22:54:55 -05:00
fixing load best model at the end (#389)
This commit is contained in:
82
test/nlp/run_gpu.py
Normal file
82
test/nlp/run_gpu.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import sys
|
||||
import pytest
|
||||
import pickle
|
||||
import shutil
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "darwin", reason="do not run on mac os")
|
||||
def _test_hf_data():
|
||||
from flaml import AutoML
|
||||
import requests
|
||||
from datasets import load_dataset
|
||||
|
||||
try:
|
||||
train_dataset = load_dataset("glue", "mrpc", split="train").to_pandas()
|
||||
dev_dataset = load_dataset("glue", "mrpc", split="validation").to_pandas()
|
||||
test_dataset = load_dataset("glue", "mrpc", split="test").to_pandas()
|
||||
except requests.exceptions.ConnectionError:
|
||||
return
|
||||
|
||||
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]
|
||||
|
||||
X_test = test_dataset[custom_sent_keys]
|
||||
|
||||
automl = AutoML()
|
||||
|
||||
automl_settings = {
|
||||
"gpu_per_trial": 1,
|
||||
"max_iter": 5,
|
||||
"time_budget": 5000,
|
||||
"task": "seq-classification",
|
||||
"metric": "accuracy",
|
||||
"log_file_name": "seqclass.log",
|
||||
"use_ray": True,
|
||||
}
|
||||
|
||||
automl_settings["custom_hpo_args"] = {
|
||||
"model_path": "facebook/muppet-roberta-base",
|
||||
"output_dir": "test/data/output/",
|
||||
"ckpt_per_epoch": 5,
|
||||
"fp16": True,
|
||||
}
|
||||
|
||||
automl.fit(
|
||||
X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val, **automl_settings
|
||||
)
|
||||
|
||||
automl = AutoML()
|
||||
automl.retrain_from_log(
|
||||
X_train=X_train,
|
||||
y_train=y_train,
|
||||
train_full=True,
|
||||
record_id=0,
|
||||
**automl_settings
|
||||
)
|
||||
with open("automl.pkl", "wb") as f:
|
||||
pickle.dump(automl, f, pickle.HIGHEST_PROTOCOL)
|
||||
with open("automl.pkl", "rb") as f:
|
||||
automl = pickle.load(f)
|
||||
shutil.rmtree("test/data/output/")
|
||||
automl.predict(X_test)
|
||||
automl.predict(["test test", "test test"])
|
||||
automl.predict(
|
||||
[
|
||||
["test test", "test test"],
|
||||
["test test", "test test"],
|
||||
["test test", "test test"],
|
||||
]
|
||||
)
|
||||
|
||||
automl.predict_proba(X_test)
|
||||
print(automl.classes_)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_test_hf_data()
|
||||
Reference in New Issue
Block a user