pytorch实现logistic回归

 
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}')

                
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值