Skip to content

kxi.ml.registry.set

model

def model(model,
          model_name: str,
          model_type: str,
          folder_path: Union[str, dict] = None,
          experiment_name: str = None,
          description: str = None,
          data: dict = None,
          requirements: Union[bool, str, List[str]] = None,
          major: bool = None,
          major_version: int = None,
          code: str = None,
          axis: bool = None) -> UUID

Persist a model to the Registry.

Arguments:

  • model - The model to be saved to the Registry, this can be one of: A Python function which can be saved using 'joblib.dump'. A fit scikit-learn model which contains a 'predict' method. A Keras model containing a 'predict' method. A PyTorch model saved which can be saved using 'torch.save'. A q model. A path to any of the above models saved to disk.

  • model_name - Name to be associated with the model when saved as a 'str'.

  • model_type - The type of model that is being saved, namely 'q', 'sklearn', 'keras', 'python', 'torch' as an 'str'.
  • folder_path - Either a string denoting the folder path where the registry exists, or a dictionary specifying the vendor (as key) and the path (as value), {'aws':'s3://kx-ml-registry-bucket'} or None to default to local current working directory.
  • experiment_name - The name of the experiment the model is associated with as a string.
  • description - User supplied description of the model to be added to the registry, this should describe important characteristics of the model indicating it's unique nature.
  • data - User supplied reference data for the parsing of statistical information about the data to be used for monitoring purposes 'kx.Table', 'np.records', 'pd.DataFrame' or 'pa.Table' objects supported
  • requirements - Are Python requirements to be associated with a model, there are 2 options bool - Whether to run 'pip freeze' against your current environment. str - Specifying the path to a 'requirements.txt' file by default no requirements are saved with the model. List[str] - A list of package names we want to link to the model.

  • major - Boolean indicating if the addition of this model a 'major' update i.e. 1.1 -> 2.0 rather than 1.1 -> 1.2

  • major_version - What major version of the model is to be incremented? By default the function will increment based on the maximal version in the registry, users can overwrite this to increment a minor version of a previous model.
  • code - Reference to the location of any files *.py, *.p, *.k or *.q required by the model, these will be loaded prior to loading the model on retrieval as such this should be used if the model being retrieved has prerequisites.
  • axis - Boolean indicating if data should be passed into the model: 'horizontally', column per feature indicated by 'True' 'vertically', row per feature indicated by 'False'

Returns:

UUID indicating the unique model added to the Registry

Examples:

Save a trained model to a local registry:

>>> from kxi import ml
>>> ml.init()
>>> from sklearn.linear_model import LinearRegression
>>> import numpy as np
>>> regressor = LinearRegression().fit(np.random.randn(10, 1), np.random.rand(10))
>>> ml.registry.set.model(model=regressor,
model_name="linear_regression",
model_type="sklearn",
folder_path="/tmp",
experiment_name="day0")
UUID('1fd69913-2ea8-6436-2f5d-129ae1261070')

parameters

def parameters(param_name: str,
               params: str,
               folder_path: Union[str, dict] = None,
               experiment_name: str = None,
               model_name: str = None,
               version: list = None) -> str

Associate parameters with a model stored in the Registry.

Arguments:

  • param_name - Name of the parameter to be associated with a model as a string
  • params - Value of the parameter to be stored as a json file
  • folder_path - Either a string denoting the folder path where the registry exists, or a dictionary specifying the vendor (as key) and the path (as value), {'aws':'s3://kx-ml-registry-bucket'} or None to default to local current working directory
  • experiment_name - The name of the experiment the model is associated with as a string
  • model_name - The name of the model to which the parameter is to be associated, if None the latest model added to the registry will be used
  • version - A list of the major and minor versions of the model - [major, minor]. If None, the latest version of the model associated with model_name is used.

Returns:

A string containing the path of the created parameter file.

Examples:

Save hyperparameters used during training along with the corresponding model:

>>> from kxi import ml
>>> ml.init()
>>> from sklearn.linear_model import QuantileRegressor
>>> import numpy as np
>>> alpha = 0.0
>>> regressor = QuantileRegressor(alpha=alpha).fit(np.random.randn(10, 1),
np.random.rand(10))
>>> ml.registry.set.model(model=regressor,
model_name="quantile_regression",
model_type="sklearn",
folder_path="/tmp",
experiment_name="day0")
>>> ml.registry.set.parameters(param_name="alpha",
params=alpha,
folder_path="/tmp",
experiment_name="day0",
model_name="quantile_regression")
':/tmp/KX_ML_REGISTRY/namedExperiments/day0/quantile_regression/1.0/params/alpha.json'