def test_serialize_sklearn_model(self):
        class MyModeler(MLModeler):
            def load_data(self) -> dict:
                data = load_iris()
                return {'X': data.data, 'y': data.target}

            def train(self, prepared_data):
                model = LogisticRegression(random_state=0)
                X, y = prepared_data['X'], prepared_data['y']
                model.fit(X, y)
                return model

        class MyModel(MLModel):
            pass

        my_modeler = MyModeler()
        my_modeler.model_class = MyModel

        model = my_modeler.build()
        with tempfile.TemporaryDirectory() as path:
            mm = ModelRepository(storage=LocalStorage(storage_path=path))
            version = mm.persist(model=model)

            model_2 = MyModel()
            mm.load(model=model_2, version=version)

            assert 'sklearn' in str(type(model_2.base_model))
Example #2
0
    def load(self, version: str = None) -> "Model":
        """
        Load parameters from the specified `version` from the ModelRepository.
        Leave version blank to load latest version.
        """
        repo = ModelRepository.get_model_repo(self)
        repo.load(model=self, version=version)

        return self
Example #3
0
    def persist(self, version=None):
        """
        Persist this model's properties to the ModelRepository. Currently, only `stats`, `metrics`, `model` properties are supported.

        `model` property could be single model, list or dict of models
        Currently, only sklearn and tensorflow-keras are supported.

        :param version: model version, leave blank for autogeneration
        :returns: model version
        """
        repo = ModelRepository.get_model_repo(self)
        return repo.persist(model=self, version=version)
Example #4
0
    def test_serialize_sklearn_model(self):
        class MyModel(MLModel):
            def __init__(self):
                super().__init__()
                self.base_model = LogisticRegression(random_state=0)

            def train(self, prepared_data):
                X, y = prepared_data['X'], prepared_data['y']
                self.base_model.fit(X, y)

        X, y = load_iris(return_X_y=True)
        prepared_data = {'X': X, 'y': y}

        model = MyModel()
        model.train(prepared_data)
        with tempfile.TemporaryDirectory() as path:
            mm = ModelRepository(storage=LocalStorage(storage_path=path))
            version = mm.persist(model=model)

            model_2 = MyModel()
            mm.load(model=model_2, version=version)

            assert 'sklearn' in str(type(model_2.base_model))
Example #5
0
 def init_model_repo(cls, repo_path):
     from h1st.model.repository import ModelRepository
     if not hasattr(ModelRepository, 'MODEL_REPO'):
         setattr(ModelRepository, 'MODEL_REPO',
                 ModelRepository(storage=repo_path))