import torch import sys from torch import nn import numpy as np import pandas as pd np.set_printoptions(suppress=False) 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("train.csv") data_test = pd.read_csv("test.csv") data_val = pd.read_csv("valid.csv") x_train = data_train[['age','anaemia','creatinine_phosphokinase','diabetes', 'ejection_fraction', 'high_blood_pressure', 'platelets', 'serum_creatinine', 'serum_sodium', 'sex', 'smoking']].astype(np.float32) y_train = data_train['DEATH_EVENT'].astype(np.float32) x_test = data_test[['age','anaemia','creatinine_phosphokinase','diabetes', 'ejection_fraction', 'high_blood_pressure', 'platelets', 'serum_creatinine', 'serum_sodium', 'sex', 'smoking']].astype(np.float32) y_test = data_test['DEATH_EVENT'].astype(np.float32) fTrain = torch.from_numpy(x_train.values) tTrain = torch.from_numpy(y_train.values.reshape(179,1)) fTest= torch.from_numpy(x_test.values) tTest = torch.from_numpy(y_test.values) batch_size = int(sys.argv[1]) if len(sys.argv) > 1 else 10 num_epochs = int(sys.argv[2]) if len(sys.argv) > 2 else 5 learning_rate = 0.001 input_dim = 11 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(y_pred.data) torch.save(model.state_dict(), 'DEATH_EVENT.pth')