Logging Model Signature¶
Another important part of logging a model is to log it's signature. ¿What is a model signature? A model signature is a description of the input and output of the model. This is important because it allows us to know what kind of data the model expects and what kind of data it will return.
Imagine the input of the model is a 2D array of shape (n_samples, n_features) and the output is a 1D array of shape (n_samples). The signature of this model would be:
import numpy as np
inputs = np.random.randn(1, 100) # 1 sample of 100 features
outputs = np.random.randn(1, ) # 1 sample
We generate the signature of the model using the infer_signature
function from the mlflow.models.signature
module. This function receives two parameters, the input and the output of the model. The input and output of the model are represented as a pandas DataFrame. The infer_signature
function will return a string with the signature of the model.
from mlflow.models import infer_signature
signature = infer_signature(model_input=inputs, model_output=outputs)
We can log the model signature along with the model using the log_model
function.
from sklearn.linear_model import LinearRegression
import mlflow
model = LinearRegression()
with mlflow.start_run():
# signature is logged along with the model
mlflow.sklearn.log_model(model, "model", signature=signature)