text_html_output_1.png
import torch

import torch

import torch.nn as nn

import torch
import torch.nn as nn
class HeartFailureSimpleNNModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = nn.Linear(12, 9)
        self.ReLU1 = nn.ReLU()
        self.linear2 = nn.Linear(9, 9)
        self.ReLU2 = nn.ReLU()
        self.linear3 = nn.Linear(9, 7)
        self.ReLU3 = nn.ReLU()
        self.linear4 = nn.Linear(7, 5)
        self.ReLU4 = nn.ReLU()
        self.linear5 = nn.Linear(5, 1)
    def forward(self, x):
        lin1_out = self.linear1(x)
        ReLU_out1 = self.ReLU1(lin1_out)
        ReLU_out2 = self.ReLU2(self.linear2(ReLU_out1))
        ReLU_out3 = self.ReLU3(self.linear3(ReLU_out2))
        ReLU_out4 = self.ReLU4(self.linear4(ReLU_out3))
        return self.linear5(ReLU_out4).sigmoid_()

net = HeartFailureSimpleNNModel()

criterion = nn.BCELoss()
num_epochs = 500
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
input_tensor = torch.from_numpy(X_train_2).type(torch.FloatTensor)
label_tensor = torch.from_numpy(y_train_2)
label_tensor = label_tensor.float()
for epoch in range(num_epochs):
    output = net(input_tensor.float())
    loss = criterion(output, label_tensor.unsqueeze(1))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if epoch % 20 == 0:
        print('Epoch {}/{} => Loss: {:.2f}'.format(epoch + 1, num_epochs, loss.item()))