PyTorch实现逻辑回归

PyTorch实现逻辑回归

 线性回归是解决回归问题的,逻辑回归和线性回归很像,
 但是它是解决分类问题的(一般 二分类问题:0 or 1)。
 也可以多分类问题(用softmax可以实现)。
import torch
from torch.autograd import Variable

x_data = Variable(torch.Tensor([[0.6], [1.0], [3.5], [4.0]]))
y_data = Variable(torch.Tensor([[0.], [0.], [1.], [1.]]))

class Model(torch.nn.Module):
  def __init__(self):
    super(Model, self).__init__()
    self.linear = torch.nn.Linear(1, 1) # One in one out
    self.sigmoid = torch.nn.Sigmoid()

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

#Our model
model = Model()

#Construct loss function and optimizer
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

#Training loop
for epoch in range(500):
  # Forward pass
  y_pred = model(x_data)

  # Compute loss
  loss = criterion(y_pred, y_data)
  if epoch % 20 == 0:
    print(epoch, loss.data[0])

  # Zero gradients
  optimizer.zero_grad()
  # Backward pass
  loss.backward()
  # update weights
  optimizer.step()

 After training
hour_var = Variable(torch.Tensor([[0.5]]))
print("predict (after training)", 0.5, model.forward(hour_var).data[0][0])
hour_var = Variable(torch.Tensor([[7.0]]))
print("predict (after training)", 7.0, model.forward(hour_var).data[0][0])

输出结果

0 0.9983477592468262
 20 0.850886881351471
 40 0.7772406339645386
 60 0.7362991571426392
 80 0.7096697092056274
 100 0.6896909475326538
 120 0.6730546355247498
 140 0.658246636390686
 160 0.644534170627594
 180 0.6315458416938782
 200 0.6190851330757141
 220 0.607043981552124
 240 0.5953611731529236
 260 0.5840001106262207
 280 0.5729377269744873
 300 0.5621585845947266
 320 0.5516515970230103
 340 0.5414079427719116
 360 0.5314203500747681
 380 0.5216821432113647
 400 0.512187123298645
 420 0.5029295086860657
 440 0.49390339851379395
 460 0.4851033389568329
 480 0.47652381658554077

你可能感兴趣的:(深度学习,pytorch,逻辑回归)