当前位置:网站首页>Station B, Mr. Liu Er - multiple logistic regression, structure 7

Station B, Mr. Liu Er - multiple logistic regression, structure 7

2022-07-06 05:42:00 Ning Ranye


Series articles :


List of articles


import torch
import  matplotlib.pyplot as plt
import numpy as  np
class LogisticRegressionModel(torch.nn.Module):
    def __init__(self):
        super(LogisticRegressionModel, self).__init__()
        #  Input dimensions 8 Output dimension 6
        self.lay1 = torch.nn.Linear(8,6)
        self.lay2 = torch.nn.Linear(6,4)
        self.lay3 = torch.nn.Linear(4,1)
        self.sigmod = torch.nn.Sigmoid()

    def forward(self,x):
        x = self.sigmod(self.lay1(x))
        x = self.sigmod(self.lay2(x))
        x = self.sigmod(self.lay3(x))
        return  x

model = LogisticRegressionModel()
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=0.005)
#  Reading data 
xy = np.loadtxt('./datasets/diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:,:-1])
y_data = torch.from_numpy(xy[:,[-1]])
epoch_list = []
loss_list = []
for epoch in range(1000):
#  Minimum batch processing is not used 
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    loss_list.append(loss.item())
    epoch_list.append(epoch)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()
原网站

版权声明
本文为[Ning Ranye]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060533516600.html