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
import pandas as pd
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
data = pd.read_csv("Sales.csv")
print(os.getcwd())
data = pd.read_csv("./Sales.csv")
# Przygotowanie danych
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)
model=ANN_Model()
model.parameters
def calculate_accuracy(model, X, y):
with torch.no_grad():
@ -65,17 +74,34 @@ epochs = 100
final_losses = []
accuracy_list = []
for i in range(epochs):
i = i + 1
y_pred = model(X_train)
loss = loss_function(y_pred, y_train)
final_losses.append(loss)
train_accuracy = calculate_accuracy(model, X_train, y_train)
test_accuracy = calculate_accuracy(model, X_test, y_test)
print(f"Epoch: {i}, Loss: {loss.item()}, Train Accuracy: {train_accuracy}%, Test Accuracy: {test_accuracy}%")
optimizer.zero_grad()
loss.backward()
optimizer.step()
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):
i = i + 1
y_pred = model(X_train)
loss = loss_function(y_pred, y_train)
final_losses.append(loss)
train_accuracy = calculate_accuracy(model, X_train, y_train)
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}%")
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.save(model,"classificationn_model.pt")