Skip to content

Commit 1b9ac49

Browse files
committed
打印学习率
1 parent 7bab986 commit 1b9ac49

File tree

2 files changed

+68
-106
lines changed

2 files changed

+68
-106
lines changed

pytorch-06/README.md

Lines changed: 50 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,65 @@
11

2-
3-
## 1 使用net.py
2+
## 1 使用cnn_lenet.py
43
```bash
5-
Accuracy of the network on the 10000 test images : 59 %
6-
[10, 2000] loss: 0.801
7-
Accuracy of the network on the 10000 test images : 61 %
8-
[10, 4000] loss: 0.845
9-
Accuracy of the network on the 10000 test images : 62 %
10-
[10, 6000] loss: 0.859
11-
Accuracy of the network on the 10000 test images : 60 %
12-
[10, 8000] loss: 0.876
13-
Accuracy of the network on the 10000 test images : 61 %
14-
[10, 10000] loss: 0.885
15-
Accuracy of the network on the 10000 test images : 61 %
16-
[10, 12000] loss: 0.881
17-
Accuracy of the network on the 10000 test images : 62 %
4+
2021-08-07 03:17:48 [10, 2000] train acc: 0.889 loss: 0.308 ; val acc: 0.694 loss: 0.299 lr: 0.001
5+
2021-08-07 03:18:07 [10, 4000] train acc: 0.869 loss: 0.356 ; val acc: 0.689 loss: 0.295 lr: 0.001
6+
2021-08-07 03:18:26 [10, 6000] train acc: 0.863 loss: 0.387 ; val acc: 0.678 loss: 0.307 lr: 0.001
7+
2021-08-07 03:18:46 [10, 8000] train acc: 0.856 loss: 0.414 ; val acc: 0.680 loss: 0.303 lr: 0.001
8+
2021-08-07 03:19:06 [10, 10000] train acc: 0.848 loss: 0.431 ; val acc: 0.689 loss: 0.295 lr: 0.001
9+
2021-08-07 03:19:25 [10, 12000] train acc: 0.844 loss: 0.445 ; val acc: 0.678 loss: 0.295 lr: 0.001
1810
Finished Training
19-
GroundTruth: cat ship ship plane
20-
Accuracy of plane : 73 %
21-
Accuracy of car : 78 %
22-
Accuracy of bird : 47 %
23-
Accuracy of cat : 47 %
24-
Accuracy of deer : 55 %
25-
Accuracy of dog : 52 %
26-
Accuracy of frog : 74 %
27-
Accuracy of horse : 67 %
28-
Accuracy of ship : 66 %
29-
Accuracy of truck : 61 %
30-
```
31-
32-
33-
34-
## 2 使用cnn_net.py
35-
```bash
36-
[10, 2000] loss: 0.291
37-
Accuracy of the network on the 10000 test images : 68 %
38-
[10, 4000] loss: 0.321
39-
Accuracy of the network on the 10000 test images : 69 %
40-
[10, 6000] loss: 0.353
41-
Accuracy of the network on the 10000 test images : 68 %
42-
[10, 8000] loss: 0.398
43-
Accuracy of the network on the 10000 test images : 67 %
44-
[10, 10000] loss: 0.445
45-
Accuracy of the network on the 10000 test images : 67 %
46-
[10, 12000] loss: 0.438
47-
Accuracy of the network on the 10000 test images : 68 %
48-
Finished Training
49-
GroundTruth: cat ship ship plane
50-
Accuracy of plane : 62 %
51-
Accuracy of car : 77 %
52-
Accuracy of bird : 58 %
53-
Accuracy of cat : 51 %
54-
Accuracy of deer : 65 %
55-
Accuracy of dog : 61 %
11+
Accuracy of plane : 72 %
12+
Accuracy of car : 84 %
13+
Accuracy of bird : 61 %
14+
Accuracy of cat : 45 %
15+
Accuracy of deer : 56 %
16+
Accuracy of dog : 59 %
5617
Accuracy of frog : 80 %
57-
Accuracy of horse : 70 %
58-
Accuracy of ship : 80 %
59-
Accuracy of truck : 80 %
18+
Accuracy of horse : 72 %
19+
Accuracy of ship : 81 %
20+
Accuracy of truck : 68 %
6021
```
6122

62-
## 3 使用net_gap.py
23+
## 2 使用net_gap.py
6324
```bash
64-
[10, 2000] loss: 1.008
65-
Accuracy of the network on the 10000 test images : 62 %
66-
[10, 4000] loss: 0.990
67-
Accuracy of the network on the 10000 test images : 64 %
68-
[10, 6000] loss: 0.993
69-
Accuracy of the network on the 10000 test images : 63 %
70-
[10, 8000] loss: 0.978
71-
Accuracy of the network on the 10000 test images : 64 %
72-
[10, 10000] loss: 0.991
73-
Accuracy of the network on the 10000 test images : 63 %
74-
[10, 12000] loss: 0.998
75-
Accuracy of the network on the 10000 test images : 64 %
25+
2021-08-07 03:19:08 [10, 2000] train acc: 0.651 loss: 1.004 ; val acc: 0.616 loss: 0.272 lr: 0.001
26+
2021-08-07 03:19:27 [10, 4000] train acc: 0.655 loss: 0.982 ; val acc: 0.650 loss: 0.253 lr: 0.001
27+
2021-08-07 03:19:45 [10, 6000] train acc: 0.653 loss: 0.993 ; val acc: 0.629 loss: 0.264 lr: 0.001
28+
2021-08-07 03:20:04 [10, 8000] train acc: 0.637 loss: 1.015 ; val acc: 0.633 loss: 0.265 lr: 0.001
29+
2021-08-07 03:20:22 [10, 10000] train acc: 0.656 loss: 0.984 ; val acc: 0.634 loss: 0.259 lr: 0.001
30+
2021-08-07 03:20:41 [10, 12000] train acc: 0.665 loss: 0.971 ; val acc: 0.623 loss: 0.268 lr: 0.001
7631
Finished Training
77-
Accuracy of plane : 76 %
78-
Accuracy of car : 84 %
79-
Accuracy of bird : 42 %
80-
Accuracy of cat : 25 %
81-
Accuracy of deer : 41 %
82-
Accuracy of dog : 65 %
83-
Accuracy of frog : 84 %
84-
Accuracy of horse : 56 %
85-
Accuracy of ship : 74 %
86-
Accuracy of truck : 72 %
32+
Accuracy of plane : 64 %
33+
Accuracy of car : 57 %
34+
Accuracy of bird : 34 %
35+
Accuracy of cat : 44 %
36+
Accuracy of deer : 39 %
37+
Accuracy of dog : 63 %
38+
Accuracy of frog : 75 %
39+
Accuracy of horse : 71 %
40+
Accuracy of ship : 80 %
41+
Accuracy of truck : 90 %
8742
```
8843

8944

90-
## 4 vgg.py
45+
## 3 vgg.py
9146
```bash
92-
Accuracy of the network on the 10000 test images : 82 %
93-
[10, 2000] loss: 0.190
94-
Accuracy of the network on the 10000 test images : 81 %
95-
[10, 4000] loss: 0.201
96-
Accuracy of the network on the 10000 test images : 82 %
97-
[10, 6000] loss: 0.214
98-
Accuracy of the network on the 10000 test images : 82 %
99-
[10, 8000] loss: 0.210
100-
Accuracy of the network on the 10000 test images : 82 %
101-
[10, 10000] loss: 0.215
102-
Accuracy of the network on the 10000 test images : 81 %
103-
[10, 12000] loss: 0.229
104-
Accuracy of the network on the 10000 test images : 81 %
47+
2021-08-07 04:01:42 [10, 2000] train acc: 0.948 loss: 0.156 ; val acc: 0.828 loss: 0.159 lr: 0.001
48+
2021-08-07 04:02:41 [10, 4000] train acc: 0.946 loss: 0.158 ; val acc: 0.828 loss: 0.158 lr: 0.001
49+
2021-08-07 04:03:41 [10, 6000] train acc: 0.945 loss: 0.170 ; val acc: 0.829 loss: 0.154 lr: 0.001
50+
2021-08-07 04:04:40 [10, 8000] train acc: 0.939 loss: 0.174 ; val acc: 0.822 loss: 0.162 lr: 0.001
51+
2021-08-07 04:05:40 [10, 10000] train acc: 0.941 loss: 0.175 ; val acc: 0.821 loss: 0.169 lr: 0.001
52+
2021-08-07 04:06:40 [10, 12000] train acc: 0.939 loss: 0.188 ; val acc: 0.820 loss: 0.163 lr: 0.001
10553
Finished Training
106-
Accuracy of plane : 77 %
107-
Accuracy of car : 94 %
108-
Accuracy of bird : 74 %
109-
Accuracy of cat : 58 %
110-
Accuracy of deer : 86 %
111-
Accuracy of dog : 78 %
112-
Accuracy of frog : 89 %
113-
Accuracy of horse : 87 %
114-
Accuracy of ship : 90 %
115-
Accuracy of truck : 87 %
54+
Accuracy of plane : 84 %
55+
Accuracy of car : 89 %
56+
Accuracy of bird : 68 %
57+
Accuracy of cat : 77 %
58+
Accuracy of deer : 79 %
59+
Accuracy of dog : 75 %
60+
Accuracy of frog : 81 %
61+
Accuracy of horse : 89 %
62+
Accuracy of ship : 89 %
63+
Accuracy of truck : 93 %
11664
```
11765

pytorch-06/cifar.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from load_data import load_data_cifar, classes
77
import argparse
88

9-
10-
device = torch.device("cuda:0" if torch.cuda.is_available() else 'cpu')
9+
device = None
10+
# device = torch.device("cuda:0" if torch.cuda.is_available() else 'cpu')
1111
# print(device)
1212

1313

@@ -34,6 +34,11 @@ def train(net, trainloader, testloader, num_epoch):
3434
criterion = nn.CrossEntropyLoss()
3535
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
3636

37+
# Print optimizer's state_dict
38+
print("Optimizer's state_dict:")
39+
for var_name in optimizer.state_dict():
40+
print(var_name, "\t", optimizer.state_dict()[var_name])
41+
3742
for epoch in range(num_epoch): # loop over the dataset multiple times
3843
running_loss = 0.0
3944
running_acc = 0
@@ -63,20 +68,29 @@ def train(net, trainloader, testloader, num_epoch):
6368
str_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
6469
train_acc = running_acc / (2000 * batch_size)
6570
train_loss = running_loss / 2000
66-
print('%s [%d, %5d] train acc: %.3f loss: %.3f ; val acc: %.3f loss: %.3f' % (str_time, epoch + 1, i + 1, train_acc, train_loss, acc, val_loss))
71+
lr = optimizer.state_dict()['param_groups'][0]['lr']
72+
print('%s [%d, %5d] train acc: %.3f loss: %.3f ; val acc: %.3f loss: %.3f lr: %.3f' % (str_time, epoch + 1, i + 1, train_acc, train_loss, acc, val_loss, lr))
73+
# print('\t', optimizer.state_dict()['param_groups'])
6774
running_loss = 0.0
6875
running_acc = 0
6976
print('Finished Training')
7077

7178

7279
def get_args():
7380
parser = argparse.ArgumentParser()
74-
parser.add_argument('--model', default='lenet5', help='cnn, gap, vgg')
81+
parser.add_argument('--model', default='lenet5', help='lenet5, gap, vgg')
7582
parser.add_argument('--num_epoch', default=10, type=int)
83+
parser.add_argument('--gpu_id', default=0, type=int)
7684
return parser.parse_args()
7785

7886

7987
def main(args):
88+
global device
89+
if args.gpu_id >= 0 and torch.cuda.is_available():
90+
device = torch.device("cuda:{}".format(args.gpu_id))
91+
else:
92+
device = torch.device("cpu")
93+
8094
if args.model == 'lenet5':
8195
from net_lenet import Net
8296
net = Net()

0 commit comments

Comments
 (0)