From 197efd7bcbf0451e9bc9a9c1bf5d776e1a8e66e5 Mon Sep 17 00:00:00 2001 From: Zofia Galla Date: Sun, 16 May 2021 22:25:46 +0200 Subject: [PATCH] Add MLflow --- Dockerfile | 1 + Jenkinsfile | 6 +++++ MLproject | 10 ++++++++ ium_zadanie6_training.py | 53 ++++++++++++++++++++++------------------ ium_zadanie8.py | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 MLproject create mode 100644 ium_zadanie8.py diff --git a/Dockerfile b/Dockerfile index 7ccfd05..7247dc8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,6 +18,7 @@ RUN pip3 install matplotlib RUN pip3 install --no-cache-dir tensorflow RUN pip3 install sacred RUN pip3 install pymongo +RUN pip3 install mlflow CMD ./run.sh diff --git a/Jenkinsfile b/Jenkinsfile index 66a0895..3afefd2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -18,4 +18,10 @@ pipeline { } } } + post { + success { + + build job: 's434684-training/master' + } + } } diff --git a/MLproject b/MLproject new file mode 100644 index 0000000..a7883b6 --- /dev/null +++ b/MLproject @@ -0,0 +1,10 @@ +name: 434684-mlflow + +docker_env: + image: zollinka/ium:latest + +entry_points: + main: + parameters: + learning_rate: {type: float, default: 0.0001} + command: "python3 ium_zadanie6_training.py {learning_rate}" \ No newline at end of file diff --git a/ium_zadanie6_training.py b/ium_zadanie6_training.py index d80cd15..15e4713 100644 --- a/ium_zadanie6_training.py +++ b/ium_zadanie6_training.py @@ -10,6 +10,7 @@ from sacred.observers import FileStorageObserver, MongoObserver from sacred import Experiment from sacred.observers import MongoObserver from datetime import datetime +import mlflow ex = Experiment("434684", interactive=False, save_git_info=False) @@ -23,34 +24,38 @@ def my_config(): @ex.capture def prepare_train_model(learning_rate, _run): - _run.info["prepare_model"] = str(datetime.now()) + with mlflow.start_run(): + _run.info["prepare_model"] = str(datetime.now()) - movies_train = pd.read_csv('movies_train.csv') + movies_train = pd.read_csv('movies_train.csv') - x_train = movies_train.copy() - y_train = x_train.pop('rottentomatoes_audience_score') - x_train.pop('Unnamed: 0') + x_train = movies_train.copy() + y_train = x_train.pop('rottentomatoes_audience_score') + x_train.pop('Unnamed: 0') - learning_rate = float(sys.argv[1]) - model = Sequential() - model.add(layers.Input(shape=(22,))) - model.add(layers.Dense(64)) - model.add(layers.Dense(64)) - model.add(layers.Dense(32)) - model.add(layers.Dense(1)) - - model.compile(loss='mean_absolute_error', optimizer=Adam(learning_rate)) - - _run.info["train model"] = str(datetime.now()) - - history = model.fit( - x = convert_to_tensor(x_train, np.float32), - y = y_train, - verbose=0, epochs=99) + learning_rate = float(sys.argv[1]) + model = Sequential() + model.add(layers.Input(shape=(22,))) + model.add(layers.Dense(64)) + model.add(layers.Dense(64)) + model.add(layers.Dense(32)) + model.add(layers.Dense(1)) - loss = history.history['loss'][-1] - _run.info["Loss"] = str(loss) - model.save('model_movies.h5') + mlflow.log_param("learning_rate", learning_rate) + + model.compile(loss='mean_absolute_error', optimizer=Adam(learning_rate)) + + _run.info["train model"] = str(datetime.now()) + + history = model.fit( + x = convert_to_tensor(x_train, np.float32), + y = y_train, + verbose=0, epochs=99) + + loss = history.history['loss'][-1] + _run.info["Loss"] = str(loss) + mlflow.log_metric("Loss", loss) + model.save('model_movies.h5') @ex.main diff --git a/ium_zadanie8.py b/ium_zadanie8.py new file mode 100644 index 0000000..b10b484 --- /dev/null +++ b/ium_zadanie8.py @@ -0,0 +1,46 @@ +import sys +from tensorflow.keras import layers, Sequential +# from keras.layers import Flatten,Dense,Dropout, GlobalAveragePooling2D +from tensorflow.keras.optimizers import Adam +from tensorflow import convert_to_tensor +import numpy as np +import pandas as pd +from sklearn.metrics import mean_squared_error +from sacred.observers import FileStorageObserver, MongoObserver +from sacred import Experiment +from sacred.observers import MongoObserver +from datetime import datetime +import mlflow + +with mlflow.start_run(): + learning_rate = float(sys.argv[1]) + + movies_train = pd.read_csv('movies_train.csv') + + x_train = movies_train.copy() + y_train = x_train.pop('rottentomatoes_audience_score') + x_train.pop('Unnamed: 0') + + learning_rate = float(sys.argv[1]) + model = Sequential() + model.add(layers.Input(shape=(22,))) + model.add(layers.Dense(64)) + model.add(layers.Dense(64)) + model.add(layers.Dense(32)) + model.add(layers.Dense(1)) + + mlflow.log_param("learning_rate", learning_rate) + + model.compile(loss='mean_absolute_error', optimizer=Adam(learning_rate)) + + + history = model.fit( + x = convert_to_tensor(x_train, np.float32), + y = y_train, + verbose=0, epochs=99) + + loss = history.history['loss'][-1] + + mlflow.log_metric("Loss", loss) + model.save('model_movies.h5') +