karthik-0306 commented on code in PR #41393:
URL: https://github.com/apache/beam/pull/41393#discussion_r4697539579
##########
sdks/python/apache_beam/ml/inference/sklearn_inference.py:
##########
@@ -209,9 +209,10 @@ def _default_pandas_inference_fn(
model: BaseEstimator,
batch: Sequence[pandas.DataFrame],
inference_args: Optional[dict[str, Any]] = None) -> Any:
+ inference_args = {} if not inference_args else inference_args
# vectorize data for better performance
vectorized_batch = pandas.concat(batch, axis=0)
- predictions = model.predict(vectorized_batch)
+ predictions = model.predict(vectorized_batch, **inference_args)
Review Comment:
Thanks for catching this! You're right, and raising an error is better than
returning mixed-up results.
I added a small check after `predict` in both default inference functions.
If `predict` returns a tuple, which is what happens with `return_std=True` or
`return_cov=True`, RunInference now raises a `ValueError` that explains what
happened and suggests passing an `inference_fn` to use the extra outputs.
Custom inference functions skip this check, so they can still return the means
and stds per example. I added a test for that as well.
I also applied the same check to `SklearnModelHandlerNumpy`. That handler
has always passed `inference_args` to `predict`, so it had the same issue on
master: with 2 examples it returned the means and stds as the two results, and
with 3 it failed with `Expected one prediction per input`. I thought it was
better to fix both handlers the same way here than to leave them behaving
differently.
One thing to mention: a model that isn't from scikit-learn and returns a
tuple with one value per row would now get this error too. scikit-learn's
`predict` always returns arrays, so I think that's acceptable, but I'm happy to
narrow the check if you'd prefer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]