import torch
from sklearn import datasets
import torch.nn as nn
from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.model_selection import train_test_split
np.random.seed(0)
torch.manual_seed(0)
# 首先读取数据
data = datasets.load_breast_cancer()#预测乳腺癌的数据从sklearn中得到
# print(data)
X, y = data.data.astype(np.float32), data.target.astype(np.float32)
X_train_np, X_test_np, y_train_np, y_test_np = train_test_split(X, y, test_size=0.3)
#划分测试集和训练集test_size=0.3表示测试集占百分之30
sc = StandardScaler()
#标准化数据通过减去均值然后除以方差(或标准差),
# 这种数据标准化方法经过处理后数据符合标准正态分布,即均值为0,标准差为1,
# 转化函数为:x =(x - 𝜇)/𝜎
X_train_np = sc.fit_transform(X_train_np)
X_test_np = sc.transform(X_test_np)
#更改格式numpy类为tensor类
X_train = torch.from_numpy(X_train_np)
X_test = torch.from_numpy(X_test_np)
y_train = torch.from_numpy(y_train_np)
y_test = torch.from_numpy(y_test_np)
# print('训练集X',X_train,'训练集X行列',X_train.shape)
# print('测试集X',X_test,'测试集X行列',X_train.shape)
# print('训练集y',y_train,'训练集y行列',X_train.shape)
# print('测试集y',y_test,'测试集y行列',X_train.shape)
# print(X_train.view(-1, 30))
# print( y_train.view(-1, 1))
# 然后构造模型
class MyLogisticRegression(nn.Module):
def __init__(self, input_features):
super().__init__()
self.linear = nn.Linear(input_features, 1)
def forward(self, x):#前向计算
y = self.linear(x)
return torch.sigmoid(y)
input_features = 30
model = MyLogisticRegression(30)
# Loss和Optimizer
lr = 0.2
iterations = 100
#代价函数形式为logistic回归的代价函数
loss_fn=nn.BCELoss()
#需要命令变量不能直接调用
optimizer = torch.optim.SGD(model.parameters(), lr=lr)
# 之后训练模型
for epoch in range(iterations):
# forward计算loss
y_pred = model(X_train)
loss = loss_fn(y_pred, y_train.unsqueeze(1))
#y_pred其实就是h(x)
# y_pred = model(X_train.view(-1, input_features))
# loss = loss_fn(y_pred.view(-1, 1), y_train.view(-1, 1))
# backward更新parameters
loss.backward()
optimizer.step()
optimizer.zero_grad()
with torch.no_grad():
y_pred_test = model(X_test.view(-1, input_features))
y_pred_test = y_pred_test .round().squeeze()
total_correct = y_pred_test.eq(y_test).sum()
prec = total_correct.item() / len(y_test)
print(f'epoch {epoch}, loss {loss.item()}, prec {prec}')
pytorch实现logistic回归
最新推荐文章于 2024-07-12 20:12:19 发布