From cbc41748d5b0129c46a8ce219a58f781d91de709 Mon Sep 17 00:00:00 2001 From: s434732 Date: Sun, 16 May 2021 15:24:58 +0200 Subject: [PATCH] mlflow --- DEATH_EVENT.pth | Bin 1151 -> 1141 bytes IUM_08_mlflow.py | 78 ++++++++++++++++++ MLproject | 11 +++ __pycache__/IUM_08_mlflow.cpython-36.pyc | Bin 0 -> 2552 bytes fileObserver.py | 2 +- .../meta.yaml | 15 ++++ .../metrics/accuracy_score | 1 + .../tags/mlflow.source.git.commit | 1 + .../tags/mlflow.source.name | 1 + .../tags/mlflow.source.type | 1 + .../tags/mlflow.user | 1 + .../meta.yaml | 15 ++++ .../metrics/accuracy_score | 1 + .../params/Last loss | 1 + .../params/batch size | 1 + .../params/epochs | 1 + .../tags/mlflow.source.git.commit | 1 + .../tags/mlflow.source.name | 1 + .../tags/mlflow.source.type | 1 + .../tags/mlflow.user | 1 + mlruns/0/meta.yaml | 4 + 21 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 IUM_08_mlflow.py create mode 100644 MLproject create mode 100644 __pycache__/IUM_08_mlflow.cpython-36.pyc create mode 100644 mlruns/0/9031aa80d3b948fca55268f6f0b0c117/meta.yaml create mode 100644 mlruns/0/9031aa80d3b948fca55268f6f0b0c117/metrics/accuracy_score create mode 100644 mlruns/0/9031aa80d3b948fca55268f6f0b0c117/tags/mlflow.source.git.commit create mode 100644 mlruns/0/9031aa80d3b948fca55268f6f0b0c117/tags/mlflow.source.name create mode 100644 mlruns/0/9031aa80d3b948fca55268f6f0b0c117/tags/mlflow.source.type create mode 100644 mlruns/0/9031aa80d3b948fca55268f6f0b0c117/tags/mlflow.user create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/meta.yaml create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/metrics/accuracy_score create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/params/Last loss create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/params/batch size create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/params/epochs create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/tags/mlflow.source.git.commit create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/tags/mlflow.source.name create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/tags/mlflow.source.type create mode 100644 mlruns/0/b5ca2a8b58714cbdb4203ff66b024557/tags/mlflow.user create mode 100644 mlruns/0/meta.yaml diff --git a/DEATH_EVENT.pth b/DEATH_EVENT.pth index 98e7250733c7376f109ac6946d08f5adde4f0ed7..2ce2b691958e76c6c3fcb5e07653a694305bab0a 100644 GIT binary patch delta 283 zcmey*@s(r30Uizp1_ld5Q*&b@W21=|y&!yJBSRAl6N||@j6Iwy!ad}&7#SF{CLd&! z)e&V7W=Jeb&d4lF)lW$*Nz{kwb#vlph(ZOEn8K7q83fVPL(O(`;)ST6T*0&o$r5d5 zM|Dx4n-E|Z&~mI+PM*OmgQnJo30*DBD-W1cB^iMxfq)P*gbwg#V+XN>C&#gfbAeSQrwEk~1>PQuR|3OA_@_jB#^fV3@=d22{m^LlwweHzy8;$rVhiIN@eb)@F9} zlma>m0geFe!);N3HzN~BEe8igN0{6G`7A&t2un@gz%0iLcNf?v*O*hSxPc~ufDki; d4)A7U2eD*9nt%W#9pKH%24b@SAxJ$$EdcZ;I>G<| diff --git a/IUM_08_mlflow.py b/IUM_08_mlflow.py new file mode 100644 index 0000000..e816b8e --- /dev/null +++ b/IUM_08_mlflow.py @@ -0,0 +1,78 @@ +import torch +import sys +from torch import nn +import numpy as np +import pandas as pd +import mlflow +from sklearn.metrics import accuracy_score + +np.set_printoptions(suppress=False) + + +class LogisticRegressionModel(nn.Module): + def __init__(self, input_dim, output_dim): + super(LogisticRegressionModel, self).__init__() + self.linear = nn.Linear(input_dim, output_dim) + self.sigmoid = nn.Sigmoid() + def forward(self, x): + out = self.linear(x) + return self.sigmoid(out) + + + +def readAndtrain(epochs, batch_size): + train = pd.read_csv("train.csv") + test = pd.read_csv("test.csv") + + xtrain = train[['age','anaemia','creatinine_phosphokinase','diabetes', 'ejection_fraction', 'high_blood_pressure', 'platelets', 'serum_creatinine', 'serum_sodium', 'sex', 'smoking']].astype(np.float32) + ytrain = train['DEATH_EVENT'].astype(np.float32) + xtest = test[['age','anaemia','creatinine_phosphokinase','diabetes', 'ejection_fraction', 'high_blood_pressure', 'platelets', 'serum_creatinine', 'serum_sodium', 'sex', 'smoking']].astype(np.float32) + ytest = test['DEATH_EVENT'].astype(np.float32) + xTrain = torch.from_numpy(xtrain.values) + yTrain = torch.from_numpy(ytrain.values.reshape(179,1)) + xTest = torch.from_numpy(xtest.values) + yTest = torch.from_numpy(ytest.values) + + learning_rate = 0.002 + input_dim = 11 + output_dim = 1 + + model = LogisticRegressionModel(input_dim, output_dim) + model.load_state_dict(torch.load('DEATH_EVENT.pth')) + + criterion = torch.nn.BCELoss(reduction='mean') + optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) + + for epoch in range(epochs): + # print ("Epoch #",epoch) + model.train() + optimizer.zero_grad() + # Forward pass + y_pred = model(xTrain) + # Compute Loss + loss = criterion(y_pred, yTrain) + # print(loss.item()) + # Backward pass + loss.backward() + optimizer.step() + + + torch.save(model.state_dict(), 'DEATH_EVENT.pth') + prediction= model(xTest) + + return prediction, loss.item(), yTest + + print("accuracy_score", accuracy_score(yTest, np.argmax(prediction.detach().numpy(), axis=1))) + # print("F1", f1_score(yTest, np.argmax(prediction.detach().numpy(), axis=1), average=None)) + + +batch_size = int(sys.argv[1]) if len(sys.argv) > 1 else 10 +epochs = int(sys.argv[2]) if len(sys.argv) > 2 else 5 +with mlflow.start_run(): + + prediction, loss, yTest = readAndtrain(epochs, batch_size) + + mlflow.log_metric("accuracy_score", accuracy_score(yTest, np.argmax(prediction.detach().numpy(), axis=1))) + mlflow.log_param("Last loss", loss) + mlflow.log_param("epochs", epochs) + mlflow.log_param("batch size", batch_size) diff --git a/MLproject b/MLproject new file mode 100644 index 0000000..0e66c2a --- /dev/null +++ b/MLproject @@ -0,0 +1,11 @@ +name: ium_s434766_mlflow_lab8 + +docker_env: my_env.yaml + image: s434732/ium:latest + +entry_points: + main: + parameters: + epochs = 5 + batch_size = 10 + command: "python mlflow.py -r {train_size_param} {test_size_param} {epochs} {batch_size}" \ No newline at end of file diff --git a/__pycache__/IUM_08_mlflow.cpython-36.pyc b/__pycache__/IUM_08_mlflow.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4eda6eedd636bb55ed162bdf714e361b995b8472 GIT binary patch literal 2552 zcmah~OK%*<5$>MX&dk2aB_%pRLIj3^O$=^g8Hf%>64|6>0g?bqbnFZgjGNQV-5Jhf z-Q6p37qb_y0H1{9FC;ggbImn>AjiJu#A6P*<&dgbie?T*cF|qcRoz{WuKMO)w;O(a z@aHezZ4>fOa_iRz{x>kxTa*w^IL%44r8J_jwsKNfkwv#MJ93&mZsawz6}5ocxnBfP zK#6d&kUQLcO(}UuxW`+s2yY21Yj5nR!+jpSB2icL1kL&z%EJpsek}&L;^6_=(9+`V zXn3RVfc`Jn`p&h!8|{czw!0yJJOg`Ou_J~X>-TmviuR&?(A?oY5r}>8Jejp2lxRD1YFb0QHHlqV zc^d*8ycJwepN@Uwg0DhK7mgY~j?*&LaV#xddXLjmFloGeOIy#=X;G!zwA7Xv+s08M zKQ}>I&KEl7X<@=@p|5$!L=eYG&XkJdZ^^&U;L=G^>Pa&BObMwbMVidmUI70b=N-u_1gpKt(eRQQSJO1jm zk=#VT4=PkQ(dPgJ8e-;6^bH5mukG5xO64xLHMTM|RhxHsciKK6nS~_>yByfnyR~-f zroR8rDGzqtKGo80=G9Ktf*bvYce7UJ^FALy6Vr9j$h<}$Hhin$+kCk0@SSy+57s>$ z)ZMzbvDW>%pW&`H-`zk=&>n8q$+~>R_g>TW09L&W_Y9y(I(+}NwH{s`>mjrgJtlv+ zzup1e4z^W2MBoA1uYK@hw;t5HbpRGd^(Y%%@7P27C;CO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