-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
163 lines (135 loc) · 6.24 KB
/
run.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
import argparse
import sys
import torch
import torch.optim as optim
from torch.autograd import Variable
from options import parse_options
# from util import SharedLogDirichletInitializer
import random
from terpret_problem import TerpretProblem,TerpretProblem_ConcreteDistribution,TerpretProblem_STE,TerpretProblem_Bop,TerpretProblem_NR,TerpretProblem_NR_New
from terpret_problem import Bop, CustomizedAdam, NewAdam
from terpret_problem import TerpretProblem_binary
from terpret_problem import NewOp
if __name__ == "__main__":
opts = parse_options()
manualSeed = 0
manualSeed = random.randint(1, 10000) # use if you want new results
print("Random Seed: ", manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)
if opts.type == 0:
''' type 0: continuous surrogate '''
tp = TerpretProblem(opts)
# optimizer = optim.Adam(tp.parameters(), lr=0.001)
optimizer = optim.Adam(tp.parameters(),lr=0.001)
elif opts.type == 1:
''' type 1: using a local reparameterization of gumble-softmax trick '''
opts.M=1
tp = TerpretProblem_ConcreteDistribution(opts)
optimizer = optim.Adam(tp.parameters(), lr=0.001)
elif opts.type == 2:
''' type 2: using Straight-Through Estimator (STE) using the standard Adam'''
tp = TerpretProblem_STE(opts)
# optimizer = optim.SGD(tp.parameters(),lr=0.001)
optimizer = optim.Adam(tp.parameters(),lr=0.001, betas=(0.01,0.999))
# optimizer = optim.Adam(tp.parameters(),lr=0.001)
elif opts.type == 21:
''' type 21: using Straight-Through Estimator (STE) using the customized Adam'''
tp = TerpretProblem_STE(opts)
# optimizer = CustomizedAdam(tp.parameters(),lr=0.001, betas=(0.0001,0.99999))
# optimizer = CustomizedAdam(tp.parameters(),lr=0.001)
# optimizer = NewAdam(tp.parameters(),lr=0.001,betas=(0.9,0.1))
# optimizer = NewAdam(tp.parameters(),lr=0.001, betas=(0.01,0.999))
# optimizer = NewAdam(tp.parameters(),lr=0.001, betas=(0.001,0.999))
optimizer = NewAdam(tp.parameters(),lr=0.001, betas=(0.99999,0.999))
elif opts.type == 3:
''' type 3: using Binary Optimizer (Bop) '''
tp = TerpretProblem_Bop(opts)
# optimizer = Bop(binary_params=tp.parameters(), ar=0.00001, threshold=0.000001, continuous_optimizer=None)
optimizer = Bop(binary_params=tp.binary_parameters(), ar=0.00001, threshold=0.1, continuous_optimizer=None)
elif opts.type == 4:
''' type 4: using Binary Optimizer (Bop) with adaptive noise '''
tp = TerpretProblem_Bop(opts,use_adaptive_noise=True)
# optimizer = Bop(binary_params=tp.parameters(), ar=0.00001, threshold=0.000001, continuous_optimizer=None)
continuous_optimizer = optim.SGD(tp.continuous_parameters(), lr=0.001)
optimizer = Bop(binary_params=tp.binary_parameters(), ar=0.00001, threshold=0.1, continuous_optimizer=continuous_optimizer)
elif opts.type == 5:
''' type 5: Neural reparameterization'''
tp = TerpretProblem_NR(opts)
# optimizer = Bop(binary_params=tp.parameters(), ar=0.00001, threshold=0.000001, continuous_optimizer=None)
# optimizer = optim.SGD(tp.learnable_parameters(), lr=0.001)
optimizer = optim.SGD(tp.learnable_parameters(), lr=0.001)
elif opts.type == 6:
''' type 6: Neural reparameterization (individual)'''
tp = TerpretProblem_NR_New(opts)
# optimizer = Bop(binary_params=tp.parameters(), ar=0.00001, threshold=0.000001, continuous_optimizer=None)
# optimizer = optim.SGD(tp.learnable_parameters(), lr=0.001)
optimizer = optim.SGD(tp.learnable_parameters(), lr=0.001)
elif opts.type == 7:
''' type 6: new approach, size-1 local proposal'''
tp = TerpretProblem_binary(opts)
optimizer = NewOp(tp.binary_parameters())
else:
print("not defined type")
quit()
x = torch.tensor([[1.0,0]],dtype=torch.float,requires_grad=False)
for epoch in range(opts.n_epochs):
tp.zero_grad()
output = tp(x)
loss = output
loss.backward()
# print(tp.ms.grad)
# if type==2:
# clip_value=0.000000000001
# torch.nn.utils.clip_grad_norm_(tp.parameters(), clip_value)
optimizer.step()
# if type==2:
# tp.parameter.data.clamp_(0.499,0.501)
# tp.parameter.data.clamp_(0.0,1.0)
# sys.stdout.write( str(tp.ms.data) )
mus = tp.return_0_prob(x).detach().numpy()
# sys.stdout.write(str(mus))
# print(mus.transpose())
# print(sum(mus.transpose()[0]),sum(mus.transpose()[1]))
# quit()
for i in range(opts.v):
if mus[i,0] == 1:
sys.stdout.write("\u2588" )
elif mus[i,0] == 0:
# sys.stdout.write("\u25A1" )
sys.stdout.write(" " )
elif mus[i,0] <= 0.25:
sys.stdout.write("\u2581" )
elif mus[i,0] < 0.5:
sys.stdout.write("\u2583" )
elif mus[i,0] == 0.5 :
sys.stdout.write("\u2584" )
elif mus[i,0] < 0.75:
sys.stdout.write("\u2585" )
elif mus[i,0] < 1:
sys.stdout.write("\u2587" )
else:
sys.stdout.write("\u25CC" )
if opts.type in [2,21]:
sys.stdout.write("||||")
mus = tp.mus_latent.detach().numpy()
for i in range(opts.v):
if mus[i,0] == 1:
sys.stdout.write("\u2588" )
elif mus[i,0] == 0:
# sys.stdout.write("\u25A1" )
sys.stdout.write(" " )
elif mus[i,0] <= 0.25:
sys.stdout.write("\u2581" )
elif mus[i,0] < 0.5:
sys.stdout.write("\u2583" )
elif mus[i,0] == 0.5 :
sys.stdout.write("\u2584" )
elif mus[i,0] < 0.75:
sys.stdout.write("\u2585" )
elif mus[i,0] < 1:
sys.stdout.write("\u2587" )
else:
sys.stdout.write("\u25CC" )
sys.stdout.write(" epoch: [%d], loss : %.8f " % (epoch, loss.data))
sys.stdout.write("\n")