unionml.model.Model#

class unionml.model.Model(name='model', init=None, *, dataset, hyperparameter_config=None)#

Initialize a UnionML Model.

The term UnionML Model refers to the specification of a model, which the user defines through the functional entrypoints, e.g. unionml.model.Model.trainer(). The term model object is used to refer to some instance of model from a machine learning framework such as the subclasses of the BaseEstimator class in sklearn, Module in pytorch, etc.

Parameters
  • name (str) – name of the model

  • init (Union[Type, Callable, None]) – a class or callable that produces a model object (e.g. an sklearn estimator) when invoked.

  • dataset (Dataset) – a UnionML Dataset object to bind to the model.

  • hyperparameter_config (Optional[Dict[str, Type]]) –

    A dictionary mapping hyperparameter names to types. This is used to determine the hyperparameter names and types associated with the model object produced by the init argument. For example:

    >>> {
    ...    "hyperparameter1": int,
    ...    "hyperparameter2": str,
    ...    "hyperparameter3": float,
    ... }
    

Methods

evaluator

Register a function for producing metrics for given model object.

init

Register a function for initializing a model object.

load

Load a model object from disk.

loader

Register a function for deserializing a model object to disk.

predict

Generate predictions locally.

predict_from_features_task

Create a Flyte task for generating predictions from a model object.

predict_from_features_workflow

Create a Flyte prediction workflow using raw features.

predict_task

Create a Flyte task for generating predictions from a model object.

predict_workflow

Create a Flyte prediction workflow using features from the dataset.reader as the data source.

predictor

Register a function that generates predictions from a model object.

remote

Configure the unionml.Model for remote backend deployment.

remote_deploy

Deploy model services to a Flyte backend.

remote_fetch_predictions

Fetch predictions from a Flyte execution.

remote_list_model_versions

Lists all the model versions of this UnionML app, in reverse chronological order.

remote_load

Load a ModelArtifact based on the provided Flyte execution.

remote_predict

Generate predictions on a remote Flyte backend.

remote_train

Train a model object on a remote Flyte backend.

remote_wait

Wait for a FlyteWorkflowExecution to complete and returns the execution's output.

save

Save the model object to disk.

saver

Register a function for serializing a model object to disk.

serve

Create a FastAPI serving app.

train

Train a model object locally

train_task

Create a Flyte task for training a model object.

train_workflow

Create a Flyte training workflow for model object training.

trainer

Register a function for training a model object.

Attributes

artifact

Model artifact associated with the unionml.Model .

config_file

Path to the config file associated with the Flyte backend.

dockerfile

Path to Docker file used to package the UnionML app.

hyperparameter_type

Hyperparameter type of the model object based on the init function signature.

model_type

rtype

Type

predict_from_features_workflow_name

Name of the prediction workflow used to generate predictions from raw features.

predict_workflow_name

Name of the prediction workflow used to generate predictions from the dataset.reader .

registry

Docker registry used to push UnionML app.

train_workflow_name

Name of the training workflow.

trainer_params

Parameters used to create a Flyte workflow for model object training.

property artifact: Optional[unionml.model.ModelArtifact]#

Model artifact associated with the unionml.Model .

Return type

Optional[ModelArtifact]

property config_file: Optional[str]#

Path to the config file associated with the Flyte backend.

Return type

Optional[str]

property dockerfile: Optional[str]#

Path to Docker file used to package the UnionML app.

Return type

Optional[str]

evaluator(fn)#

Register a function for producing metrics for given model object.

property hyperparameter_type: Type#

Hyperparameter type of the model object based on the init function signature.

Return type

Type

init(fn)#

Register a function for initializing a model object.

load(file, *args, **kwargs)#

Load a model object from disk.

loader(fn)#

Register a function for deserializing a model object to disk.

predict(features=None, **reader_kwargs)#

Generate predictions locally.

You can either pass this function raw features via the features argument or you can pass in keyword arguments that will be forwarded to the unionml.Dataset.reader() method as the feature source.

Parameters
predict_from_features_task()#

Create a Flyte task for generating predictions from a model object.

This is used in the Flyte workflow produced by predict_from_features_workflow.

predict_from_features_workflow()#

Create a Flyte prediction workflow using raw features.

property predict_from_features_workflow_name#

Name of the prediction workflow used to generate predictions from raw features.

predict_task()#

Create a Flyte task for generating predictions from a model object.

This is used in the Flyte workflow produced by predict_workflow.

predict_workflow()#

Create a Flyte prediction workflow using features from the dataset.reader as the data source.

property predict_workflow_name#

Name of the prediction workflow used to generate predictions from the dataset.reader .

predictor(fn=None, **predict_task_kwargs)#

Register a function that generates predictions from a model object.

property registry: Optional[str]#

Docker registry used to push UnionML app.

Return type

Optional[str]

remote(registry=None, image_name=None, dockerfile='Dockerfile', config_file=None, project=None, domain=None)#

Configure the unionml.Model for remote backend deployment.

Parameters
  • registry (Optional[str]) – Docker registry used to push UnionML app.

  • image_name (Optional[str]) – image name to give to the Docker image associated with the UnionML app.

  • dockerfile (str) – path to the Dockerfile used to package the UnionML app.

  • config_file (Optional[str]) – path to the flytectl config to use for deploying your UnionML app to a Flyte backend.

  • project (Optional[str]) – deploy your app to this Flyte project name.

  • project – deploy your app to this Flyte domain name.

remote_deploy(app_version=None)#

Deploy model services to a Flyte backend.

remote_fetch_predictions(execution)#

Fetch predictions from a Flyte execution.

Parameters

execution (FlyteWorkflowExecution) – a Flyte workflow execution, which is the output of remote_predict(..., wait=False) .

Return type

Any

remote_list_model_versions(app_version=None, limit=10)#

Lists all the model versions of this UnionML app, in reverse chronological order.

Parameters
  • app_version (Optional[str]) – if provided, lists the model versions associated with this app version. By default, this uses the current git sha of the repo, which versions your UnionML app.

  • limit (int) – limit the number results to fetch.

Return type

List[str]

remote_load(execution)#

Load a ModelArtifact based on the provided Flyte execution.

Parameters

execution (FlyteWorkflowExecution) – a Flyte workflow execution, which is the output of remote_train(..., wait=False) .

remote_predict(app_version=None, model_version=None, wait=True, *, features=None, **reader_kwargs)#

Generate predictions on a remote Flyte backend.

You can either pass this function raw features via the features argument or you can pass in keyword arguments that will be forwarded to the unionml.Dataset.reader() method as the feature source.

Parameters
  • app_version (Optional[str]) – if provided, executes a prediction job using the specified UnionML app version. By default, this uses the current git sha of the repo, which versions your UnionML app.

  • model_version (Optional[str]) – if provided, executes a prediction job using the specified model version. By default, this uses the latest Flyte execution id as the model version.

  • wait (bool) – if True, this is a synchronous operation, returning a ModelArtifact. Otherwise, this function returns a FlyteWorkflowExecution.

  • features (Optional[Any]) –

    Raw features that are pre-processed by the :py:class:unionml.Dataset methods in the following order:

  • reader_kwargs – keyword arguments that correspond to the unionml.Dataset.reader() method signature.

Return type

Union[Any, FlyteWorkflowExecution]

remote_train(app_version=None, wait=True, *, hyperparameters=None, loader_kwargs=None, splitter_kwargs=None, parser_kwargs=None, trainer_kwargs=None, **reader_kwargs)#

Train a model object on a remote Flyte backend.

Parameters
  • app_version (Optional[str]) – if provided, executes a training job using the specified UnionML app version. By default, this uses the current git sha of the repo, which versions your UnionML app.

  • wait (bool) – if True, this is a synchronous operation, returning a ModelArtifact. Otherwise, this function returns a FlyteWorkflowExecution.

  • hyperparameters (Optional[Dict[str, Any]]) – a dictionary mapping hyperparameter names to values. This is passed into the init callable to initialize a model object.

  • loader_kwargs (Optional[Dict[str, Any]]) – key-word arguments to pass to the registered unionml.Dataset.loader function. This will override any defaults set in the function definition.

  • splitter_kwargs (Optional[Dict[str, Any]]) – key-word arguments to pass to the registered unionml.Dataset.splitter function. This will override any defaults set in the function definition.

  • parser_kwargs (Optional[Dict[str, Any]]) – key-word arguments to pass to the registered unionml.Dataset.parser function. This will override any defaults set in the function definition.

  • trainer_kwargs (Optional[Dict[str, Any]]) – a dictionary mapping training parameter names to values. There training parameters are determined by the keyword-only arguments of the model.trainer function.

  • reader_kwargs – keyword arguments that correspond to the unionml.Dataset.reader() method signature.

Return type

Union[ModelArtifact, FlyteWorkflowExecution]

remote_wait(execution, **kwargs)#

Wait for a FlyteWorkflowExecution to complete and returns the execution’s output.

Return type

Any

save(file, *args, **kwargs)#

Save the model object to disk.

saver(fn)#

Register a function for serializing a model object to disk.

serve(app, remote=False, model_version='latest')#

Create a FastAPI serving app.

Parameters

app (FastAPI) – A FastAPI app to use for model serving.

train(hyperparameters=None, loader_kwargs=None, splitter_kwargs=None, parser_kwargs=None, trainer_kwargs=None, **reader_kwargs)#

Train a model object locally

Parameters
  • hyperparameters (Optional[Dict[str, Any]]) – a dictionary mapping hyperparameter names to values. This is passed into the init callable to initialize a model object.

  • loader_kwargs (Optional[Dict[str, Any]]) – key-word arguments to pass to the registered unionml.Dataset.loader function. This will override any defaults set in the function definition.

  • splitter_kwargs (Optional[Dict[str, Any]]) – key-word arguments to pass to the registered unionml.Dataset.splitter function. This will override any defaults set in the function definition.

  • parser_kwargs (Optional[Dict[str, Any]]) – key-word arguments to pass to the registered unionml.Dataset.parser function. This will override any defaults set in the function definition.

  • trainer_kwargs (Optional[Dict[str, Any]]) – a dictionary mapping training parameter names to values. There training parameters are determined by the keyword-only arguments of the model.trainer function.

  • reader_kwargs – keyword arguments that correspond to the unionml.Dataset.reader() method signature.

Return type

Tuple[Any, Any]

train_task()#

Create a Flyte task for training a model object.

This is used in the Flyte workflow produced by train_workflow.

train_workflow()#

Create a Flyte training workflow for model object training.

property train_workflow_name#

Name of the training workflow.

trainer(fn=None, **train_task_kwargs)#

Register a function for training a model object.

property trainer_params: Dict[str, inspect.Parameter]#

Parameters used to create a Flyte workflow for model object training.

Return type

Dict[str, Parameter]