PyTorch: Logistic regression (multidimensional feature input)
1. Multi-dimensional feature input: (multiple input parameters Xn, influence on Y = 0 or 1)
2. Before we used single feature input, the model function is:
In the face of multi-dimensional feature input, the model function needs to be changed:
In the formula, the multiplication of multidimensional feature x and weight w is equal to a scalar, so:
simplify:
The symbol in the expression actually represents the Logistic function
3. Enter multidimensional features into the function expression:
because
but:
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()