change number of epochs

This commit is contained in:
Sebastian Wałęsa 2022-05-06 19:12:12 +02:00
parent 2aad3fa066
commit a955ecc7da

View File

@ -1,159 +1,159 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
# In[1]: # In[1]:
import torch import torch
import jovian import jovian
import torchvision import torchvision
import matplotlib import matplotlib
import torch.nn as nn import torch.nn as nn
import pandas as pd import pandas as pd
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import seaborn as sns import seaborn as sns
import torch.nn.functional as F import torch.nn.functional as F
from torchvision.datasets.utils import download_url from torchvision.datasets.utils import download_url
from torch.utils.data import DataLoader, TensorDataset, random_split from torch.utils.data import DataLoader, TensorDataset, random_split
import random import random
import os import os
import sys import sys
# In[2]: # In[2]:
#load data #load data
dataframe = pd.read_csv("understat.csv") dataframe = pd.read_csv("understat.csv")
#choose columns #choose columns
input_cols=list(dataframe.columns)[4:11] input_cols=list(dataframe.columns)[4:11]
output_cols = ['position'] output_cols = ['position']
input_cols, output_cols input_cols, output_cols
# In[4]: # In[4]:
def dataframe_to_arrays(dataframe): def dataframe_to_arrays(dataframe):
dataframe_loc = dataframe.copy(deep=True) dataframe_loc = dataframe.copy(deep=True)
inputs_array = dataframe_loc[input_cols].to_numpy() inputs_array = dataframe_loc[input_cols].to_numpy()
targets_array = dataframe_loc[output_cols].to_numpy() targets_array = dataframe_loc[output_cols].to_numpy()
return inputs_array, targets_array return inputs_array, targets_array
inputs_array, targets_array = dataframe_to_arrays(dataframe) inputs_array, targets_array = dataframe_to_arrays(dataframe)
inputs = torch.from_numpy(inputs_array).type(torch.float) inputs = torch.from_numpy(inputs_array).type(torch.float)
targets = torch.from_numpy(targets_array).type(torch.float) targets = torch.from_numpy(targets_array).type(torch.float)
dataset = TensorDataset(inputs, targets) dataset = TensorDataset(inputs, targets)
# In[7]: # In[7]:
train_ds, val_ds = random_split(dataset, [548, 136]) train_ds, val_ds = random_split(dataset, [548, 136])
batch_size=50 batch_size=50
train_loader = DataLoader(train_ds, batch_size, shuffle=True) train_loader = DataLoader(train_ds, batch_size, shuffle=True)
val_loader = DataLoader(val_ds, batch_size) val_loader = DataLoader(val_ds, batch_size)
# In[8]: # In[8]:
class Model_xPosition(nn.Module): class Model_xPosition(nn.Module):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.linear = nn.Linear(input_size,output_size) self.linear = nn.Linear(input_size,output_size)
def forward(self, xb): def forward(self, xb):
out = self.linear(xb) out = self.linear(xb)
return out return out
def training_step(self, batch): def training_step(self, batch):
inputs, targets = batch inputs, targets = batch
# Generate predictions # Generate predictions
out = self(inputs) out = self(inputs)
# Calcuate loss # Calcuate loss
loss = F.l1_loss(out,targets) loss = F.l1_loss(out,targets)
return loss return loss
def validation_step(self, batch): def validation_step(self, batch):
inputs, targets = batch inputs, targets = batch
out = self(inputs) out = self(inputs)
loss = F.l1_loss(out,targets) loss = F.l1_loss(out,targets)
return {'val_loss': loss.detach()} return {'val_loss': loss.detach()}
def validation_epoch_end(self, outputs): def validation_epoch_end(self, outputs):
batch_losses = [x['val_loss'] for x in outputs] batch_losses = [x['val_loss'] for x in outputs]
epoch_loss = torch.stack(batch_losses).mean() epoch_loss = torch.stack(batch_losses).mean()
return {'val_loss': epoch_loss.item()} return {'val_loss': epoch_loss.item()}
def epoch_end(self, epoch, result, num_epochs): def epoch_end(self, epoch, result, num_epochs):
if (epoch+1) % 100 == 0 or epoch == num_epochs-1: if (epoch+1) % 100 == 0 or epoch == num_epochs-1:
print("Epoch {} loss: {:.4f}".format(epoch+1, result['val_loss'])) print("Epoch {} loss: {:.4f}".format(epoch+1, result['val_loss']))
def evaluate(model, val_loader): def evaluate(model, val_loader):
outputs = [model.validation_step(batch) for batch in val_loader] outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs) return model.validation_epoch_end(outputs)
def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD): def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):
history = [] history = []
optimizer = opt_func(model.parameters(), lr) optimizer = opt_func(model.parameters(), lr)
for epoch in range(epochs): for epoch in range(epochs):
for batch in train_loader: for batch in train_loader:
loss = model.training_step(batch) loss = model.training_step(batch)
loss.backward() loss.backward()
optimizer.step() optimizer.step()
optimizer.zero_grad() optimizer.zero_grad()
result = evaluate(model, val_loader) result = evaluate(model, val_loader)
model.epoch_end(epoch, result, epochs) model.epoch_end(epoch, result, epochs)
history.append(result) history.append(result)
return history return history
# In[9]: # In[9]:
input_size = len(input_cols) input_size = len(input_cols)
output_size = len(output_cols) output_size = len(output_cols)
model=Model_xPosition() model=Model_xPosition()
# In[11]: # In[11]:
epochs = 2000 epochs = 1000
lr = 1e-5 lr = 1e-5
learning_proccess = fit(epochs, lr, model, train_loader, val_loader) learning_proccess = fit(epochs, lr, model, train_loader, val_loader)
# In[13]: # In[13]:
def predict_single(input, target, model): def predict_single(input, target, model):
inputs = input.unsqueeze(0) inputs = input.unsqueeze(0)
predictions = model(inputs) predictions = model(inputs)
prediction = predictions[0].detach() prediction = predictions[0].detach()
return "Target: "+str(target)+" Predicted: "+str(prediction)+"\n" return "Target: "+str(target)+" Predicted: "+str(prediction)+"\n"
# In[14]: # In[14]:
for i in random.sample(range(0, len(val_ds)), 10): for i in random.sample(range(0, len(val_ds)), 10):
input_, target = val_ds[i] input_, target = val_ds[i]
print(predict_single(input_, target, model),end="") print(predict_single(input_, target, model),end="")
# In[15]: # In[15]:
with open("result.txt", "w+") as file: with open("result.txt", "w+") as file:
for i in range(0, len(val_ds), 1): for i in range(0, len(val_ds), 1):
input_, target = val_ds[i] input_, target = val_ds[i]
file.write(str(predict_single(input_, target, model))) file.write(str(predict_single(input_, target, model)))