import numpy as np from matplotlib import pyplot as plt import torch data_xy = np.loadtxt('/home/chasing/Documents/pytorchbooklit/diabetes.csv.gz', delimiter=',', dtype=np.float32) x_data = torch.from_numpy(data_xy[:,:-1]) y_data = torch.from_numpy(data_xy[:,-1]).reshape(-1,1) class LinearExample(torch.nn.Module): def __init__(self): super(LinearExample, self).__init__() self.linear1 = torch.nn.Linear(8,6) self.linear2 = torch.nn.Linear(6,4) self.linear3 = torch.nn.Linear(4,1) # self.linear4 = torch.nn.Linear(2,1) self.sigmoid = torch.nn.Sigmoid() self.relu = torch.nn.ReLU() def forward(self,x): x = self.relu(self.linear1(x)) x = self.relu(self.linear2(x)) x = self.linear3(x) # x = self.linear4(x) return self.relu(x) # return self.sigmoid(x) model = LinearExample() criterion = torch.nn.BCELoss(reduction='mean') optimizer = torch.optim.SGD(model.parameters(),lr=1e-2) loss_list = list() if __name__ == '__main__': for epoch in range(300): y_pred = model(x_data) loss = criterion(y_pred, y_data) loss_list.append(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() plt.plot(loss_list) plt.show()