示例#1
0
def mlflow_run(params, run_name="LOCAL_REGISTRY"):
    """
    Function to start a run within a Default experiment
    :param params: ters used for the run, such as arguments to RandomForest scikit-learn
    :param run_name: label for the name of the run
    :return: experiment ID and run ID
    """

    with mlflow.start_run(run_name=run_name) as run:
        # Get the run and experimentid

        run_id = run.info.run_uuid
        experiment_id = run.info.experiment_id

        # Create our model type instance
        sk_learn_rfr = RandomForestRegressor(params)

        # Log params and metrics using the MLflow APIs
        mlflow.log_params(params)
        mlflow.log_metric("metric_1", random())
        mlflow.log_metric("metric_2", random() + 1)
        mlflow.log_metric("metric_3", random() + 2)

        # Set the notes for experiment and the Runs
        MlflowClient().set_experiment_tag(
            experiment_id, "mlflow.note.content",
            "This is experiment for getting started with MLflow ...")
        MlflowClient().set_tag(
            run_id, "mlflow.note.content",
            "This Run is for getting started with MLflow Model Registry ...")

        # Log the model at the same time
        mlflow.sklearn.log_model(sk_model=sk_learn_rfr,
                                 artifact_path="sklearn-model")

        # Create sample message artifact
        if not os.path.exists("messages"):
            os.makedirs("messages")
        with open("messages/message.txt", "w") as f:
            f.write(gen_random_text())

        mlflow.log_artifacts("messages")
        shutil.rmtree('messages')

        # Create scatter random plot artifacts file and log artifact
        for npoints in range(55, 70, 5):
            fig, ex = gen_random_scatter_plots(npoints)
            temp_file_name = Utils.get_temporary_directory_path(
                "scatter-plot-", ".png")
            temp_name = temp_file_name.name
            try:
                fig.savefig(temp_name)
                mlflow.log_artifact(temp_name, "scatter_plots")
            finally:
                temp_file_name.close()  # Delete the temp file

        return (run_id, experiment_id)
Draxl, C., B.M. Hodge, A. Clifton, and J. McCaa. 2015. "The Wind Integration National Dataset (WIND) Toolkit." Applied Energy 151: 355366.

Lieberman-Cribbin, W., C. Draxl, and A. Clifton. 2014. Guide to Using the WIND Toolkit Validation Code (Technical Report, NREL/TP-5000-62595). Golden, CO: National Renewable Energy Laboratory.

King, J., A. Clifton, and B.M. Hodge. 2014. Validation of Power Output for the WIND Toolkit (Technical Report, NREL/TP-5D00-61714). Golden, CO: National Renewable Energy Laboratory.
"""

if __name__ == "__main__":
    # Use sqlite:///mlruns.db as the local store for tracking and registery
    mlflow.set_tracking_uri("sqlite:///mlruns.db")

    # Load and print dataset
    csv_path = "data/windfarm_data.csv"

    # Use column 0 (date) as the index
    wind_farm_data = Utils.load_data(csv_path, index_col=0)
    Utils.print_pandas_dataset(wind_farm_data)

    # Get Validation data
    X_train, y_train = Utils.get_training_data(wind_farm_data)
    val_x, val_y = Utils.get_validation_data(wind_farm_data)

    # Train, fit and register our model
    params_list = [{
        "n_estimators": 100
    }, {
        "n_estimators": 200
    }, {
        "n_estimators": 300
    }]
import requests
from cls.utils import Utils
import json

(x_train, y_train), (val_x, val_y) = Utils.load_data()

data = val_x[0].reshape(1, -1)
data_json = json.dumps(data.tolist())
# print(data_json)
headers = {'Content-Type': 'application/json; format=pandas-records'}
request_uri = 'http://127.0.0.1:5000/invocations'

if __name__ == '__main__':
    try:
        response = requests.post(request_uri, data=data_json, headers=headers)
        print(response.content)
        print('done!!!')
    except Exception as ex:
        raise (ex)