1.介紹
- logistic回歸是一種廣義線性回歸打月,因此與多重線性回歸分析有很多相同之處短纵。它們的模型形式基本上相同,都具有 w'x+b僵控,其中w和b是待求參數(shù)香到,其區(qū)別在于他們的因變量不同,多重線性回歸直接將w‘x+b作為因變量,即y =w'x+b悠就,而logistic回歸則通過函數(shù)L將w‘x+b對應(yīng)一個隱狀態(tài)p千绪,p =L(w'x+b),然后根據(jù)p 與1-p的大小決定因變量的值。如果L是logistic函數(shù)梗脾,就是logistic回歸荸型,如果L是多項(xiàng)式函數(shù)就是多項(xiàng)式回歸。
- logistic回歸的因變量可以是二分類的炸茧,也可以是多分類的瑞妇,但是二分類的更為常用,也更加容易解釋梭冠,多類可以使用softmax方法進(jìn)行處理辕狰。實(shí)際中最為常用的就是二分類的logistic回歸。
2.模型訓(xùn)練
# -*- coding: utf-8 -*-
import torch
from torch import nn, optim
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision import datasets
import time
# 定義超參數(shù)
batch_size = 32
num_epoches = 10
learning_rate = 1e-3
# 下載訓(xùn)練集 MNIST 手寫數(shù)字訓(xùn)練集
train_dataset = datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor())
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
class Logstic_Regression(nn.Module):
"""邏輯回歸模型定義"""
def __init__(self, in_dim, n_class):
super(Logstic_Regression, self).__init__()
self.logstic = nn.Linear(in_dim, n_class)
def forward(self, x):
# 前向傳播
output = self.logstic(x)
return output
# 模型初始化
model = Logstic_Regression(28 * 28, 10) # 圖片大小是28x28
# 定義loss和optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
# 模型訓(xùn)練
for epoch in range(num_epoches):
print '#' * 45
print 'Epoch {}'.format(epoch + 1)
since = time.time()
running_loss = 0.0
running_acc = 0.0
for i, data in enumerate(train_loader, 1):
img, label = data
img = img.view(img.size(0), -1)
img = Variable(img)
label = Variable(label)
# 前向傳播
out = model(img)
loss = criterion(out, label)
running_loss += loss.item() * label.size(0)
_, pred = torch.max(out, 1)
num_correct = (pred == label).sum()
running_acc += num_correct.item()
# 后向傳播
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % 300 == 0:
print '[{}/{}] Loss: {:.6f}, Acc: {:.6f}'.format(
epoch + 1, num_epoches, running_loss / (batch_size * i),
running_acc / (batch_size * i))
print 'Finish {} epoch, Loss: {:.6f}, Acc: {:.6f}'.format(
epoch + 1, running_loss / (len(train_dataset)), running_acc / (len(
train_dataset)))
# 模型評估
model.eval()
eval_loss = 0.
eval_acc = 0.
for data in test_loader:
img, label = data
img = img.view(img.size(0), -1)
with torch.no_grad():
img = Variable(img)
label = Variable(label)
out = model(img)
loss = criterion(out, label)
eval_loss += loss.item() * label.size(0)
_, pred = torch.max(out, 1)
num_correct = (pred == label).sum()
eval_acc += num_correct.item()
print 'Test Loss: {:.6f}, Acc: {:.6f}'.format(eval_loss / (len(
test_dataset)), eval_acc / (len(test_dataset)))
print 'Time:{:.1f} s'.format(time.time() - since)
# 模型保存
torch.save(model.state_dict(), './Logistic_Regression.model')