diff --git a/Dockerfile b/Dockerfile index cc14c58..9d1f34b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ COPY ./skrypt_download.py ./ COPY ./skrypt_stat.py ./ COPY ./IUM_05.py ./ COPY ./training.py ./ +COPY ./mongoObserver.py ./ RUN mkdir /.kaggle RUN chmod -R 777 /.kaggle diff --git a/Jenkinsfile_evaluation b/Jenkinsfile_evaluation index 1385ed8..0359a2a 100644 --- a/Jenkinsfile_evaluation +++ b/Jenkinsfile_evaluation @@ -24,6 +24,7 @@ pipeline { steps{ copyArtifacts fingerprintArtifacts: true, projectName: 's434732-training/master', selector: buildParameter('WHICH_BUILD_TRAIN') sh 'python3 "./evaluation.py" >> result.txt' + sh 'python3 "./mongoObserver.py"' } } stage('archiveArtifacts') { diff --git a/mongoObserver.py b/mongoObserver.py new file mode 100644 index 0000000..4a079d5 --- /dev/null +++ b/mongoObserver.py @@ -0,0 +1,85 @@ +import torch +from torch import nn +import numpy as np +import pandas as pd +from sklearn.metrics import accuracy_score +from sklearn.metrics import f1_score +from sacred import Experiment +from sacred.observers import MongoObserver + +np.set_printoptions(suppress=False) + +ex = Experiment("434732-mongo", interactive=False, save_git_info=False) +ex.observers.append(MongoObserver(url='mongodb://mongo_user:mongo_password_IUM_2021@172.17.0.1:27017', db_name='sacred')) + +@ex.config +def my_config(): + epochs = 5 + batch_size = 10 + +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) + + +@ex.capture +def readAndtrain(epchos, batch_size, _run): + 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 + + _run.info("Batch: " + str(batch_size) + " epoch: " + epchos) + + 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(epchos): + # 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() + + _run.info("Lost", str(loss.item())) + + + torch.save(model.state_dict(), 'DEATH_EVENT.pth') + prediction= model(xTest) + _run.info("accuracy_score", accuracy_score(yTest, np.argmax(prediction.detach().numpy(), axis=1))) + _run.info("F1", f1_score(yTest, np.argmax(prediction.detach().numpy(), axis=1), average=None)) + + 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)) + +@ex.automain +def my_main(epochs, batch_size): + readAndtrain() + +ex.run() +ex.add_artifact('DEATH_EVENT.pth') \ No newline at end of file diff --git a/training.py b/training.py index 6d168e6..c131e29 100644 --- a/training.py +++ b/training.py @@ -55,7 +55,7 @@ for epoch in range(num_epochs): # Backward pass loss.backward() optimizer.step() -y_pred = model(xTest) -print(y_pred.data) +predictions = model(xTest) +print(predictions.data) torch.save(model.state_dict(), 'DEATH_EVENT.pth')