-
Notifications
You must be signed in to change notification settings - Fork 0
/
简单全连接MINIST(1个隐藏层,sigmoid激活函数).py
122 lines (114 loc) · 5.69 KB
/
简单全连接MINIST(1个隐藏层,sigmoid激活函数).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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#引入相关库
import torch
import torch.optim as optim
from torch.autograd import Variable
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
#定义网络
class Batch_Net(nn.Module):
def __init__(self, in_dim, n_hidden_1,out_dim):
super(Batch_Net, self).__init__()
self.layer1 = nn.Sequential(
nn.Linear(in_dim, n_hidden_1),
nn.Sigmoid())
self.layer2 = nn.Sequential(
nn.Linear(n_hidden_1,out_dim))
def forward(self, x):
x = x.view(x.size()[0], -1)#展平
hidden_1_out = self.layer1(x)
out=self.layer2(hidden_1_out)
return out
# 定义超参数
EPOCH = 20 #遍历数据集次数
pre_epoch = 0 # 定义已经遍历数据集的次数
BATCH_SIZE = 64 #批处理尺寸(batch_size)
LR = 0.001 #学习率
# 启用GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#加载数据集
train_loader = torch.utils.data.DataLoader( # 加载训练数据
datasets.MNIST('./data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)) # 数据集给出的均值和标准差系数,每个数据集都不同的,都数据集提供方给出的
])),
batch_size=BATCH_SIZE, shuffle=True)
test_loader = torch.utils.data.DataLoader( # 加载训练数据
datasets.MNIST('./data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)) # 数据集给出的均值和标准差系数,每个数据集都不同的,都数据集提供方给出的
])),
batch_size=BATCH_SIZE, shuffle=True)
# 实例化一个网络对象
model = Batch_Net(28*28,300,10)
model = model.to(device)
#定义损失函数
criterion = nn.CrossEntropyLoss() #损失函数为交叉熵,多用于多分类问题
optimizer = optim.Adam(model.parameters(), lr=LR) #优化方式为Adam
# 训练开始
if __name__ == "__main__":
best_acc = 85 #2 初始化best test accuracy
print("Start Training, 简单全连接MINIST(1个隐藏层,sigmoid激活函数)!")
with open("简单全连接MINIST(1个隐藏层,sigmoid激活函数)acc.txt", "w") as f:
with open("简单全连接MINIST(1个隐藏层,sigmoid激活函数)log.txt", "w")as f2:
for epoch in range(pre_epoch, EPOCH):
print('\nEpoch: %d' % (epoch + 1))
model.train()
sum_loss = 0.0
correct = 0.0
total = 0.0
for i, data in enumerate(train_loader, 0):
# 准备数据
length = len(train_loader)
inputs, labels = data
inputs, labels = Variable(inputs), Variable(labels)
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
# forward + backward
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 每训练1个batch打印一次loss和准确率
sum_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += predicted.eq(labels.data).cpu().sum()
print('[epoch:%d, iter:%d] Loss: %.03f | Acc: %.3f%% '
% (epoch + 1, (i + 1 + epoch * length), sum_loss / (i + 1), 100. * correct / total))
f2.write('%03d %05d |Loss: %.03f | Acc: %.3f%% '
% (epoch + 1, (i + 1 + epoch * length), sum_loss / (i + 1), 100. * correct / total))
f2.write('\n')
f2.flush()
# 每训练完一个epoch测试一下准确率
print("Waiting Test!")
model.eval()
with torch.no_grad():
correct = 0
total = 0
for data in test_loader:
model.eval()
images, labels = data
images, labels = Variable(images), Variable(labels)
images, labels = images.to(device), labels.to(device)
outputs = model(images)
# 取得分最高的那个类 (outputs.data的索引号)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('测试分类准确率为:%.3f%%' % (100 * correct / total))
acc = 100. * correct / total
# 将每次测试结果实时写入acc.txt文件中
f.write("EPOCH=%03d,Accuracy= %.3f%%" % (epoch + 1, acc))
f.write('\n')
f.flush()
# 记录最佳测试分类准确率并写入best_acc.txt文件中
if acc > best_acc:
f3 = open("简单全连接MINIST(1个隐藏层,sigmoid激活函数)best_acc.txt", "w")
f3.write("EPOCH=%d,best_acc= %.3f%%" % (epoch + 1, acc))
f3.close()
best_acc = acc
print('Saving model......')
torch.save(model, '简单全连接MINIST(1个隐藏层,sigmoid激活函数)_%03d.pth' % (epoch + 1))
print("Training Finished, TotalEPOCH=%d" % EPOCH)