-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2d_five_optimize.py
More file actions
211 lines (184 loc) · 8.23 KB
/
2d_five_optimize.py
File metadata and controls
211 lines (184 loc) · 8.23 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import mnist_web
import select
import numpy as np
import random
import sys
from dataloader import DataLoader
from cradle import Cradle
import torch
import time
from tqdm import tqdm
random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
dl = DataLoader(True,cuda = 1)
images,labels = dl.get_all()
images, labels = images[:18000],labels[:18000]
dl_test = DataLoader(False,cuda = 1)
images_t,labels_t = dl_test.get_all()
images_t, labels_t = images_t,labels_t
def get_classfication_score_table(o,labels):
r = o.mm( o.t().mm(labels) )
r = (r + labels.sum(0).unsqueeze(0)) / 2
return r
def show_gather(o, labels):
r = o.t().mm(labels)
r = (r + labels.sum(0).unsqueeze(0)) / 2
output_str = ''
for i in range(10):
output_str += '%7d'%(r[0][i])
print(output_str)
def get_loss(class_s_table, labels):
r = class_s_table / class_s_table.sum(1).unsqueeze(1)
r = -torch.sum(torch.log(r) * labels,1)
r = r.mean()
return r
def show_accuarcate(r, labels, train=True):#r:classfication_score_table
a = torch.argmax(r,1)
b = labels.argmax(1)
accuarcate = torch.mean((a==b).float())*100
if train:
print('Train accuarcate:%6.2f%%\n\n'%(accuarcate))
else:
print('Test accuarcate:%6.2f%%\n\n'%(accuarcate))
return accuarcate
#if __name__ == '__main__':
# for iteration in range(10):
# print('iteration:\n%5d'%iteration)
# out_accumu = 0
# avoid_repeat_list = []
# for f in range(5):
# best = {'column':0, 'bit_w':0, 'loss':9999, 'o':None}
# for column in tqdm(range(images.shape[1]), leave=False):
# if column in avoid_repeat_list:
# continue
# for bit_w in [-1,1]:
# rand_swing = torch.randint(0, 2, (images.shape[0],)).cuda().float()
# o = out_accumu + bit_w * images[:,column] + rand_swing
# o = (o >= 1).float().unsqueeze(1)
# o = o * 2 - 1
# r = get_classfication_score_table(o, labels)
# r = get_loss(r, labels)
# if r < best['loss']:
# best['loss'] = r.item()
# best['column'] = column
# best['bit_w'] = bit_w
# best['o'] = o
# print('%5d %5d bit_w:%2d loss:%8.5f'%\
# (f,best['column'],best['bit_w'],best['loss']))
# out_accumu += images[:,best['column']] * best['bit_w']
# avoid_repeat_list.append(best['column'])
#
# o = (out_accumu > 0).float().unsqueeze(1)
# o = o * 2 - 1
# r = get_classfication_score_table(o,labels)
# show_gather(best['o'],labels)
# show_accuarcate(r, labels)
# sys.exit(0)
if __name__ == '__main__':
for iteration in range(10):
print('iteration:\n%5d'%iteration)
out_accumu = 0
avoid_repeat_list = []
f = 1
best = {'column':0, 'bit_w':0, 'loss':9999, 'o':None}
for column in tqdm(range(images.shape[1]), leave=False):
if column in avoid_repeat_list:
continue
for bit_w in [-1,1]:
o = out_accumu + bit_w * images[:,column]
o = (o >= 0).float().unsqueeze(1)
o = o * 2 - 1
r = get_classfication_score_table(o, labels)
r = get_loss(r, labels)
if r < best['loss']:
best['loss'] = r.item()
best['column'] = column
best['bit_w'] = bit_w
best['o'] = o
print('%5d %5d bit_w:%2d loss:%8.5f'%\
(f,best['column'],best['bit_w'],best['loss']))
out_accumu += images[:,best['column']] * best['bit_w']
avoid_repeat_list.append(best['column'])
best = {'column1':0, 'bit_w1':0, 'column2':0, 'bit_w2':0, 'loss':9999, 'o':None}
for column1 in [376]:
for column2 in [429]:
if column1 in avoid_repeat_list:
continue
if column2 in avoid_repeat_list:
continue
for bit_w1 in [-1,1]:
for bit_w2 in [-1,1]:
o = out_accumu + bit_w1 * images[:,column1] + bit_w2 * images[:,column2]
o = (o >= 0).float().unsqueeze(1)
o = o * 2 - 1
r = get_classfication_score_table(o, labels)
r = get_loss(r, labels)
if r < best['loss']:
best['loss'] = r.item()
best['column1'] = column1
best['bit_w1'] = bit_w1
best['column2'] = column2
best['bit_w2'] = bit_w2
best['o'] = o
print('%5d %5d bit_w:%2d loss:%8.5f'%\
(1,best['column1'],best['bit_w1'],best['loss']))
print('%5d %5d bit_w:%2d loss:%8.5f'%\
(2,best['column2'],best['bit_w2'],best['loss']))
out_accumu += images[:,best['column1']] * best['bit_w1']
out_accumu += images[:,best['column2']] * best['bit_w2']
avoid_repeat_list.append(best['column1'])
avoid_repeat_list.append(best['column2'])
best = {'column1':0, 'bit_w1':0, 'column2':0, 'bit_w2':0, 'loss':9999, 'o':None}
for column1 in tqdm(range(images.shape[1]), leave=False):
for column2 in tqdm(range(images.shape[1]), leave=False):
if column1 in avoid_repeat_list:
continue
if column2 in avoid_repeat_list:
continue
for bit_w1 in [-1,1]:
for bit_w2 in [-1,1]:
o = out_accumu + bit_w1 * images[:,column1] + bit_w2 * images[:,column2]
o = (o >= 0).float().unsqueeze(1)
o = o * 2 - 1
r = get_classfication_score_table(o, labels)
r = get_loss(r, labels)
if r < best['loss']:
best['loss'] = r.item()
best['column1'] = column1
best['bit_w1'] = bit_w1
best['column2'] = column2
best['bit_w2'] = bit_w2
best['o'] = o
print('%5d %5d bit_w:%2d loss:%8.5f'%\
(3,best['column1'],best['bit_w1'],best['loss']))
print('%5d %5d bit_w:%2d loss:%8.5f'%\
(4,best['column2'],best['bit_w2'],best['loss']))
out_accumu += images[:,best['column1']] * best['bit_w1']
out_accumu += images[:,best['column2']] * best['bit_w2']
avoid_repeat_list.append(best['column1'])
avoid_repeat_list.append(best['column2'])
o = (out_accumu > 0).float().unsqueeze(1)
o = o * 2 - 1
r = get_classfication_score_table(o,labels)
show_gather(best['o'],labels)
show_accuarcate(r, labels)
sys.exit(0)
#iteration:
# 0
# 0 378 bit_w:-1 loss: 2.08234
# 1 350 bit_w:-1 loss: 2.10095
# 2 429 bit_w: 1 loss: 2.05335
# 3 405 bit_w:-1 loss: 2.06593
# 4 456 bit_w: 1 loss: 2.03012
# 1699 35 1214 134 1374 472 1279 1695 256 945
#Train accuarcate: 20.64%
#iteration:
# 0
# 1 378 bit_w:-1 loss: 2.08234
# 1 376 bit_w:-1 loss: 2.04831
# 2 429 bit_w: 1 loss: 2.04831
# 3 153 bit_w:-1 loss: 2.02929
# 4 399 bit_w: 1 loss: 2.02929
# 1640 30 730 143 1507 532 1071 1752 244 1236
#Train accuarcate: 20.96%