(6) PyTorch deep learning: Logistic regression (multi-layer multi-dimensional feature input)

PyTorch: Logistic regression (multidimensional feature input)

1. Multi-dimensional feature input: (multiple input parameters Xn, influence on Y = 0 or 1)

insert image description here

2. Before we used single feature input, the model function is:

insert image description here

In the face of multi-dimensional feature input, the model function needs to be changed:

insert image description here

In the formula, the multiplication of multidimensional feature x and weight w is equal to a scalar, so:

insert image description here

simplify:

insert image description here

The symbol in the expression actually represents the Logistic functioninsert image description here

insert image description here

3. Enter multidimensional features into the function expression:

because

insert image description here

but:

insert image description here

4. Program expression:

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = torch.nn.Linear(8, 1)
        self.sigmoid = torch.nn.Sigmoid()
        
    def forward(self, x):
        x = self.sigmoid(self.linear(x))
        return x
    
model = Model()

5. Multi-layer multi-parameter code:

import torch
import numpy as np

xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)  # np里面的数据集,可打印查看
x_data = torch.from_numpy(xy[ : , : -1])   # 所有行,除最后一列的所有列
y_data = torch.from_numpy(xy[ : , [-1]])   # 最后一列的所有行

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.sigmoid = torch.nn.Sigmoid()   # Sigmoid 激活函数

    def forward(self, x):
        x = self.sigmoid(self.linear1(x))
        x = self.sigmoid(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x

# 想使用模型,就实例化即可,可以直接调用
model = Model()

# 构建损失函数、优化器
criterion = torch.nn.BCELoss(size_average=False)          # BCE损失
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)   # 参数优化

for epoch in range(100):
    # forward
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())

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

    # Update
    optimizer.step()

Guess you like

Origin blog.csdn.net/K_AAbb/article/details/125807830