Add MLFlow model generator && serving it

This commit is contained in:
Wojciech Jarmosz 2021-05-23 14:53:17 +02:00
parent b82917a5d9
commit 925b5e71ce
16 changed files with 121 additions and 0 deletions

View File

@ -20,8 +20,14 @@ pipeline {
sh "python3 sacred_exp.py"
}
}
stage("Run MLFlow training"){
steps {
sh "python3 ml_model.py ${verbose} ${epochs}"
}
}
stage('Save trained model files') {
steps{
archiveArtifacts 'movies_on_streaming_platforms_model/**'
archiveArtifacts 'sacred_file/**'
archiveArtifacts 'linear_regression.h5'
}

54
ml_model.py Normal file
View File

@ -0,0 +1,54 @@
import pandas as pd
import numpy as np
import tensorflow as tf
import os.path
import mlflow
import sys
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
arguments = sys.argv[1:]
verbose = int(arguments[0])
epochs = int(arguments[1])
# Wczytanie danych
train_data = pd.read_csv("./MoviesOnStreamingPlatforms_updated.train")
test_data = pd.read_csv("./MoviesOnStreamingPlatforms_updated.test")
# Stworzenie modelu
columns_to_use = ['Year', 'Runtime', 'Netflix']
train_X = tf.convert_to_tensor(train_data[columns_to_use])
train_Y = tf.convert_to_tensor(train_data[["IMDb"]])
test_X = tf.convert_to_tensor(test_data[columns_to_use])
test_Y = tf.convert_to_tensor(test_data[["IMDb"]])
normalizer = preprocessing.Normalization(input_shape=[3,])
normalizer.adapt(train_X)
model = keras.Sequential([
keras.Input(shape=(len(columns_to_use),)),
normalizer,
layers.Dense(30, activation='relu'),
layers.Dense(10, activation='relu'),
layers.Dense(25, activation='relu'),
layers.Dense(1)
])
model.compile(loss='mean_absolute_error',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=[tf.keras.metrics.RootMeanSquaredError()])
model.fit(train_X, train_Y, verbose=verbose, epochs=epochs)
signature = mlflow.models.signature.infer_signature(train_X.numpy(), model.predict(train_X.numpy()))
input_data = test_X
with mlflow.start_run():
mlflow.keras.save_model(model, "movies_on_streaming_platforms_model", input_example=input_data.numpy(), signature=signature)

View File

@ -0,0 +1,15 @@
artifact_uri: file:///Volumes/seagate/ium_434704/mlruns/0/724dc0d672664057b760fe5f18801036/artifacts
end_time: 1621772380826
entry_point_name: ''
experiment_id: '0'
lifecycle_stage: active
name: ''
run_id: 724dc0d672664057b760fe5f18801036
run_uuid: 724dc0d672664057b760fe5f18801036
source_name: ''
source_type: 4
source_version: ''
start_time: 1621772379167
status: 3
tags: []
user_id: wj

View File

@ -0,0 +1 @@
b82917a5d90e071de9b67f2b6648be0353c54b62

View File

@ -0,0 +1 @@
ml_model.py

View File

@ -0,0 +1 @@
LOCAL

View File

@ -0,0 +1 @@
wj

View File

@ -0,0 +1,20 @@
flavors:
keras:
data: data
keras_module: tensorflow.keras
keras_version: 2.5.0
save_format: tf
python_function:
data: data
env: conda.yaml
loader_module: mlflow.keras
python_version: 3.9.1
saved_input_example_info:
artifact_path: input_example.json
format: tf-serving
type: ndarray
signature:
inputs: '[{"type": "tensor", "tensor-spec": {"dtype": "float64", "shape": [-1, 3]}}]'
outputs: '[{"type": "tensor", "tensor-spec": {"dtype": "float32", "shape": [-1,
1]}}]'
utc_time_created: '2021-05-23 12:19:39.663838'

View File

@ -0,0 +1,10 @@
channels:
- defaults
- conda-forge
dependencies:
- python=3.9.1
- pip
- pip:
- mlflow
- tensorflow==2.5.0-rc1
name: mlflow-env

View File

@ -0,0 +1 @@
tensorflow.keras

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
tf

File diff suppressed because one or more lines are too long