jrmccluskey commented on code in PR #23642:
URL: https://github.com/apache/beam/pull/23642#discussion_r1014305586
##########
sdks/python/apache_beam/ml/inference/sklearn_inference.py:
##########
@@ -83,14 +83,21 @@ def _convert_to_result(
]
return [PredictionResult(x, y) for x, y in zip(batch, predictions)]
+def _default_numpy_inference_fn(
+ model: BaseEstimator, batch: Sequence[numpy.ndarray], inference_args:
Optional[Dict[str, Any]] = None
+) -> Any:
+ # vectorize data for better performance
+ vectorized_batch = numpy.stack(batch, axis=0)
+ return model.predict(vectorized_batch)
class SklearnModelHandlerNumpy(ModelHandler[numpy.ndarray,
PredictionResult,
BaseEstimator]):
def __init__(
self,
model_uri: str,
- model_file_type: ModelFileType = ModelFileType.PICKLE):
+ model_file_type: ModelFileType = ModelFileType.PICKLE,
+ inference_fn: Optional[Callable[[BaseEstimator, Sequence[numpy.ndarray],
Optional[Dict[str, Any]]], Any]] = _default_numpy_inference_fn):
Review Comment:
Circled around to defining a custom type to make this look less verbose in
function signatures while still providing solid typing guarantees.
--
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]