Skip to content

Commit

Permalink
handling nlp divide by zero (microsoft#926)
Browse files Browse the repository at this point in the history
* handling nlp divide by zero

* catching zerodivisionerror

* catching zerodivisionerror

* catching zerodivisionerror

* addressing comments

* addressing comments

* updating test case

* update

* add blank to last line

* update nlp notebook

* rerun

* rerun

* sync with main

* add model selection for nlg

* addressing keyerror

* add raise exception

* update

* fix bug

* revert

* updating automl_nlp

* Update flaml/automl/model.py

Co-authored-by: Zvi Baratz <[email protected]>

* address comments

* address comments

---------

Co-authored-by: Li Jiang <[email protected]>
Co-authored-by: Zvi Baratz <[email protected]>
  • Loading branch information
3 people authored Apr 9, 2023
1 parent 61bbd28 commit 393de33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions flaml/automl/automl.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ def predict(
return None
X = self._state.task.preprocess(X, self._transformer)
y_pred = estimator.predict(X, **pred_kwargs)

if (
isinstance(y_pred, np.ndarray)
and y_pred.ndim > 1
Expand Down
23 changes: 14 additions & 9 deletions flaml/automl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,13 @@ def predict_proba(self, X, **pred_kwargs):
test_dataset = Dataset.from_pandas(X_test)

new_trainer = self._init_model_for_predict()
predictions = new_trainer.predict(test_dataset)
return predictions.predictions
try:
predictions = new_trainer.predict(test_dataset).predictions
except ZeroDivisionError:
logger.warning("Zero division error appeared in HuggingFace Transformers.")
predictions = np.array([-0.05] * len(test_dataset))
else:
return predictions

def score(self, X_val: DataFrame, y_val: Series, **kwargs):
import transformers
Expand Down Expand Up @@ -1222,13 +1227,13 @@ def predict(self, X, **pred_kwargs):

new_trainer = self._init_model_for_predict()

if self._task not in NLG_TASKS:
predictions = new_trainer.predict(test_dataset)
else:
predictions = new_trainer.predict(
test_dataset,
metric_key_prefix="predict",
)
kwargs = {} if self._task not in NLG_TASKS else {"metric_key_prefix": "predict"}
try:
predictions = new_trainer.predict(test_dataset, **kwargs)
except ZeroDivisionError:
logger.warning("Zero division error appeared in HuggingFace Transformers.")
predictions = np.array([0] * len(test_dataset))

post_y_pred, _ = postprocess_prediction_and_true(
task=self._task,
y_pred=predictions.predictions,
Expand Down
4 changes: 3 additions & 1 deletion test/nlp/test_autohf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def test_hf_data():
**automl_settings
)
automl.predict(X_test, **{"per_device_eval_batch_size": 2})
automl.predict(["test test", "test test"])
automl.predict(["", ""])
automl.predict_proba(["", ""])

automl.predict(
[
["test test", "test test"],
Expand Down

0 comments on commit 393de33

Please sign in to comment.