Skip to content

Instantly share code, notes, and snippets.

@MachineLearningIsEasy
Created November 29, 2021 12:45
Show Gist options
  • Select an option

  • Save MachineLearningIsEasy/8614de5f3de88ec194b09d4d4c6d7c48 to your computer and use it in GitHub Desktop.

Select an option

Save MachineLearningIsEasy/8614de5f3de88ec194b09d4d4c6d7c48 to your computer and use it in GitHub Desktop.

Revisions

  1. MachineLearningIsEasy created this gist Nov 29, 2021.
    819 changes: 819 additions & 0 deletions mlflow_models.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,819 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 1,
    "id": "d5a2a0cb-4ebe-4b4a-8163-ccddc4ddfc30",
    "metadata": {},
    "outputs": [],
    "source": [
    "import mlflow\n",
    "import mlflow.sklearn\n",
    "import mlflow.tensorflow \n",
    "import tensorflow as tf\n",
    "from tensorflow.keras.layers import Dense\n",
    "from tensorflow.keras.models import Sequential\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import os\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.datasets import load_boston"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "id": "b6f2932e-c351-489a-8539-096ff1f0a4f2",
    "metadata": {},
    "outputs": [],
    "source": [
    "# https://mlflow.org/docs/latest/index.html"
    ]
    },
    {
    "cell_type": "markdown",
    "id": "d74f4ff6-0d31-4af9-8373-63c6d96d37c5",
    "metadata": {},
    "source": [
    "### Настройки подключения"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "id": "af03f99e-77b5-49f0-88e0-94762cb169b4",
    "metadata": {},
    "outputs": [],
    "source": [
    "# Указываем доступ в БД\n",
    "sql_string = \"postgresql://unicorn_user:magical_password@0.0.0.0/rainbow_database\"\n",
    "mlflow.set_tracking_uri(sql_string)"
    ]
    },
    {
    "cell_type": "markdown",
    "id": "59ad7367-eeb8-4447-a7a2-b069072fdc22",
    "metadata": {},
    "source": [
    "### Загрузка данных и обучение моделей"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "id": "48b5a6eb-ebb2-4774-8cdc-d5b79dfcf7f4",
    "metadata": {},
    "outputs": [],
    "source": [
    "data = load_boston()\n",
    "X = pd.DataFrame(data.data)\n",
    "y = data.target"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 7,
    "id": "ff1dd0c4-fdf3-4a4e-abb0-49091c9ff58b",
    "metadata": {},
    "outputs": [],
    "source": [
    "def train_tf_model(X, y):\n",
    " \n",
    " model = Sequential()\n",
    " model.add(Dense(50, input_shape=(X.shape[-1],), activation=\"relu\", name=\"hidden_layer\"))\n",
    " model.add(Dense(1))\n",
    " model.compile(loss=\"mse\", optimizer=\"adam\")\n",
    " \n",
    " model.fit(X, y, epochs=10, batch_size=64, validation_split=.2)\n",
    " return model"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 8,
    "id": "4b53d080-3f77-46e3-98a9-d426809bf984",
    "metadata": {},
    "outputs": [],
    "source": [
    "def train_sklearn_model(X, y):\n",
    " \n",
    " model = LinearRegression() \n",
    " model.fit(X, y)\n",
    " return model"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 9,
    "id": "d14588c3-cdc1-4589-ba88-aceea5f994b2",
    "metadata": {},
    "outputs": [],
    "source": [
    "# Создаем эксперимент и указываем хранилище артефактов\n",
    "expname = \"Boston_exp\"\n",
    "exp_id = mlflow.create_experiment(expname, artifact_location=\"s3://mlflow-example-bds\")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "id": "a368aa17-9245-4499-8b60-7cdde7766d6d",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'4'"
    ]
    },
    "execution_count": 10,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "exp_id"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 11,
    "id": "caea5ec0-f7a5-4251-9e72-c9f5bc3c9099",
    "metadata": {},
    "outputs": [
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "2021-11-29 15:30:09.596139: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
    "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
    "2021-11-29 15:30:09.794391: I tensorflow/core/profiler/lib/profiler_session.cc:131] Profiler session initializing.\n",
    "2021-11-29 15:30:09.794406: I tensorflow/core/profiler/lib/profiler_session.cc:146] Profiler session started.\n",
    "2021-11-29 15:30:09.795408: I tensorflow/core/profiler/lib/profiler_session.cc:164] Profiler session tear down.\n",
    "2021-11-29 15:30:11.244840: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)\n"
    ]
    },
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Epoch 1/10\n",
    "3/7 [===========>..................] - ETA: 0s - loss: 2293.2336WARNING:tensorflow:Callback method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0012s vs `on_train_batch_end` time: 0.0090s). Check your callbacks.\n",
    "7/7 [==============================] - 1s 30ms/step - loss: 1578.5481 - val_loss: 6507.6152\n"
    ]
    },
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "2021-11-29 15:30:11.709945: I tensorflow/core/profiler/lib/profiler_session.cc:131] Profiler session initializing.\n",
    "2021-11-29 15:30:11.709963: I tensorflow/core/profiler/lib/profiler_session.cc:146] Profiler session started.\n",
    "2021-11-29 15:30:11.714878: I tensorflow/core/profiler/lib/profiler_session.cc:66] Profiler session collecting data.\n",
    "2021-11-29 15:30:11.724263: I tensorflow/core/profiler/lib/profiler_session.cc:164] Profiler session tear down.\n",
    "2021-11-29 15:30:11.737336: I tensorflow/core/profiler/rpc/client/save_profile.cc:136] Creating directory: /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11\n",
    "\n",
    "2021-11-29 15:30:11.739096: I tensorflow/core/profiler/rpc/client/save_profile.cc:142] Dumped gzipped tool data for trace.json.gz to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.trace.json.gz\n",
    "2021-11-29 15:30:11.750789: I tensorflow/core/profiler/rpc/client/save_profile.cc:136] Creating directory: /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11\n",
    "\n",
    "2021-11-29 15:30:11.751044: I tensorflow/core/profiler/rpc/client/save_profile.cc:142] Dumped gzipped tool data for memory_profile.json.gz to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.memory_profile.json.gz\n",
    "2021-11-29 15:30:11.754133: I tensorflow/core/profiler/rpc/client/capture_profile.cc:251] Creating directory: /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11\n",
    "Dumped tool data for xplane.pb to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.xplane.pb\n",
    "Dumped tool data for overview_page.pb to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.overview_page.pb\n",
    "Dumped tool data for input_pipeline.pb to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.input_pipeline.pb\n",
    "Dumped tool data for tensorflow_stats.pb to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.tensorflow_stats.pb\n",
    "Dumped tool data for kernel_stats.pb to /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpegzn63c7/train/plugins/profile/2021_11_29_15_30_11/MacBook-Pro-MasterDelivery.local.kernel_stats.pb\n",
    "\n"
    ]
    },
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Epoch 2/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 715.7516 - val_loss: 3393.0291\n",
    "Epoch 3/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 466.3834 - val_loss: 2525.4910\n",
    "Epoch 4/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 242.5830 - val_loss: 2034.0503\n",
    "Epoch 5/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 185.8198 - val_loss: 1310.2159\n",
    "Epoch 6/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 142.6448 - val_loss: 998.5560\n",
    "Epoch 7/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 145.2558 - val_loss: 921.1584\n",
    "Epoch 8/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 139.0472 - val_loss: 894.2036\n",
    "Epoch 9/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 133.4951 - val_loss: 911.0057\n",
    "Epoch 10/10\n",
    "7/7 [==============================] - 0s 4ms/step - loss: 127.5104 - val_loss: 914.6703\n"
    ]
    },
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "2021-11-29 15:30:12.431758: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.\n"
    ]
    },
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "INFO:tensorflow:Assets written to: /var/folders/0f/0yc24w1x5fg93fk9thd7tqt00000gn/T/tmpka80aq4g/model/data/model/assets\n"
    ]
    }
    ],
    "source": [
    "with mlflow.start_run(experiment_id=exp_id, run_name='tf'):\n",
    " # Автоматом сохраняет model's parameters, metrics, artifacts\n",
    " mlflow.tensorflow.autolog()\n",
    " train_tf_model(X, y)\n",
    " run_id = mlflow.active_run().info.run_id"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 12,
    "id": "9a9195a7-29f4-4a34-a725-2a03db163c84",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'7162708fb83942feae6013c06454491a'"
    ]
    },
    "execution_count": 12,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "run_id"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "id": "f9624802-fad9-4bfa-a94b-e65d148fd20e",
    "metadata": {},
    "outputs": [],
    "source": [
    "with mlflow.start_run(experiment_id=exp_id, run_name='sk'):\n",
    " # Автоматом сохраняет model's parameters, metrics, artifacts\n",
    " mlflow.sklearn.autolog()\n",
    " train_sklearn_model(X, y)\n",
    " run_id_sk = mlflow.active_run().info.run_id"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 14,
    "id": "20c8fe8c-178b-4f9c-8a6f-39b987beaa7e",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'a7cd2a31c2cc4379a990a304c74a3b66'"
    ]
    },
    "execution_count": 14,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "run_id_sk"
    ]
    },
    {
    "cell_type": "markdown",
    "id": "dab5b8ad-d04e-4849-9e08-e30d839b10a2",
    "metadata": {},
    "source": [
    "### Mlflow API"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 15,
    "id": "de77621e-c984-4a27-9b05-547a917bbdfa",
    "metadata": {},
    "outputs": [
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "Successfully registered model 'model_boston_tf'.\n",
    "2021/11/29 15:34:06 INFO mlflow.tracking._model_registry.client: Waiting up to 300 seconds for model version to finish creation. Model name: model_boston_tf, version 1\n",
    "Created version '1' of model 'model_boston_tf'.\n"
    ]
    }
    ],
    "source": [
    "# Задаем имя модели\n",
    "model_name = \"model_boston_tf\"\n",
    " \n",
    "# Стандартный путь к каталогу с моделями\n",
    "artifact_path = \"model\"\n",
    "model_uri = \"runs:/{run_id}/{artifact_path}\".format(run_id=run_id, artifact_path=artifact_path)\n",
    " \n",
    "model_details_tf = mlflow.register_model(model_uri=model_uri, name=model_name)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 16,
    "id": "896a433c-37ed-407c-a184-9d5ff3485e7a",
    "metadata": {},
    "outputs": [
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "Successfully registered model 'model_boston_sk'.\n",
    "2021/11/29 15:34:17 INFO mlflow.tracking._model_registry.client: Waiting up to 300 seconds for model version to finish creation. Model name: model_boston_sk, version 1\n",
    "Created version '1' of model 'model_boston_sk'.\n"
    ]
    }
    ],
    "source": [
    "# Задаем имя модели\n",
    "model_name = \"model_boston_sk\"\n",
    " \n",
    "# Стандартный путь к каталогу с моделями\n",
    "artifact_path = \"model\"\n",
    "model_uri = \"runs:/{run_id}/{artifact_path}\".format(run_id=run_id, artifact_path=artifact_path)\n",
    " \n",
    "model_details_sk = mlflow.register_model(model_uri=model_uri, name=model_name)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 17,
    "id": "9a7d72d9-e062-4245-bccf-da9608ea2b55",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<RegisteredModel: creation_timestamp=1638189257839, description='Линейная регрессия', last_updated_timestamp=1638189293487, latest_versions=[<ModelVersion: creation_timestamp=1638189257875, current_stage='None', description=None, last_updated_timestamp=1638189257875, name='model_boston_sk', run_id='7162708fb83942feae6013c06454491a', run_link=None, source='s3://mlflow-example-bds/7162708fb83942feae6013c06454491a/artifacts/model', status='READY', status_message=None, tags={}, user_id=None, version=1>], name='model_boston_sk', tags={}>"
    ]
    },
    "execution_count": 17,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "from mlflow.tracking.client import MlflowClient\n",
    "# Описание совокупности моделей\n",
    "client = MlflowClient()\n",
    "client.update_registered_model(\n",
    " name=model_details_tf.name,\n",
    " description=\"Нейронная сетка\"\n",
    ")\n",
    "client.update_registered_model(\n",
    " name=model_details_sk.name,\n",
    " description=\"Линейная регрессия\"\n",
    ")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 18,
    "id": "af26a63d-f360-4a1b-af5e-50060217c789",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<ModelVersion: creation_timestamp=1638189246419, current_stage='None', description='Ванильная нейронная сеть.', last_updated_timestamp=1638189310804, name='model_boston_tf', run_id='7162708fb83942feae6013c06454491a', run_link=None, source='s3://mlflow-example-bds/7162708fb83942feae6013c06454491a/artifacts/model', status='READY', status_message=None, tags={}, user_id=None, version=1>"
    ]
    },
    "execution_count": 18,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "# Описание версии моделей\n",
    "client.update_model_version(\n",
    " name=model_details_tf.name,\n",
    " version=model_details_tf.version,\n",
    " description=\"Ванильная нейронная сеть.\"\n",
    ")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 19,
    "id": "5a43e2c4-05a0-485a-9be7-2fd748b95d3a",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<ModelVersion: creation_timestamp=1638189246419, current_stage='Production', description='Ванильная нейронная сеть.', last_updated_timestamp=1638189319988, name='model_boston_tf', run_id='7162708fb83942feae6013c06454491a', run_link=None, source='s3://mlflow-example-bds/7162708fb83942feae6013c06454491a/artifacts/model', status='READY', status_message=None, tags={}, user_id=None, version=1>"
    ]
    },
    "execution_count": 19,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "# Изменяем ТЕГ модели\n",
    "client.transition_model_version_stage(\n",
    " name=model_details_tf.name,\n",
    " version=model_details_tf.version,\n",
    " stage='Production',\n",
    ")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 23,
    "id": "3208439d-6f27-4e09-9e76-0bb25fbef9ba",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<ModelVersion: creation_timestamp=1637826757683, current_stage='Production', description=None, last_updated_timestamp=1637827787018, name='model_boston_tf', run_id='16f6872e1a0648e993b99595f2f0bea3', run_link=None, source='s3://s3-mlflow/16f6872e1a0648e993b99595f2f0bea3/artifacts/model', status='READY', status_message=None, tags={}, user_id=None, version=1>"
    ]
    },
    "execution_count": 23,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "# Изменяем ТЕГ модели\n",
    "client.transition_model_version_stage(\n",
    " name=model_details_tf.name,\n",
    " version='1',\n",
    " stage='Production',\n",
    ")"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 24,
    "id": "959dc1a8-8957-4cec-8471-fa35fadd98c3",
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "The current model stage is: 'None'\n"
    ]
    }
    ],
    "source": [
    "# Смотрим на статус модели по имени\n",
    "model_version_details = client.get_model_version(\n",
    " name=model_details_tf.name,\n",
    " version=model_details_tf.version,\n",
    ")\n",
    "print(\"The current model stage is: '{stage}'\".format(stage=model_details_tf.current_stage))"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 19,
    "id": "13c23c10-dde1-4ae2-ba27-3834c9f51644",
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "The latest production version of the model 'power-forecasting-model' is '1'.\n"
    ]
    }
    ],
    "source": [
    "latest_version_info = client.get_latest_versions(model_name, stages=[\"Production\"])\n",
    "latest_production_version = latest_version_info[0].version\n",
    "print(\"The latest production version of the model '%s' is '%s'.\" % (model_name, latest_production_version))"
    ]
    },
    {
    "cell_type": "markdown",
    "id": "81ee6bd2-b5c4-4125-8d73-04eee8e9d3fa",
    "metadata": {},
    "source": [
    "#### experiment_id & run_id"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 36,
    "id": "2b25c254-75e5-4587-ba41-19db98126f3f",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[<Experiment: artifact_location='s3://s3-mlflow', experiment_id='73', lifecycle_stage='active', name='Test_last', tags={}>,\n",
    " <Experiment: artifact_location='s3://s3-mlflow', experiment_id='76', lifecycle_stage='active', name='tensorflow', tags={}>,\n",
    " <Experiment: artifact_location='s3://s3-mlflow', experiment_id='77', lifecycle_stage='active', name='sklearn', tags={}>,\n",
    " <Experiment: artifact_location='s3://s3-mlflow', experiment_id='78', lifecycle_stage='active', name='Boston', tags={}>,\n",
    " <Experiment: artifact_location='s3://s3-mlflow', experiment_id='79', lifecycle_stage='active', name='Boston_exp', tags={}>]"
    ]
    },
    "execution_count": 36,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "experiments = client.list_experiments()\n",
    "experiments"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 40,
    "id": "8899f56e-ff43-4cae-a0fa-edcf53070f93",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'79'"
    ]
    },
    "execution_count": 40,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "experiments[-1].experiment_id"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 41,
    "id": "669c5c37-9fe9-40f0-951b-b5a6ba39be06",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "<Run: data=<RunData: metrics={}, params={}, tags={}>, info=<RunInfo: artifact_uri='s3://s3-mlflow/94d0d9670d11443eae2a70e4f90f7baa/artifacts', end_time=None, experiment_id='79', lifecycle_stage='active', run_id='94d0d9670d11443eae2a70e4f90f7baa', run_uuid='94d0d9670d11443eae2a70e4f90f7baa', start_time=1637872007314, status='RUNNING', user_id='unknown'>>"
    ]
    },
    "execution_count": 41,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "run = client.create_run(experiments[-1].experiment_id)\n",
    "run"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 42,
    "id": "c8dc7a44-d075-4aae-955d-2d0544a5b064",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "[<RunInfo: artifact_uri='s3://s3-mlflow/94d0d9670d11443eae2a70e4f90f7baa/artifacts', end_time=None, experiment_id='79', lifecycle_stage='active', run_id='94d0d9670d11443eae2a70e4f90f7baa', run_uuid='94d0d9670d11443eae2a70e4f90f7baa', start_time=1637872007314, status='RUNNING', user_id='unknown'>,\n",
    " <RunInfo: artifact_uri='s3://s3-mlflow/ec386d2a3ead4f98a867d9c5716d0995/artifacts', end_time=1637827675438, experiment_id='79', lifecycle_stage='active', run_id='ec386d2a3ead4f98a867d9c5716d0995', run_uuid='ec386d2a3ead4f98a867d9c5716d0995', start_time=1637827662912, status='FINISHED', user_id='masterdelivery'>,\n",
    " <RunInfo: artifact_uri='s3://s3-mlflow/af2ff1403ba040a4bd4587f251d86b90/artifacts', end_time=1637826456404, experiment_id='79', lifecycle_stage='active', run_id='af2ff1403ba040a4bd4587f251d86b90', run_uuid='af2ff1403ba040a4bd4587f251d86b90', start_time=1637826451765, status='FINISHED', user_id='masterdelivery'>,\n",
    " <RunInfo: artifact_uri='s3://s3-mlflow/16f6872e1a0648e993b99595f2f0bea3/artifacts', end_time=1637826425021, experiment_id='79', lifecycle_stage='active', run_id='16f6872e1a0648e993b99595f2f0bea3', run_uuid='16f6872e1a0648e993b99595f2f0bea3', start_time=1637826413087, status='FINISHED', user_id='masterdelivery'>,\n",
    " <RunInfo: artifact_uri='s3://s3-mlflow/e3b78aafb6654bf39fa358fbd04dc313/artifacts', end_time=1637826407743, experiment_id='79', lifecycle_stage='active', run_id='e3b78aafb6654bf39fa358fbd04dc313', run_uuid='e3b78aafb6654bf39fa358fbd04dc313', start_time=1637826407571, status='FAILED', user_id='masterdelivery'>,\n",
    " <RunInfo: artifact_uri='s3://s3-mlflow/23c4b1fed8444ddaa09da4b7a01d4268/artifacts', end_time=1637826395466, experiment_id='79', lifecycle_stage='active', run_id='23c4b1fed8444ddaa09da4b7a01d4268', run_uuid='23c4b1fed8444ddaa09da4b7a01d4268', start_time=1637826395249, status='FAILED', user_id='masterdelivery'>]"
    ]
    },
    "execution_count": 42,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "client.list_run_infos(experiments[-1].experiment_id)"
    ]
    },
    {
    "cell_type": "markdown",
    "id": "90b4e0d0-2cb3-4e07-addb-efe9db6a899e",
    "metadata": {},
    "source": [
    "### Грузим модель"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 20,
    "id": "d8f4157c-be8a-4f55-998f-a1bd98e70ddf",
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Loading registered model version from URI: 'models:/model_boston_sk/1'\n"
    ]
    },
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/backend.py:401: UserWarning: `tf.keras.backend.set_learning_phase` is deprecated and will be removed after 2020-10-11. To update it, simply pass a True/False value to the `training` argument of the `__call__` method of your layer or model.\n",
    " warnings.warn('`tf.keras.backend.set_learning_phase` is deprecated and '\n"
    ]
    }
    ],
    "source": [
    "import mlflow.pyfunc\n",
    " \n",
    "model_version_uri = \"models:/{model_name}/1\".format(model_name=model_details_sk.name)\n",
    " \n",
    "print(\"Loading registered model version from URI: '{model_uri}'\".format(model_uri=model_version_uri))\n",
    "model_sk = mlflow.pyfunc.load_model(model_version_uri)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 21,
    "id": "f57d8be5-235b-44c7-818e-ed1859150beb",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "mlflow.pyfunc.PyFuncModel"
    ]
    },
    "execution_count": 21,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "type(model_sk)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 22,
    "id": "fb00edd8-892d-4e8d-8189-1af9d6f97a12",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/html": [
    "<div>\n",
    "<style scoped>\n",
    " .dataframe tbody tr th:only-of-type {\n",
    " vertical-align: middle;\n",
    " }\n",
    "\n",
    " .dataframe tbody tr th {\n",
    " vertical-align: top;\n",
    " }\n",
    "\n",
    " .dataframe thead th {\n",
    " text-align: right;\n",
    " }\n",
    "</style>\n",
    "<table border=\"1\" class=\"dataframe\">\n",
    " <thead>\n",
    " <tr style=\"text-align: right;\">\n",
    " <th></th>\n",
    " <th>0</th>\n",
    " </tr>\n",
    " </thead>\n",
    " <tbody>\n",
    " <tr>\n",
    " <th>0</th>\n",
    " <td>25.967667</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>1</th>\n",
    " <td>23.962097</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>2</th>\n",
    " <td>26.217857</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>3</th>\n",
    " <td>28.784697</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>4</th>\n",
    " <td>27.669765</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>...</th>\n",
    " <td>...</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>501</th>\n",
    " <td>24.977636</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>502</th>\n",
    " <td>24.587610</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>503</th>\n",
    " <td>23.871611</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>504</th>\n",
    " <td>23.722614</td>\n",
    " </tr>\n",
    " <tr>\n",
    " <th>505</th>\n",
    " <td>24.410461</td>\n",
    " </tr>\n",
    " </tbody>\n",
    "</table>\n",
    "<p>506 rows × 1 columns</p>\n",
    "</div>"
    ],
    "text/plain": [
    " 0\n",
    "0 25.967667\n",
    "1 23.962097\n",
    "2 26.217857\n",
    "3 28.784697\n",
    "4 27.669765\n",
    ".. ...\n",
    "501 24.977636\n",
    "502 24.587610\n",
    "503 23.871611\n",
    "504 23.722614\n",
    "505 24.410461\n",
    "\n",
    "[506 rows x 1 columns]"
    ]
    },
    "execution_count": 22,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "y_predict = model_sk.predict(X)\n",
    "y_predict"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 23,
    "id": "88d0f569-89e7-48f0-ad54-2b64f778cf0c",
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "10.684860846930343"
    ]
    },
    "execution_count": 23,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "from sklearn.metrics import mean_absolute_error\n",
    "mean_absolute_error(y,y_predict)"
    ]
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 3 (ipykernel)",
    "language": "python",
    "name": "python3"
    },
    "language_info": {
    "codemirror_mode": {
    "name": "ipython",
    "version": 3
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
    "version": "3.9.6"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 5
    }