Pytorch实践----06.Logistic Regression逻辑斯蒂回归

学习

刘二大人《PyTorch深度学习实践》
B站地址:B站视频
逻辑斯蒂回归虽然叫和回归,但其实是为了解决分类问题

import torch
import torch.nn as nn
import torch.nn.functional as F

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


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

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


model = LogistRegressionModel()

criterion = nn.BCELoss(size_average=False)

optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

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

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

你可能感兴趣的:(Pytorch,pytorch,回归,深度学习,人工智能,机器学习)