-
Notifications
You must be signed in to change notification settings - Fork 9
/
Pytorch_logisticRegression.py
81 lines (65 loc) · 2.7 KB
/
Pytorch_logisticRegression.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# 超参数
input_size = 784
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001
# 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())
# 数据加载器
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)
# 逻辑回归模型
model = nn.Linear(input_size, num_classes)
# 损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# 训练模型
total_step = len(train_loader)
for epoch in range(num_epoches):
for i, (images,labels) in enumerate(train_loader):
# Reshape images to (batch_size, input_size)
images = images.reshape(-1,28*28)
# forward 计算
outputs = model(images)
loss = criterion(outputs, labels)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 打印信息
running_loss += loss.item()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, running_loss))
# 测试模型
# 在测试阶段,为了效率,不需进行梯度计算
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1,28*28)
outputs = model(images)
# 分类问题,挑选出最大概率的一个
# outputs是输出的10个类别的概率,输出最大的索引,1代表输出列方向索引
# _ 代表忽略返回的最大概率值
_, 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))
# 保存模型参数
torch.save(model.state_dict(), 'model.ckpt')