当前位置:   article > 正文

PyTorch实现Logistic Regression (mini-batch,多维特征处理)_logistic regression 特征处理

logistic regression 特征处理

1. Logistic Regression

import torch
import torch.nn.functional as F  # functional下有很多函数包

x_data = torch.Tensor([[1.0], [2.0], [3.0]])  # 3*1 Tensor
y_data = torch.Tensor([[0], [0], [1]])  # 3*1 Tensor


class LogisticRegressionModel(torch.nn.Module):
    def __init__(self):
        super(LogisticRegressionModel, self).__init__()
        self.linear = torch.nn.Linear(1, 1)

    def forward(self, x):
        y_pred = F.sigmoid(self.linear(x))
        return y_pred


model = LogisticRegressionModel()

critetion = torch.nn.BCELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

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

    optimizer.zero_grad()
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/179900
推荐阅读
相关标签
  

闽ICP备14008679号