2

It is not clear to me if one could use mlflow to serve a model that is evolving continuously based on its previous predictions.

I need to be able to query a model in order to make a prediction on a sample of data which is the basic use of mlflow serve. However I also want the model to be updated internaly now that it has seen new data.

Is it possible or does it need a FR ?

1 Answers1

1

I think that you should be able to do that by implementing the custom python model or custom flavor, as it's described in the documentation. In this case you need to create a class that is inherited from mlflow.pyfunc.PythonModel, and implement the predict method, and inside that method you're free to do anything. Here is just simple example from documentation:

class AddN(mlflow.pyfunc.PythonModel):

    def __init__(self, n):
        self.n = n

    def predict(self, context, model_input):
        return model_input.apply(lambda column: column + self.n)

and this model is then could be saved & loaded again just as normal models:

# Construct and save the model
model_path = "add_n_model"
add5_model = AddN(n=5)
mlflow.pyfunc.save_model(path=model_path, python_model=add5_model)

# Load the model in `python_function` format
loaded_model = mlflow.pyfunc.load_model(model_path)
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • Thanks, that was my intuition as well, however I would only be able to save the model once, because the save_model takes a PythonModel and loaded_model returns a PyfuncModel that cannot be saved again. – Merkle Daamgard Apr 12 '21 at 08:13
  • I didn't try it myself, but maybe look to https://mlflow.org/docs/latest/models.html#custom-flavors. Or add a method to your class that will return new PythonModel instance that could be saved? Or even write data directly from model – Alex Ott Apr 12 '21 at 08:20
  • What I ended up doing was saving the model once and saving the model (pickle) as an artifact of that run. Then I would just get the run from the model register every time I need to run the model (and overwrite the model pickle). – Merkle Daamgard Apr 14 '21 at 12:20