PyTorch implements logistic regression for multi-dimensional feature input

1. Implementation process

1. Prepare data

The data in this paper adopts the data set given in the literature [1]. The first 8 columns of the data set are features, and the last 1 column is the label (0/1). This model uses pandas to process the data set. It should be noted that the original data set does not have a feature name and needs to be added in the first line. Otherwise, pandas will treat the data in the first line as a feature name, which will affect the final classification. Effect. code show as below:

# 1、准备数据
import torch
import pandas as pd
import numpy as np
xy = pd.read_csv('G:/datasets/diabetes/diabetes.csv',dtype=np.float32)	# 文件路径
x_data = torch.from_numpy(xy.values[:,:-1])
y_data = torch.from_numpy(xy.values[:,[-1]])

2. Design model

This paper adopts the idea of ​​literature [1], the activation function uses ReLU, and the last layer uses the Sigmoid function. The code is as follows:

class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.linear1 = torch.nn.Linear(8,6)
        self.linear2 = torch.nn.Linear(6,4)
        self.linear3 = torch.nn.Linear(4,1)
        self.activate = torch.nn.ReLU()
    
    def forward(self, x):
        x = self.activate(self.linear1(x))
        x = self.activate(self.linear2(x))
        x = torch.sigmoid(self.linear3(x))
        return x
model = Model()

Load the model and data onto the GPU with the following code:

### 将模型和训练数据加载到GPU上
# 模型加载到GPU上
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
# 数据加载到GPU上
x = x_data.to(device)
y = y_data.to(device)

3. Construct the loss function and optimizer

# 3、构造损失函数和优化器
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(),lr=0.1)

4. Training process

epoch_list = []
loss_list = []
epochs = 10000
for epoch in range(epochs):
    # Forward
    y_pred = model(x)
    loss = criterion(y_pred, y)
    print(epoch, loss)

    epoch_list.append(epoch)
    loss_list.append(loss.data.item())

    # Backward
    optimizer.zero_grad()
    loss.backward()

    # Update
    optimizer.step()

5. Result display

Check out the weights and biases of the individual layers:

model.linear1.weight,model.linear1.bias
model.linear2.weight,model.linear2.bias
model.linear3.weight,model.linear3.bias

The change curve of the loss value with the number of iterations:

# 绘图展示
plt.plot(epoch_list,loss_list,'b')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.grid()
plt.show()

insert image description hereFinal loss and accuracy:

# 准确率
y_pred_label = torch.where(y_pred.data.cpu() >= 0.5,torch.tensor([1.0]),torch.tensor([0.0]))
acc = torch.eq(y_pred_label, y_data).sum().item()/y_data.size(0)

print("loss = ",loss.item(), "acc = ",acc)
loss =  0.4232381284236908 acc =  0.7931488801054019

2. References

[1] https://www.bilibili.com/video/BV1Y7411d7Ys?p=7
[2] https://blog.csdn.net/bit452/article/details/109682078

Guess you like

Origin blog.csdn.net/weixin_43821559/article/details/123314829