示例#1
0
def test_raise_exception(sequential_model):
    with TempDir(chdr=True, remove_on_exit=True) as tmp:
        path = tmp.path("model")
        with pytest.raises(IOError, match="No such file or directory"):
            mlflow.pytorch.load_model(path)

        with pytest.raises(
                TypeError,
                match="Argument 'pytorch_model' should be a torch.nn.Module"):
            mlflow.pytorch.save_model([1, 2, 3], path)

        mlflow.pytorch.save_model(sequential_model, path)
        with pytest.raises(
                RuntimeError,
                match=f"Path '{os.path.abspath(path)}' already exists"):
            mlflow.pytorch.save_model(sequential_model, path)

        from mlflow import sklearn
        import sklearn.neighbors as knn

        path = tmp.path("knn.pkl")
        knn = knn.KNeighborsClassifier()
        with open(path, "wb") as f:
            pickle.dump(knn, f)
        path = tmp.path("knn")
        sklearn.save_model(knn, path=path)
        with pytest.raises(MlflowException,
                           match='Model does not have the "pytorch" flavor'):
            mlflow.pytorch.load_model(path)
示例#2
0
def sv_model(params, model):
    mlflow.log_params(params)
    mlflow.set_tag("running_from_jupyter", "True")
    #mlflow.log_artifact("./models")
    #mlflow.sklearn.log_model(model, "model")
    path = f"models/{params['model']+params['label']}_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}"
    save_model(sk_model=model, path=path)
示例#3
0
 def _save_model(self):
     """Save model in `./models` as `<model_type>_<label>_<time>`
     """
     if self.model:
         time_now = datetime.now().strftime('%Y-%m-%d_%H%M%S')
         model_type = self.params["model"]
         label = self.params["label"]
         path = self.model_path / f"{model_type}_{label}_{time_now}"
         save_model(sk_model=self.model, path=path)
         logger.info(f"Saved model to {path}.")
示例#4
0
 def test_model_save_load(self):
     with TempDir(chdr=True, remove_on_exit=True) as tmp:
         model_path = tmp.path("knn.pkl")
         with open(model_path, "wb") as f:
             pickle.dump(self._knn, f)
         path = tmp.path("knn")
         sklearn.save_model(self._knn, path=path)
         x = sklearn.load_model(path)
         xpred = x.predict(self._X)
         np.testing.assert_array_equal(self._knn_predict, xpred)
         # sklearn should also be stored as a valid pyfunc model
         # test pyfunc compatibility
         y = pyfunc.load_pyfunc(path)
         ypred = y.predict(self._X)
         np.testing.assert_array_equal(self._knn_predict, ypred)
示例#5
0
def test_raise_exception(sequential_model):
    with TempDir(chdr=True, remove_on_exit=True) as tmp:
        path = tmp.path("model")
        with pytest.raises(IOError):
            mlflow.pytorch.load_model(path)

        with pytest.raises(TypeError):
            mlflow.pytorch.save_model([1, 2, 3], path)

        mlflow.pytorch.save_model(sequential_model, path)
        with pytest.raises(RuntimeError):
            mlflow.pytorch.save_model(sequential_model, path)

        from mlflow import sklearn
        import sklearn.neighbors as knn
        path = tmp.path("knn.pkl")
        knn = knn.KNeighborsClassifier()
        with open(path, "wb") as f:
            pickle.dump(knn, f)
        path = tmp.path("knn")
        sklearn.save_model(knn, path=path)
        with pytest.raises(MlflowException):
            mlflow.pytorch.load_model(path)
示例#6
0
        'classification',
        'classification',
        'classification',
        'classification',
    ]
})

helper_features = ['ew_'+str(x) for x in range(2,14)] + \
                  ['lw_distinct_series', 'lw_distinct_episodes'] + \
                  ['genre_share_drama', 'genre_share_comedy', 'genre_share_sport', 'genre_share_music']

# Create the missing value imputer to learn sensible substitute values
mvi = missing_values.missing_value_imputer(impute_strategies=impute_strategies,
                                           helper_features=helper_features)

sklearn.save_model(mvi, f"missing_value_imputer-{date.today()}")
sklearn.log_model(mvi, f"missing_value_imputer-{date.today()}")

# Train the imputer on df (the training set)
df_impute = mvi.train(df)

# Replacing missing value columns in fresh data with imputed columns
non_imputed = [
    c for c in df.columns if c not in mvi.impute_strategies.colname.values
]
df = pd.concat([df[non_imputed], df_impute], axis=1)

# Pickle the imputer for use with model scoring
utils.pickler(mvi, pickle_dir + '/prep/missing_value_imputer')

print("Missing value imputation completed\n")