LAB 5 FRAMEWORK PYTORCH

This commit is contained in:
s434766 2021-04-17 13:35:20 +02:00
parent e380a345e3
commit 26b8d85650
5 changed files with 349 additions and 2 deletions

View File

@ -1,10 +1,10 @@
FROM ubuntu:latest FROM ubuntu:latest
RUN apt update && apt install -y python3-pip && pip3 install setuptools && pip3 install numpy && pip3 install pandas && pip3 install wget && pip3 install scikit-learn && rm -rf /var/lib/apt/lists/* RUN apt update && apt install -y python3-pip && pip3 install setuptools && pip3 install numpy && pip3 install pandas && pip3 install wget && pip3 install scikit-learn && pip3 install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html &&rm -rf /var/lib/apt/lists/*
WORKDIR /app WORKDIR /app
COPY ./create.py ./ COPY ./create.py ./
COPY ./stats.py ./ COPY ./stats.py ./
COPY ./stroke-pytorch.py ./

2
Jenkinsfile vendored
View File

@ -14,6 +14,7 @@ pipeline {
stage('Docker'){ stage('Docker'){
steps{ steps{
sh 'python3 ./create.py' sh 'python3 ./create.py'
sh 'python3 "./stroke-pytorch.py" > model.txt'
} }
} }
stage('checkout: Check out from version control') { stage('checkout: Check out from version control') {
@ -23,6 +24,7 @@ pipeline {
} }
stage('archiveArtifacts') { stage('archiveArtifacts') {
steps { steps {
archiveArtifacts 'model.txt'
archiveArtifacts 'data_val.csv' archiveArtifacts 'data_val.csv'
archiveArtifacts 'data_test.csv' archiveArtifacts 'data_test.csv'
archiveArtifacts 'data_train.csv' archiveArtifacts 'data_train.csv'

276
lab5.ipynb Normal file

File diff suppressed because one or more lines are too long

69
stroke-pytorch.py Normal file
View File

@ -0,0 +1,69 @@
import torch
import torch.nn.functional as F
from torch import nn
from torch.autograd import Variable
import torchvision.transforms as transforms
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
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)
data_train = pd.read_csv("data_train.csv")
data_test = pd.read_csv("data_test.csv")
data_val = pd.read_csv("data_val.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)
batch_size = 95
n_iters = 1000
num_epochs = int(n_iters / (len(x_train) / batch_size))
learning_rate = 0.001
input_dim = 6
output_dim = 1
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()
y_pred = model(fTest)
print("predicted Y value: ", y_pred.data)
print ("The accuracy is", accuracy_score(tTest, np.argmax(y_pred.detach().numpy(), axis=1)))
torch.save(model, 'stroke.pkl')

BIN
stroke.pkl Normal file

Binary file not shown.