-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze_succ_prob.py
191 lines (153 loc) · 5.19 KB
/
analyze_succ_prob.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import argparse
import os
import re
from itertools import groupby
import scienceplots
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from mmpretrain.utils import load_json_log
def get_succ_prob(filename):
file=open(filename,'r')
succ_prob={}
for line in file.readlines():
temp=line.split(' ')
dim=int(temp[0])
sqrt_p=float(temp[1])
if dim in succ_prob.keys():
succ_prob[dim].append(sqrt_p)
else:
if dim>5000:
succ_prob[dim]=[sqrt_p]
# print(dim,sqrt_p)
return succ_prob
def get_main_prob(filename,r_dim):
file=open(filename,'r')
succ_prob=[]
# r_dim=((r_size/16)**2+1)*768
file_name_split=filename.split('_')
for line in file.readlines():
temp=line.split(' ')
dim=int(temp[0])
sqrt_p=float(temp[1])
if dim==r_dim:
if file_name_split[-1]=='backpropagation.txt' and file_name_split[-2]=='block-encoding':
if sqrt_p<0.05:
succ_prob.append(sqrt_p)
else:
succ_prob.append(sqrt_p)
return succ_prob
def plot_curve_main_succ_r(dataset,r_size_range,mode1_r,mode2_r):
x=[((r_size/16)**2+1)*768 for r_size in r_size_range]
for mode1 in mode1_r:
for mode2 in mode2_r:
y=[]
error_bar=[]
for r_size in r_size_range:
filename=f'prob_results/{dataset}/{dataset}_{r_size}_{mode1}_{mode2}.txt'
r_dim=((r_size/16)**2+1)*768
succ_prob=get_main_prob(filename,r_dim)
y.append(np.average(succ_prob))
temp1=np.std(succ_prob)
error_bar.append(temp1)
plt.scatter(x,y,label=f'{mode1}-{mode2}')
plt.errorbar(x, y, yerr=error_bar,capsize=3, capthick=2)
plt.yscale('log')
plt.xscale('log')
plt.legend(loc=1)
plt.show()
def plot_curve_main_succ_fit(dataset,r_size_range,mode1='qdac',mode2='backpropagation'):
x=[((r_size/16)**2+1)*768 for r_size in r_size_range]
y=[]
for r_size in r_size_range:
filename=f'prob_results_0122/{dataset}/{dataset}_{r_size}_{mode1}_{mode2}.txt'
r_dim=((r_size/16)**2+1)*768
succ_prob=get_main_prob(filename,r_dim)
y.append(np.average(succ_prob))
log_x=np.array([np.log10(i) for i in x])
log_y=np.array([np.log10(i) for i in y])
slope,intercept=np.polyfit(log_x,log_y,1)
fit_logy=[slope*i+intercept for i in log_x]
print(slope,intercept)
plt.scatter(log_x,log_y)
plt.plot(log_x,fit_logy,color='red')
plt.show()
def plot_curve_main_succ_mata(dataset,r_size_range,mode1_r):
# x=[((r_size/16)**2+1)*768 for r_size in r_size_range]
for mode1 in mode1_r:
error_bar=[]
y=[]
x=[]
for r_size in r_size_range:
filename=f'prob_results_0123/{dataset}/{dataset}_{r_size}_a_{mode1}_forward.txt'
if mode1=='qdac':
r_dim=((r_size/16)**2+1)**2
else:
r_dim=((r_size/16)**2+1)*64
x.append(r_dim)
succ_prob=get_main_prob(filename,r_dim)
y.append(np.average(succ_prob))
temp1=np.std(succ_prob)
error_bar.append(temp1)
plt.scatter(x,y,label=f'{mode1}-forward')
plt.errorbar(x, y, yerr=error_bar,capsize=3, capthick=2)
plt.yscale('log')
plt.xscale('log')
plt.legend(loc=1)
plt.show()
def plot_curve_main_succ(dataset,r_size_range,mode1,mode2):
x=[((r_size/16)**2+1)*768 for r_size in r_size_range]
y=[]
for r_size in r_size_range:
filename=f'prob_results/{dataset}/{dataset}_{r_size}_{mode1}_{mode2}.txt'
succ_prob=get_main_prob(filename,r_size)
y.append(np.average(succ_prob))
plt.scatter(x,y)
# plt.yscale('log')
plt.xscale('log')
plt.show()
def plot_curve_succ(dataset,r_size_range,mode1,mode2):
for r_size in r_size_range:
filename=f'prob_results/{dataset}/{dataset}_{r_size}_{mode1}_{mode2}.txt'
succ_prob=get_succ_prob(filename)
x=[keys for keys in succ_prob]
y=[]
error_bar=[]
for key in succ_prob:
temp=np.average(succ_prob[key])
temp1=np.std(succ_prob[key])
y.append(temp)
error_bar.append(temp1)
plt.errorbar(x, y, yerr=error_bar,capsize=3, capthick=2)
plt.scatter(x,y)
# plt.yscale('log')
# plt.xscale('log')
plt.show()
if __name__=="__main__":
# plot_loss_curve(filename_cifar100,dataset='cifar100',kind='val')
# dataset='cifar10'
dataset='cub'
# dataset='oxford_iii_pets'
# dataset='cifar100'
# r_size_range=[640,576,512,448,384,320,256,192,128,64]
r_size_range=[640,576,512,448,384,320,256,192,128,64]
# mode1='qdac'
# # mode1='block-encoding'
# mode2='forward'
# # mode2='backpropagation'
# # plot_curve_succ(dataset,r_size_range,mode1,mode2)
# plot_curve_main_succ(dataset,r_size_range,mode1,mode2)
mode1_r=['qdac','block-encoding']
mode2_r=['forward','backpropagation']
# plot_curve_main_succ_r(dataset,r_size_range,mode1_r,mode2_r)
plot_curve_main_succ_mata(dataset,r_size_range,mode1_r)
# plot_curve_main_succ_fit(dataset,r_size_range,mode1='qdac',mode2='backpropagation')
# r_size=384
# filename=f'prob_results/{dataset}/{dataset}_{r_size}_qdac_backpropagation.txt'
# succ_prob=get_succ_prob(filename)
# print(succ_prob.keys())
exit()
log_dicts=get_log_dicts(filename)[3]
# print(log_dicts)
plot_loss(log_dicts)
plot_accu(log_dicts)