-
Notifications
You must be signed in to change notification settings - Fork 15
/
plot1.py
99 lines (87 loc) · 2.54 KB
/
plot1.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
import argparse
import math
import sys
import os
parser=argparse.ArgumentParser()
parser.add_argument('-f',default='',type=str)
parser.add_argument('-fold',default=0,type=int)
parser.add_argument('-avg',default=1,type=int)
parser.add_argument('-test',default=1,type=int)
args=parser.parse_args()
files=[os.path.join(args.f,x) for x in os.listdir(args.f)]
test_acc={}
train_acc={}
train_cnt={}
test_cnt={}
layer_acc=[{},{},{},{}]
train_record=[]
test_record=[]
for i in range(1000):
train_record.append([])
test_record.append([])
max_layer=-1
flag=0
for x in files:
f=open(x,'r')
for l in f.readlines():
if ('of epoch' in l) and ('layer0' in l):
for k in range(100):
if ('layer%d'%k) in l:
max_layer=max(max_layer,k+1)
break
f.close()
f=open(x,'r')
for l in f.readlines():
if 'of epoch' not in l:
continue
words=l.strip().split(' ')
epoch=int(words[words.index('epoch')+1][:-1])
if epoch not in train_acc.keys():
train_acc[epoch]=0
test_acc[epoch]=0
train_cnt[epoch]=0
test_cnt[epoch]=0
for k in range(max_layer):
layer_acc[k][epoch]=0
if args.avg:
acc=float(words[words.index('avg_acc')+1])
else:
acc=float(words[words.index('acc')+1])
if 'test' in l:
test_cnt[epoch]+=1
test_acc[epoch]+=acc
test_record[epoch].append(acc)
for k in range(max_layer):
acc_k=float(words[words.index('layer%d'%k)+1])
layer_acc[k][epoch]+=acc_k
else:
train_cnt[epoch]+=1
train_acc[epoch]+=acc
train_record[epoch].append(acc)
flag=1
def var(l,avg):
sqsum=0
for x in l:
sqsum+=(x-avg)**2
return math.sqrt(sqsum/len(l))
max_acc=0
max_epoch=-1
for i,x in enumerate(sorted(train_acc.keys())):
if not args.test:
if train_cnt[x]<args.fold:
continue
acc=train_acc[x]/train_cnt[x]
print('%d\t%.4f\t%.6f'%(i+1,acc,var(train_record[i+1],acc)))
else:
if test_cnt[x]<args.fold:
continue
acc=test_acc[x]/test_cnt[x]
s='%d\t%.4f\t%.4f'%(i+1,acc,var(test_record[i+1],acc))
for k in range(max_layer):
s+='\t%.4f'%(layer_acc[k][x]/test_cnt[x])
print(s)
if max_acc<acc:
max_acc=acc
max_epoch=i+1
print('max',max_acc)
print('max acc: %.4f, epoch %d'%(max_acc,max_epoch))