This commit is contained in:
s434766 2021-05-13 23:06:29 +02:00
parent b3d4c74ecb
commit ee20c96360
5 changed files with 177 additions and 13 deletions

View File

@ -2,7 +2,7 @@ FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3-pip && pip3 install setuptools && pip3 install numpy && pip3 install pandas && pip3 install wget && pip3 install scikit-learn && pip3 install matplotlib && rm -rf /var/lib/apt/lists/*
RUN pip3 install torch torchvision torchaudio
RUN pip3 install sacred
RUN pip3 install sacred && pip3 install GitPython
WORKDIR /app
COPY ./create.py ./

View File

@ -2,6 +2,7 @@ FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3-pip && pip3 install setuptools && pip3 install numpy && pip3 install pandas && pip3 install wget && pip3 install scikit-learn && pip3 install matplotlib && rm -rf /var/lib/apt/lists/*
RUN pip3 install torch torchvision torchaudio
RUN pip3 install sacred && pip3 install GitPython
WORKDIR /app
COPY ./../create.py ./

85
sacred-pytorch1.py Normal file
View File

@ -0,0 +1,85 @@
import torch
import sys
import torch.nn.functional as F
from torch import nn
from sklearn.metrics import accuracy_score, mean_squared_error
import numpy as np
import pandas as pd
from sacred import Experiment
from sacred.observers import FileStorageObserver
np.set_printoptions(suppress=False)
ex = Experiment("ium_s434766", interactive=False, save_git_info=False)
ex.observers.append(FileStorageObserver("ium_s434766/my_runs"))
@ex.config
def my_config():
num_epochs = 15
batch_size = 16
learning_rate = 0.001
class LogisticRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LogisticRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
out = self.linear(x)
return self.sigmoid(out)
@ex.capture
def train(num_epochs, batch_size, learning_rate, _run):
data_train = pd.read_csv("data_train.csv")
data_test = pd.read_csv("data_test.csv")
FEATURES = ['age','hypertension','heart_disease','ever_married', 'avg_glucose_level', 'bmi']
x_train = data_train[FEATURES].astype(np.float32)
y_train = data_train['stroke'].astype(np.float32)
x_test = data_test[FEATURES].astype(np.float32)
y_test = data_test['stroke'].astype(np.float32)
fTrain = torch.from_numpy(x_train.values)
tTrain = torch.from_numpy(y_train.values.reshape(2945,1))
fTest= torch.from_numpy(x_test.values)
tTest = torch.from_numpy(y_test.values)
input_dim = 6
output_dim = 1
info_params = "Batch size = " + str(batch_size) + " Epochs = " + str(num_epochs)
_run.info(info_params)
model = LogisticRegressionModel(input_dim, output_dim)
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
for epoch in range(num_epochs):
# print ("Epoch #",epoch)
model.train()
optimizer.zero_grad()
# Forward pass
y_pred = model(fTrain)
# Compute Loss
loss = criterion(y_pred, tTrain)
# print(loss.item())
# Backward pass
loss.backward()
optimizer.step()
info_loss = "Last loss = " + str(loss.item())
_run.info(info_loss)
y_pred = model(fTest)
print("predicted Y value: ", y_pred.data)
torch.save(model.state_dict(), 'stroke.pth')
@ex.automain
def my_main(num_epochs, batch_size, learning_rate, _run):
train()
r = ex.run()
ex.add_artifact("stroke_model/stroke.pth")

86
sacred-pytorch2.py Normal file
View File

@ -0,0 +1,86 @@
import torch
import sys
import torch.nn.functional as F
from torch import nn
from sklearn.metrics import accuracy_score, mean_squared_error
import numpy as np
import pandas as pd
from sacred import Experiment
from sacred.observers import MongoObserver
np.set_printoptions(suppress=False)
ex = Experiment("ium_s434766", interactive=False, save_git_info=False)
ex.observers.append(MongoObserver(url='mongodb://mongo_user:mongo_password_IUM_2021@172.17.0.1:27017',
db_name='sacred'))
@ex.config
def my_config():
num_epochs = 15
batch_size = 16
learning_rate = 0.001
class LogisticRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LogisticRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
out = self.linear(x)
return self.sigmoid(out)
@ex.capture
def train(num_epochs, batch_size, learning_rate, _run):
data_train = pd.read_csv("data_train.csv")
data_test = pd.read_csv("data_test.csv")
FEATURES = ['age','hypertension','heart_disease','ever_married', 'avg_glucose_level', 'bmi']
x_train = data_train[FEATURES].astype(np.float32)
y_train = data_train['stroke'].astype(np.float32)
x_test = data_test[FEATURES].astype(np.float32)
y_test = data_test['stroke'].astype(np.float32)
fTrain = torch.from_numpy(x_train.values)
tTrain = torch.from_numpy(y_train.values.reshape(2945,1))
fTest= torch.from_numpy(x_test.values)
tTest = torch.from_numpy(y_test.values)
input_dim = 6
output_dim = 1
info_params = "Batch size = " + str(batch_size) + " Epochs = " + str(num_epochs)
_run.info(info_params)
model = LogisticRegressionModel(input_dim, output_dim)
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
for epoch in range(num_epochs):
# print ("Epoch #",epoch)
model.train()
optimizer.zero_grad()
# Forward pass
y_pred = model(fTrain)
# Compute Loss
loss = criterion(y_pred, tTrain)
# print(loss.item())
# Backward pass
loss.backward()
optimizer.step()
info_loss = "Last loss = " + str(loss.item())
_run.info(info_loss)
y_pred = model(fTest)
print("predicted Y value: ", y_pred.data)
torch.save(model.state_dict(), 'stroke.pth')
@ex.automain
def my_main(num_epochs, batch_size, learning_rate, _run):
train()
r = ex.run()
ex.add_artifact("stroke_model/stroke.pth")

View File

@ -13,10 +13,7 @@ from sacred import Experiment
from sacred.observers import FileStorageObserver
np.set_printoptions(suppress=False)
# ex = Experiment("stroke-pytorch", interactive=True)
# ex.observers.append(FileStorageObserver('ium_s434766O_files'))
# ex.observers.append(MongoObserver(url='mongodb://mongo_user:mongo_password_IUM_2021@localhost:27017',
# db_name='sacred'))
class LogisticRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LogisticRegressionModel, self).__init__()
@ -26,8 +23,7 @@ class LogisticRegressionModel(nn.Module):
out = self.linear(x)
return self.sigmoid(out)
# @ex.main
# def my_main(_log):
data_train = pd.read_csv("data_train.csv")
data_test = pd.read_csv("data_test.csv")
data_val = pd.read_csv("data_val.csv")
@ -50,8 +46,7 @@ num_epochs = int(sys.argv[2]) if len(sys.argv) > 2 else 5
learning_rate = 0.001
input_dim = 6
output_dim = 1
info_params = "Batch size = " + str(batch_size) + " Epochs = " + str(num_epochs)
# _log.info(info_params)
model = LogisticRegressionModel(input_dim, output_dim)
criterion = torch.nn.BCELoss(reduction='mean')
@ -69,11 +64,8 @@ for epoch in range(num_epochs):
# Backward pass
loss.backward()
optimizer.step()
info_loss = "Last loss = " + str(loss.item())
# _log.info(info_loss)
y_pred = model(fTest)
print("predicted Y value: ", y_pred.data)
torch.save(model.state_dict(), 'stroke.pth')
# ex.run()