From c2a7fbbf2541857d281193e4d5306c17962c498c Mon Sep 17 00:00:00 2001 From: Maciej Czajka Date: Mon, 9 May 2022 22:25:23 +0200 Subject: [PATCH] update dllib-mlflow.py --- Biblioteka_DL/dllib-mlflow.py | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Biblioteka_DL/dllib-mlflow.py b/Biblioteka_DL/dllib-mlflow.py index 2be6024..a57aef9 100644 --- a/Biblioteka_DL/dllib-mlflow.py +++ b/Biblioteka_DL/dllib-mlflow.py @@ -296,34 +296,34 @@ def my_main(epochs): print ("The loss calculated: ", loss) - # Not using dataloader - 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")