ium_444507/lab07_sacred.py

143 lines
4.7 KiB
Python
Raw Normal View History

2022-05-06 00:52:42 +02:00
#!/usr/bin/python
import numpy as np
import torch
from torch import nn
from torch.autograd import Variable
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
import torch.nn.functional as F
import pandas as pd
from sklearn import preprocessing
import sys
2022-05-09 10:26:34 +02:00
from sacred.observers import MongoObserver, FileStorageObserver
2022-05-06 00:52:42 +02:00
from sacred import Experiment
import random
import time
ex = Experiment(save_git_info=False)
2022-05-08 23:35:15 +02:00
ex.observers.append(MongoObserver(url='mongodb://admin:IUM_2021@172.17.0.1:27017',
2022-05-06 00:52:42 +02:00
db_name='sacred'))
2022-05-09 10:26:34 +02:00
ex.observers.append(FileStorageObserver('s444507_sacred_FileObserver'))
2022-05-06 00:52:42 +02:00
@ex.config
def my_config():
epochs = "50"
class Model(nn.Module):
def __init__(self, input_dim):
super(Model, self).__init__()
self.layer1 = nn.Linear(input_dim, 100)
self.layer2 = nn.Linear(100, 60)
self.layer3 = nn.Linear(60, 5)
def forward(self, x):
x = F.relu(self.layer1(x))
x = F.relu(self.layer2(x))
x = F.softmax(self.layer3(x)) # To check with the loss function
return x
def load_dataset_raw():
""" Load data from .csv file. """
2022-05-08 23:49:16 +02:00
cars = pd.read_csv('./Car_Prices_Poland_Kaggle.csv', usecols=[1, 4, 5, 6, 10], sep=',')
2022-05-06 00:52:42 +02:00
return cars
def load_dataset_files():
""" Load shuffled, splitted dev and train files from .csv files. """
2022-05-08 23:49:16 +02:00
cars_dev = pd.read_csv('./Car_Prices_Poland_Kaggle_dev.csv', usecols=[1, 4, 5, 6, 10], sep=',', names= [str(i) for i in range(5)])
cars_train = pd.read_csv('./Car_Prices_Poland_Kaggle_train.csv', usecols=[1, 4, 5, 6, 10], sep=',', names= [str(i) for i in range(5)])
2022-05-06 00:52:42 +02:00
return cars_dev, cars_train
def remove_rows(data_dev, data_train):
dev_removed_rows = data_dev.loc[(data_dev['0'] == 'audi') | (data_dev['0'] == 'bmw') | (data_dev['0'] == 'ford') | (data_dev['0'] == 'opel') | (data_dev['0'] == 'volkswagen')]
train_removed_rows = data_train.loc[(data_train['0'] == 'audi') | (data_train['0'] == 'bmw') | (data_train['0'] == 'ford') | (data_train['0'] == 'opel') | (data_train['0'] == 'volkswagen')]
return dev_removed_rows, train_removed_rows
def prepare_labels_features(dataset):
""" Label make column"""
le = preprocessing.LabelEncoder()
mark_column = np.array(dataset[:]['0'])
le.fit(mark_column)
print(list(le.classes_))
lab = le.transform(mark_column)
feat = dataset.drop(['0'], axis=1).to_numpy()
mm_scaler = preprocessing.MinMaxScaler()
feat = mm_scaler.fit_transform(feat)
return lab, feat
2022-05-09 00:23:12 +02:00
@ex.automain
2022-05-06 00:52:42 +02:00
def my_main(epochs, _run):
# Prepare dataset
print("Loading dataset...")
dev, train = load_dataset_files()
print("Dataset loaded")
print("Preparing dataset...")
dev, train = remove_rows(dev, train)
labels_train, features_train = prepare_labels_features(train)
labels_test, features_test = prepare_labels_features(dev)
print("Dataset prepared")
# Training
model = Model(features_train.shape[1])
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = nn.CrossEntropyLoss()
# number of epochs is parametrized
try:
2022-05-09 00:23:12 +02:00
epochs_n = int(epochs)
2022-05-06 00:52:42 +02:00
except Exception as e:
print(e)
print("Setting default epochs value to 1000.")
2022-05-09 00:23:12 +02:00
epochs_n = 10
2022-05-06 00:52:42 +02:00
2022-05-09 00:23:12 +02:00
print(f"Number of epochs: {epochs_n}")
2022-05-06 00:52:42 +02:00
print("Starting model training...")
x_train, y_train = Variable(torch.from_numpy(features_train)).float(), Variable(torch.from_numpy(labels_train)).long()
2022-05-09 00:23:12 +02:00
for epoch in range(1, epochs_n + 1):
2022-05-06 00:52:42 +02:00
print("Epoch #", epoch)
y_pred = model(x_train)
loss = loss_fn(y_pred, y_train)
print(f"The loss calculated: {loss}")
# Zero gradients
optimizer.zero_grad()
loss.backward() # Gradients
optimizer.step() # Update
print("Model training finished")
x_test = Variable(torch.from_numpy(features_test)).float()
pred = model(x_test)
pred = pred.detach().numpy()
print(f"The accuracy metric is: {accuracy_score(labels_test, np.argmax(pred, axis=1))}")
accuracy = accuracy_score(labels_test, np.argmax(pred, axis=1))
f1 = f1_score(labels_test, np.argmax(pred, axis=1), average='weighted')
_run.log_scalar("measure.accuracy", accuracy)
_run.log_scalar("measure.f1", f1)
print("Saving model to file...")
torch.save(model, "CarPrices_pytorch_model.pkl")
print("Model saved with name: CarPrices_pytorch_model.pkl")
saved_model = torch.load("CarPrices_pytorch_model.pkl")
print(np.argmax(saved_model(x_test[0]).detach().numpy(), axis=0))
pd_predictions = pd.DataFrame(pred)
pd_predictions.to_csv("./prediction_results.csv")
2022-05-09 00:23:12 +02:00
# ex.run()
2022-05-06 00:52:42 +02:00
ex.add_artifact("CarPrices_pytorch_model.pkl")