From 531f469e4eb6fc1bdf9584ece946caa292885092 Mon Sep 17 00:00:00 2001 From: Maciej Czajka Date: Wed, 11 May 2022 15:08:32 +0200 Subject: [PATCH] update jenkinsfile_train-sacred, dllib-mlflow.py --- Biblioteka_DL/dllib-mlflow.py | 53 ++++++++++++++++++----------------- Jenkinsfile_train-sacred | 35 +++++++---------------- 2 files changed, 38 insertions(+), 50 deletions(-) diff --git a/Biblioteka_DL/dllib-mlflow.py b/Biblioteka_DL/dllib-mlflow.py index 59d5968..f219e99 100644 --- a/Biblioteka_DL/dllib-mlflow.py +++ b/Biblioteka_DL/dllib-mlflow.py @@ -1,4 +1,4 @@ -import numpy as np +67import numpy as np import sys import os import torch @@ -237,6 +237,7 @@ def remove_list(games): # 'user_review': games['user_review']} # features_g = pd.DataFrame(features_g, dtype=np.float64) # features_g = features_g.to_numpy() +epochs = int(sys.argv[1]) if len(sys.argv) > 1 else 20 def my_main(epochs): platform = pd.read_csv('all_games.train.csv', sep=',', usecols=[1], header=None).values.tolist() @@ -290,41 +291,43 @@ def my_main(epochs): loss_fn = nn.CrossEntropyLoss() # epochs = 1000 # epochs = epochs - epochs = int(sys.argv[1]) if len(sys.argv) > 1 else 20 - mlflow.log_param("epochs", epochs) def print_(loss): print ("The loss calculated: ", loss) + x_train, y_train = Variable(torch.from_numpy(features_train_g)).float(), Variable( + torch.from_numpy(labels_train_g)).long() + for epoch in range(1, epochs + 1): + print("Epoch #", epoch) + y_pred = model(x_train) - with mlflow.start_run() as run: - x_train, y_train = Variable(torch.from_numpy(features_train_g)).float(), Variable(torch.from_numpy(labels_train_g)).long() - for epoch in range(1, epochs + 1): - print("Epoch #", epoch) - y_pred = model(x_train) + loss = loss_fn(y_pred, y_train.squeeze(-1)) + print_(loss.item()) - loss = loss_fn(y_pred, y_train.squeeze(-1)) - print_(loss.item()) + # Zero gradients + optimizer.zero_grad() + loss.backward() # Gradients + optimizer.step() # Update - # Zero gradients - optimizer.zero_grad() - loss.backward() # Gradients - optimizer.step() # Update + # Prediction + x_test = Variable(torch.from_numpy(features_test_g)).float() + pred = model(x_test) - # Prediction - x_test = Variable(torch.from_numpy(features_test_g)).float() - pred = model(x_test) + pred = pred.detach().numpy() - pred = pred.detach().numpy() + print("The accuracy is", accuracy_score(labels_test_g, np.argmax(pred, axis=1))) + # mlflow.log_metric("accuracy", accuracy_score(labels_test_g, np.argmax(pred, axis=1))) - print("The accuracy is", accuracy_score(labels_test_g, np.argmax(pred, axis=1))) - mlflow.log_metric("accuracy", accuracy_score(labels_test_g, np.argmax(pred, axis=1))) + pred = pd.DataFrame(pred) - pred = pd.DataFrame(pred) + pred.to_csv('result.csv') - pred.to_csv('result.csv') - - # save model - torch.save(model, "games_model.pkl") + # save model + torch.save(model, "games_model.pkl") + return accuracy_score(labels_test_g, np.argmax(pred, axis=1)) +with mlflow.start_run() as run: + acc = my_main(epochs) + mlflow.log_param("epochs", epochs) + mlflow.log_metric("accuracy", acc) \ No newline at end of file diff --git a/Jenkinsfile_train-sacred b/Jenkinsfile_train-sacred index 7a87075..f010fc4 100644 --- a/Jenkinsfile_train-sacred +++ b/Jenkinsfile_train-sacred @@ -1,12 +1,12 @@ pipeline { agent { - dockerfile { - additionalBuildArgs "--build-arg KAGGLE_USERNAME=${params.KAGGLE_USERNAME} --build-arg KAGGLE_KEY=${params.KAGGLE_KEY} --build-arg CUTOFF=${params.CUTOFF} -t maciejczajka" - } + docker { + image 'maciejczajka' + } } parameters { string( - defaultValue: '1000', + defaultValue: '100', description: 'Number of epochs', name: 'EPOCHS', trim: false @@ -16,28 +16,13 @@ pipeline { stage('Script'){ steps { copyArtifacts filter: '*', projectName: 's444356-create-dataset' - sh 'python Biblioteka_DL/dllib-sacred.py with "epochs=$EPOCHS"' - sh 'ls my_res' - sh 'cp -r my_res res' - archiveArtifacts artifacts: 'games_model.pkl, res/**/*.*' - sh 'rm -r my_res' - sh 'rm -r res' - build job: 's444356-evaluation/master/' + sh "python Biblioteka_DL/dllib-mlflow.py -e $EPOCHS" + archiveArtifacts artifacts: 'games_model.pkl' + sh 'ls -al' + sh 'cat MLProject' + sh 'ls mlruns' + archiveArtifacts artifacts: 'mlruns/**' } } } - post { - success { - emailext body: 'SUCCESS', subject: 's444356-training', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms' - } - failure { - emailext body: 'FAILURE', subject: 's444356-training', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms' - } - unstable { - emailext body: 'UNSTABLE', subject: 's444356-training', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms' - } - changed { - emailext body: 'CHANGED', subject: 's444356-training', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms' - } - } } \ No newline at end of file