Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.

save: saving custom models #586

Open
aguschin opened this issue Jan 21, 2023 · 0 comments
Open

save: saving custom models #586

aguschin opened this issue Jan 21, 2023 · 0 comments
Labels
p2-medium Medium priority serialization Dumping and loading Python objects

Comments

@aguschin
Copy link
Contributor

aguschin commented Jan 21, 2023

I'd like to have a way to serialize custom models. I know I just could implement something like class CustomModel(ModelType, ModelHook, IsInstanceHookMixin) and use it as a plugin, but would be good to have some built-in way of doing that.

Last thing I tried was this, but it doesn't work (in the sense that upon model loading whichtype and metrics are disconnected and calling whichtypes doesn't increase the counter returned by metrics.

from mlem.core.objects import MlemModel, ModelAnalyzer


class Model:
    times_called = 0
    
    def whichtype(self, data):
        self.times_called += 1
        return str(type(data))
    
    def metrics(self, dummyarg):
        return self.times_called


simple_model = Model()
    

model = MlemModel()
model.add_processor("whichtype", ModelAnalyzer.analyze(simple_model.whichtype, sample_data="word"))
model.add_processor("metrics", ModelAnalyzer.analyze(simple_model.metrics, sample_data="word"))
model.call_orders["whichtype"] = [("whichtype", "__call__")]
model.call_orders["metrics"] = [("metrics", "__call__")]

model.dump("model")

Few ideas how this can look like

from mlem.bla.bla import modelmethoddecorator

class Model:
    times_called = 0
    
    @modelmethoddecorator
    def whichtype(self, data):
        self.times_called += 1
        return str(type(data))
    
    @modelmethoddecorator
    def metrics(self, dummyarg):
        self.times_called += 1
        return self.times_called

or

from mlem.bla.bla import modelmethoddecorator

class Model:
    _model_methods=["whichtypes", "metrics"]
    times_called = 0
    
    def whichtype(self, data):
        self.times_called += 1
        return str(type(data))
    
    def metrics(self, dummyarg):
        self.times_called += 1
        return self.times_called

or just

mlem.api.save(model, methods=["whichtypes", "metrics"])

or

mlem.api.save(model, sample_data={"whichtypes": (["word"], {}), "metrics": ({dummyarg="value"})])
@aguschin aguschin added serialization Dumping and loading Python objects p1-high High priority labels Jan 21, 2023
@aguschin aguschin added p2-medium Medium priority and removed p1-high High priority labels Mar 14, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
p2-medium Medium priority serialization Dumping and loading Python objects
Projects
None yet
Development

No branches or pull requests

1 participant