(1)逻辑回归原理
关于逻辑回归原理课可参考这一部分
https://blog.csdn.net/weixin_39445556/article/details/83930186
(2)使用pytorch框架实现对MNIST数据集分类
"""
Created on Sun Apr 28 11:41:28 2019
@author: hp
"""
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torchvision.datasets import MNIST
# Hyper-parameters
input_size = 784 #输入层神经元大小
num_classes = 10 #种类类别,数字0-9
num_epochs = 25#迭代次数
batch_size = 100 #每次训练取得样本数
learning_rate = 0.5 #学习率
# MNIST dataset (images and labels)
#训练集
train_dataset = torchvision.datasets.MNIST(root='data/',
train=True,
transform=transforms.ToTensor(),
download=True)
#测试集
test_dataset = torchvision.datasets.MNIST(root='data/',
train=False,
transform=transforms.ToTensor())
# Data loader (input pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# Logistic regression model
model = nn.Linear(input_size, num_classes)
# Loss and optimizer 损失函数和优化器
# nn.CrossEntropyLoss() computes softmax internally
criterion = nn.CrossEntropyLoss() #采用多分类交叉熵损失函数
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# Train the model 训练模型
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Reshape images to (batch_size, input_size)
images = images.reshape(-1, 28*28)
# Forward pass 前向传播
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize 反向传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, 28*28)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')
(3)结果如下
Epoch [1/10], Step [100/600], Loss: 0.4521
Epoch [1/10], Step [200/600], Loss: 0.3724
Epoch [1/10], Step [300/600], Loss: 0.3691
Epoch [1/10], Step [400/600], Loss: 0.5303
Epoch [1/10], Step [500/600], Loss: 0.3729
Epoch [1/10], Step [600/600], Loss: 0.3130
Epoch [2/10], Step [100/600], Loss: 0.3146
Epoch [2/10], Step [200/600], Loss: 0.2711
Epoch [2/10], Step [300/600], Loss: 0.2242
Epoch [2/10], Step [400/600], Loss: 0.2495
Epoch [2/10], Step [500/600], Loss: 0.2807
Epoch [2/10], Step [600/600], Loss: 0.2015
Epoch [3/10], Step [100/600], Loss: 0.4398
Epoch [3/10], Step [200/600], Loss: 0.2047
Epoch [3/10], Step [300/600], Loss: 0.3251
Epoch [3/10], Step [400/600], Loss: 0.3168
Epoch [3/10], Step [500/600], Loss: 0.2460
Epoch [3/10], Step [600/600], Loss: 0.3565
Epoch [4/10], Step [100/600], Loss: 0.1985
Epoch [4/10], Step [200/600], Loss: 0.2450
Epoch [4/10], Step [300/600], Loss: 0.3512
Epoch [4/10], Step [400/600], Loss: 0.2825
Epoch [4/10], Step [500/600], Loss: 0.3792
Epoch [4/10], Step [600/600], Loss: 0.2888
Epoch [5/10], Step [100/600], Loss: 0.3353
Epoch [5/10], Step [200/600], Loss: 0.1694
Epoch [5/10], Step [300/600], Loss: 0.3694
Epoch [5/10], Step [400/600], Loss: 0.2912
Epoch [5/10], Step [500/600], Loss: 0.2994
Epoch [5/10], Step [600/600], Loss: 0.1766
Epoch [6/10], Step [100/600], Loss: 0.2142
Epoch [6/10], Step [200/600], Loss: 0.2137
Epoch [6/10], Step [300/600], Loss: 0.2421
Epoch [6/10], Step [400/600], Loss: 0.3232
Epoch [6/10], Step [500/600], Loss: 0.2588
Epoch [6/10], Step [600/600], Loss: 0.3145
Epoch [7/10], Step [100/600], Loss: 0.1498
Epoch [7/10], Step [200/600], Loss: 0.2251
Epoch [7/10], Step [300/600], Loss: 0.2388
Epoch [7/10], Step [400/600], Loss: 0.2169
Epoch [7/10], Step [500/600], Loss: 0.1757
Epoch [7/10], Step [600/600], Loss: 0.1307
Epoch [8/10], Step [100/600], Loss: 0.1784
Epoch [8/10], Step [200/600], Loss: 0.2199
Epoch [8/10], Step [300/600], Loss: 0.2975
Epoch [8/10], Step [400/600], Loss: 0.2000
Epoch [8/10], Step [500/600], Loss: 0.3108
Epoch [8/10], Step [600/600], Loss: 0.5101
Epoch [9/10], Step [100/600], Loss: 0.2494
Epoch [9/10], Step [200/600], Loss: 0.1489
Epoch [9/10], Step [300/600], Loss: 0.1862
Epoch [9/10], Step [400/600], Loss: 0.2729
Epoch [9/10], Step [500/600], Loss: 0.2638
Epoch [9/10], Step [600/600], Loss: 0.2751
Epoch [10/10], Step [100/600], Loss: 0.2940
Epoch [10/10], Step [200/600], Loss: 0.2902
Epoch [10/10], Step [300/600], Loss: 0.4425
Epoch [10/10], Step [400/600], Loss: 0.2188
Epoch [10/10], Step [500/600], Loss: 0.1756
Epoch [10/10], Step [600/600], Loss: 0.1794
Accuracy of the model on the 10000 test images: 92 %
采用逻辑回归,对MNIST数据集进行10次迭代,最后识别正确率为92%