当前位置:   article > 正文

PyTorch 实现逻辑回归(机器学习训练的五大模块)_机器学习模块

机器学习模块

机器学习训练的五大模块

  • 数据
  • 模型
  • 损失函数
  • 优化器
  • 模型训练
    • 前向传播
    • 计算损失
    • 反向传播
    • 更新参数
    • 计算准确率

数据

# 数据
sample_nums = 100
mean_value = 1.7
bias = 1
n_data = torch.ones(size=(sample_nums,2))
x0 = torch.normal(mean=mean_value*n_data,std=1) + bias
y0 = torch.zeros(sample_nums)
x1 = torch.normal(mean=-mean_value*n_data,std=1) + bias
y1 = torch.ones(sample_nums)
train_x = torch.cat(tensors=(x0,x1),dim=0)
train_y = torch.cat(tensors=(y0,y1),dim=0)
print(train_x.shape)
print(train_y.shape)
# print(train_y.size(0))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

模型

# 模型
class LR(nn.Module):
    def __init__(self):
        super(LR, self).__init__()
        self.features = nn.Linear(2,1)
        self.sigmoid = nn.Sigmoid()
    
    def forward(self,x):
        x = self.features(x)
        x = self.sigmoid(x)
        return x
        
LR_net = LR()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

损失函数

# 损失函数
loss_fn = nn.BCELoss()
  • 1
  • 2

优化器

# 优化器
lr = 0.01
optimizer = torch.optim.SGD(params=LR_net.parameters(),lr=lr, momentum=0.9)
  • 1
  • 2
  • 3

模型训练

# 模型训练
for idx,iteration in enumerate(range(1000)):
    ## 前向传播
    y_pred = LR_net(x=train_x)

    ## 计算损失
    loss = loss_fn(input=y_pred.squeeze(),target=train_y)
    
    ## 反向传播
    loss.backward()

    ## 更新参数
    optimizer.step()

    ## 计算准确率
    mask = y_pred.ge(0.5).float().squeeze()
    correct = (mask == train_y).sum()
    acc = correct.item() / train_y.size(0)

    print(idx,acc)
    if acc > 0.99:
        break
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/887054
推荐阅读
相关标签
  

闽ICP备14008679号