This commit is contained in:
Witold Woch 2023-05-14 14:42:58 +02:00
parent f48c13ca46
commit 4196b9ec68

View File

@ -7,9 +7,19 @@ from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import StandardScaler
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import os
import mlflow
import mlflow.sklearn
import logging
logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__name__)
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("s487194")
# Wczytanie danych # Wczytanie danych
data = pd.read_csv("Sales.csv") print(os.getcwd())
data = pd.read_csv("./Sales.csv")
# Przygotowanie danych # Przygotowanie danych
data["Profit_Category"] = pd.cut(data["Profit"], bins=[-np.inf, 500, 1000, np.inf], labels=[0, 1, 2]) data["Profit_Category"] = pd.cut(data["Profit"], bins=[-np.inf, 500, 1000, np.inf], labels=[0, 1, 2])
@ -47,7 +57,6 @@ class ANN_Model(nn.Module):
torch.manual_seed(20) torch.manual_seed(20)
model=ANN_Model() model=ANN_Model()
model.parameters
def calculate_accuracy(model, X, y): def calculate_accuracy(model, X, y):
with torch.no_grad(): with torch.no_grad():
@ -65,6 +74,14 @@ epochs = 100
final_losses = [] final_losses = []
accuracy_list = [] accuracy_list = []
with mlflow.start_run() as run:
# Logowanie parametrów modelu
mlflow.log_param("hidden_layer_1", 20)
mlflow.log_param("hidden_layer_2", 20)
mlflow.log_param("output_layer", 3)
mlflow.log_param("learning_rate", 0.01)
mlflow.log_param("epochs", epochs)
for i in range(epochs): for i in range(epochs):
i = i + 1 i = i + 1
y_pred = model(X_train) y_pred = model(X_train)
@ -73,9 +90,18 @@ for i in range(epochs):
train_accuracy = calculate_accuracy(model, X_train, y_train) train_accuracy = calculate_accuracy(model, X_train, y_train)
test_accuracy = calculate_accuracy(model, X_test, y_test) test_accuracy = calculate_accuracy(model, X_test, y_test)
# Logowanie metryk po każdej epoce
mlflow.log_metric("train_loss", loss.item(), step=i)
mlflow.log_metric("train_accuracy", train_accuracy, step=i)
mlflow.log_metric("test_accuracy", test_accuracy, step=i)
print(f"Epoch: {i}, Loss: {loss.item()}, Train Accuracy: {train_accuracy}%, Test Accuracy: {test_accuracy}%") print(f"Epoch: {i}, Loss: {loss.item()}, Train Accuracy: {train_accuracy}%, Test Accuracy: {test_accuracy}%")
optimizer.zero_grad() optimizer.zero_grad()
loss.backward() loss.backward()
optimizer.step() optimizer.step()
torch.save(model,"classificationn_model.pt") torch.save(model,"classificationn_model.pt")