2022-04-29 19:17:50 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
|
|
|
import torch
|
|
|
|
from torch import nn, optim
|
|
|
|
import torch.nn.functional as F
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
epochs = int(sys.argv[1])
|
2022-04-29 19:17:50 +02:00
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
|
|
|
X_train = pd.read_csv('X_train.csv')
|
|
|
|
y_train = pd.read_csv('y_train.csv')
|
2022-04-29 19:17:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
X_train = torch.from_numpy(np.array(X_train)).float()
|
|
|
|
y_train = torch.squeeze(torch.from_numpy(y_train.values).float())
|
2022-04-29 19:17:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
|
|
|
class Net(nn.Module):
|
|
|
|
def __init__(self, n_features):
|
|
|
|
super(Net, self).__init__()
|
|
|
|
self.fc1 = nn.Linear(n_features, 5)
|
|
|
|
self.fc2 = nn.Linear(5, 3)
|
|
|
|
self.fc3 = nn.Linear(3, 1)
|
|
|
|
def forward(self, x):
|
|
|
|
x = F.relu(self.fc1(x))
|
|
|
|
x = F.relu(self.fc2(x))
|
|
|
|
return torch.sigmoid(self.fc3(x))
|
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
net = Net(X_train.shape[1])
|
|
|
|
criterion = nn.BCELoss()
|
|
|
|
optimizer = optim.Adam(net.parameters(), lr=0.001)
|
2022-04-29 19:17:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
2022-04-29 19:17:50 +02:00
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
X_train = X_train.to(device)
|
|
|
|
y_train = y_train.to(device)
|
2022-05-08 15:25:29 +02:00
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
net = net.to(device)
|
|
|
|
criterion = criterion.to(device)
|
2022-05-08 15:25:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
def calculate_accuracy(y_true, y_pred):
|
|
|
|
predicted = y_pred.ge(.5).view(-1)
|
|
|
|
return (y_true == predicted).sum().float() / len(y_true)
|
|
|
|
|
|
|
|
def round_tensor(t, decimal_places=3):
|
|
|
|
return round(t.item(), decimal_places)
|
|
|
|
|
|
|
|
|
|
|
|
for epoch in range(epochs):
|
|
|
|
y_pred = net(X_train)
|
|
|
|
y_pred = torch.squeeze(y_pred)
|
|
|
|
train_loss = criterion(y_pred, y_train)
|
|
|
|
if epoch % 100 == 0:
|
|
|
|
train_acc = calculate_accuracy(y_train, y_pred)
|
|
|
|
print(
|
|
|
|
f'''epoch {epoch}
|
|
|
|
Train set - loss: {round_tensor(train_loss)}, accuracy: {round_tensor(train_acc)}
|
|
|
|
''')
|
|
|
|
optimizer.zero_grad()
|
|
|
|
train_loss.backward()
|
|
|
|
optimizer.step()
|
2022-04-29 19:17:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
2022-05-08 15:33:57 +02:00
|
|
|
torch.save(net, 'model.pth')
|
2022-04-29 19:17:50 +02:00
|
|
|
|