2022-05-15 12:56:38 +02:00
|
|
|
|
2022-05-15 12:29:42 +02:00
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
|
|
|
import torch.nn.functional as F
|
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
from sklearn import preprocessing
|
2022-05-15 22:33:12 +02:00
|
|
|
from fastai.basics import *
|
2022-05-15 12:29:42 +02:00
|
|
|
from sklearn.preprocessing import StandardScaler
|
|
|
|
scaler = StandardScaler()
|
|
|
|
from sacred import Experiment
|
|
|
|
from sacred.observers import FileStorageObserver
|
|
|
|
from sacred.observers import MongoObserver
|
|
|
|
from sklearn.metrics import accuracy_score
|
|
|
|
from sacred.observers.file_storage import file_storage_option
|
|
|
|
from sacred.observers.mongo import mongo_db_option
|
|
|
|
from sklearn.metrics import mean_squared_error
|
|
|
|
from sklearn.metrics import mean_absolute_error
|
|
|
|
import warnings
|
|
|
|
from sklearn.metrics import r2_score
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
from sklearn.linear_model import ElasticNet
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
import mlflow
|
|
|
|
import mlflow.sklearn
|
|
|
|
from urllib.parse import urlparse
|
2022-05-15 15:20:53 +02:00
|
|
|
from mlflow.models import infer_signature
|
2022-05-15 12:29:42 +02:00
|
|
|
|
2022-05-15 21:58:02 +02:00
|
|
|
mlflow.set_experiment("478815")
|
2022-05-17 21:28:09 +02:00
|
|
|
mlflow.set_tracking_uri("http://172.17.0.1:5000")
|
2022-05-15 12:29:42 +02:00
|
|
|
|
|
|
|
# Model
|
|
|
|
class Model(nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.linear = nn.Linear(1,1)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
y_predicted = torch.sigmoid(self.linear(x))
|
|
|
|
return y_predicted
|
|
|
|
|
|
|
|
data = pd.read_csv('data.csv')
|
|
|
|
data.dropna()
|
|
|
|
|
|
|
|
training_data = data.sample(frac=0.9, random_state=25)
|
|
|
|
testing_data = data.drop(training_data.index)
|
|
|
|
|
|
|
|
print(f"No. of training examples: {training_data.shape[0]}")
|
|
|
|
print(f"No. of testing examples: {testing_data.shape[0]}")
|
|
|
|
|
|
|
|
training_data = training_data[['sqft_living', 'price']]
|
|
|
|
|
|
|
|
testing_data = testing_data[['sqft_living', 'price']]
|
|
|
|
|
|
|
|
training_data[['price']] = training_data[['price']] / 10000000
|
|
|
|
training_data[['sqft_living']] = training_data[['sqft_living']] / 10000
|
|
|
|
|
|
|
|
testing_data[['price']] = testing_data[['price']] / 10000000
|
|
|
|
testing_data[['sqft_living']] = testing_data[['sqft_living']] / 10000
|
|
|
|
|
|
|
|
# Tensory
|
|
|
|
X_training = training_data[['sqft_living']].to_numpy()
|
|
|
|
X_testing = testing_data[['sqft_living']].to_numpy()
|
|
|
|
y_training = training_data[['price']].to_numpy()
|
|
|
|
y_testing = testing_data[['price']].to_numpy()
|
|
|
|
|
|
|
|
torch.from_file
|
|
|
|
X_training = torch.from_numpy(X_training.astype(np.float32))
|
|
|
|
X_testing = torch.from_numpy(X_testing.astype(np.float32))
|
|
|
|
y_training = torch.from_numpy(y_training.astype(np.float32))
|
|
|
|
y_testing = torch.from_numpy(y_testing.astype(np.float32))
|
|
|
|
|
|
|
|
model = Model()
|
|
|
|
criterion = nn.BCELoss()
|
|
|
|
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
|
|
|
|
|
2022-05-15 13:09:28 +02:00
|
|
|
epochs=1000
|
2022-05-15 12:29:42 +02:00
|
|
|
|
|
|
|
def my_main(epochs):
|
|
|
|
# Trening
|
|
|
|
|
|
|
|
mlflow.log_param("epochs", epochs)
|
|
|
|
|
|
|
|
for epochs in range(epochs):
|
2022-05-15 14:31:36 +02:00
|
|
|
y_predicted_train = model(X_training)
|
|
|
|
loss = criterion(y_predicted_train,y_training)
|
2022-05-15 12:29:42 +02:00
|
|
|
loss.backward()
|
|
|
|
optimizer.step()
|
|
|
|
optimizer.zero_grad()
|
|
|
|
with open ("output.txt",'a+') as f:
|
|
|
|
if (epochs%100==0):
|
|
|
|
f.write(f'epoch:{epochs+1},loss = {loss.item():.4f}')
|
2022-05-15 15:36:17 +02:00
|
|
|
x = X_training.detach().numpy()
|
|
|
|
y = y_predicted_train.detach().numpy()
|
2022-05-15 15:20:53 +02:00
|
|
|
|
2022-05-15 12:29:42 +02:00
|
|
|
with torch.no_grad():
|
|
|
|
y_predicted = model(X_testing)
|
|
|
|
y_predicted_cls = y_predicted.round()
|
|
|
|
acc = y_predicted_cls.eq(y_testing).sum()/float(y_testing.shape[0])
|
|
|
|
print(f'{acc:.4f}')
|
|
|
|
#result = open("output",'w+')
|
|
|
|
#result.write(f'{y_predicted}')
|
|
|
|
|
2022-05-15 22:50:41 +02:00
|
|
|
print(np.array(X_testing[0]))
|
2022-05-15 22:33:12 +02:00
|
|
|
|
2022-05-17 21:28:09 +02:00
|
|
|
input_example = np.array(X_testing[0])
|
|
|
|
siganture = mlflow.models.signature.infer_signature(np.array(X_training), np.array(y_predicted_train))
|
|
|
|
tracking_url_type_store = urlparse(mlflow.get_tracking_uri()).scheme
|
2022-05-15 22:33:12 +02:00
|
|
|
|
2022-05-17 21:28:09 +02:00
|
|
|
if tracking_url_type_store != "file":
|
|
|
|
mlflow.pytorch.log_model(model, "model_new", registered_model_name="478815", signature=siganture,
|
|
|
|
input_example=input_example)
|
|
|
|
else:
|
|
|
|
mlflow.pytorch.log_model(model, "model_new", signature=siganture, input_example=input_example)
|
|
|
|
mlflow.pytorch.save_model(model, "model_new", signature=siganture, input_example=input_example)
|
2022-05-15 22:33:12 +02:00
|
|
|
|
2022-05-15 22:09:51 +02:00
|
|
|
rmse = mean_squared_error(y_testing, y_predicted)
|
|
|
|
#print(rmse)
|
|
|
|
|
|
|
|
mae = mean_absolute_error(y_testing, y_predicted)
|
|
|
|
#print(mae)
|
|
|
|
|
|
|
|
mlflow.log_metric("rmse", rmse)
|
|
|
|
mlflow.log_metric("mae", mae)
|
|
|
|
|
2022-05-15 12:29:42 +02:00
|
|
|
#with open('metrics.txt', 'a+') as f:
|
|
|
|
#f.write('Root mean squared error:' + str(rmse) + '\n')
|
|
|
|
#f.write('Mean absolute error:' + str(mae) + '\n')
|
|
|
|
#count = [float(line) for line in f if line]
|
|
|
|
#builds = list(range(1, len(count)))
|
|
|
|
|
|
|
|
#with open('metric.txt', 'a+') as f:
|
|
|
|
# f.write(str(rmse) + '\n')
|
|
|
|
|
|
|
|
#with open('metric.txt') as file:
|
|
|
|
# y_rmse = [float(line) for line in file if line]
|
|
|
|
# x_builds = list(range(1, len(y_rmse) + 1))
|
|
|
|
|
|
|
|
|
|
|
|
with mlflow.start_run() as run:
|
|
|
|
my_main(epochs)
|