forked from atulsinha007/Domain-adaptation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
334 lines (274 loc) · 10.1 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import array
import random
import time
import numpy
from math import sqrt
import cluster
from deap import algorithms
from deap import base
from deap import benchmarks
from deap.benchmarks.tools import diversity, convergence
from deap import creator
from deap import tools
import os
from population import *
import network
from network import Neterr
from chromosome import Chromosome, crossover
import traceback
import sys
n_hidden = 40
indim = 32
outdim = 5
#
network_obj_src = Neterr(indim, outdim, n_hidden, change_to_target=1, rng=random)
# creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0, 0.0, 0.0))
creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0, -1.0))
creator.create("Individual", Chromosome, fitness=creator.FitnessMin)
print("here network object created")
toolbox = base.Toolbox()
def minimize_src(individual):
outputarr = network_obj_src.feedforward_ne(individual, final_activation=network.softmax)
neg_log_likelihood_val = give_neg_log_likelihood(outputarr, network_obj_src.resty)
mean_square_error_val = give_mse(outputarr, network_obj_src.resty)
# anyways not using these as you can see in 'creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0, 0.0, 0.0))'
# return neg_log_likelihood_val, mean_square_error_val, false_positve_rat, false_negative_rat
return (neg_log_likelihood_val, mean_square_error_val, len(individual.conn_arr))
def mycross(ind1, ind2, gen_no):
child1 = crossover(ind1, ind2, gen_no, inputdim=indim, outputdim=outdim)
child2 = crossover(ind1, ind2, gen_no, inputdim=indim, outputdim=outdim)
return child1, child2
def mymutate_src(ind1):
# do_mutation(self, rate_conn_weight, rate_conn_itself, rate_node, inputdim, outputdim, max_hidden_unit, rng)
new_ind = ind1.do_mutation(rate_conn_weight=0.4, rate_conn_itself=0.3, rate_node=0.2, weight_factor=2,
inputdim=indim, outputdim=outdim, max_hidden_unit=n_hidden, rng=random)
return ind1
def initIndividual(ind_class, inputdim, outputdim):
ind = ind_class(inputdim, outputdim)
return ind
old_chromosome = None
toolbox.register("individual", initIndividual, creator.Individual, indim, outdim)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", mycross)
toolbox.register("select", tools.selNSGA2)
bp_rate = 0.05
def main(seed=None, play=0, NGEN=40, MU=4 * 10):
# random.seed(seed)
# MU has to be a multiple of 4. period.
CXPB = 0.9
stats = tools.Statistics(lambda ind: (ind.fitness.values[0], ind.fitness.values[1], ind.fitness.values[2]))
# stats.register("avg", numpy.mean, axis=0)
# stats.register("std", numpy.std, axis=0)
stats.register("min", numpy.min, axis=0)
stats.register("max", numpy.max, axis=0)
logbook = tools.Logbook()
logbook.header = "gen", "evals", "std", "min", "avg", "max"
toolbox.register("evaluate", minimize_src)
time1 = time.time()
toolbox.register("mutate", mymutate_src)
pop_src = toolbox.population(n=MU)
time2 = time.time()
print("After population initialisation", time2 - time1)
print(type(pop_src))
# print("population initialized")
# network_obj = Neterr(indim, outdim, n_hidden, np.random)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in pop_src if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
time3 = time.time()
print("After feedforward", time3 - time2)
# This is just to assign the crowding distance to the individuals
# no actual selection is done
pop_src = toolbox.select(pop_src, len(pop_src))
# print( "first population selected, still outside main loop")
# print(pop)
record = stats.compile(pop_src)
logbook.record(gen=0, evals=len(invalid_ind), **record)
print(logbook.stream)
maxi = 0
stri = ''
flag = 0
# Begin the generational process
# print(pop.__dir__())
time4 = time.time()
for gen in range(1, NGEN):
# Vary the population
if gen == 1:
time6 = time.time()
if gen == NGEN - 1:
time7 = time.time()
print()
print("here in gen no.", gen)
offspring = tools.selTournamentDCD(pop_src, len(pop_src))
offspring = [toolbox.clone(ind) for ind in offspring]
if play:
if play == 1:
pgen = NGEN * 0.1
elif play == 2:
pgen = NGEN * 0.9
if gen == int(pgen):
print("gen:", gen, "doing clustering")
to_bp_lis = cluster.give_cluster_head(offspring, int(MU * bp_rate))
assert (to_bp_lis[0] in offspring)
print("doing bp")
[item.modify_thru_backprop(indim, outdim, network_obj_src.rest_setx, network_obj_src.rest_sety,
epochs=20, learning_rate=0.1, n_par=10) for item in to_bp_lis]
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
if gen == 1:
time8 = time.time()
if gen == NGEN - 1:
time9 = time.time()
dum_ctr = 0
for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
flag = 0
if random.random() <= CXPB:
ind1, ind2 = toolbox.mate(ind1, ind2, gen)
ind1 = creator.Individual(indim, outdim, ind1)
ind2 = creator.Individual(indim, outdim, ind2)
flag = 1
maxi = max(maxi, ind1.node_ctr, ind2.node_ctr)
toolbox.mutate(ind1)
toolbox.mutate(ind2)
offspring[dum_ctr] = ind1
offspring[dum_ctr + 1] = ind2
del offspring[dum_ctr].fitness.values, offspring[dum_ctr + 1].fitness.values
dum_ctr += 2
if gen == 1:
print("1st gen after newpool", time.time() - time8)
if gen == NGEN - 1:
print("last gen after newpool", time.time() - time9)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# Select the next generation population
pop_src = toolbox.select(pop_src + offspring, MU)
record = stats.compile(pop_src)
logbook.record(gen=gen, evals=len(invalid_ind), **record)
anost = logbook.stream
liso = [item.rstrip() for item in anost.split("\t")]
#mse = float(liso[3])
print(anost)
stri += anost + '\n'
print("generation done")
# file_ob.write(str(logbook.stream))
# print(len(pop))
# file_ob.close()
time5 = time.time()
print("Overall time", time5 - time4)
# print(stri)
print(' ------------------------------------src done------------------------------------------- ')
fronts = tools.sortNondominated(pop_src, len(pop_src))
pareto_front = fronts[0]
print(pareto_front)
print("Pareto Front: ")
for i in range(len(pareto_front)):
print(pareto_front[i].fitness.values)
return pareto_front, logbook
def note_this_string(new_st, stringh):
"""flag_ob = open("flag.txt","r+")
ctr = None
st = flag_ob.read()
flag = int(st.rstrip())
while flag ==1:
flag_ob.seek(0)
st = flag_ob.read()
flag = int(st.rstrip())
time.sleep(3)
if flag == 0:
flag = 1
flag_ob.seek(0)
flag_ob.write("1\n")
flag_ob.close()
'/home/robita/forgit/neuro-evolution/05/state/tf/indep_pima/input/model.ckpt.meta'
"""
name = "./ctr_folder/ctr" + stringh + ".txt"
if not os.path.isfile(name):
new_f = open(name, "w+")
new_f.write("0\n")
new_f.close()
ctr_ob = open(name, "r+")
strin = ctr_ob.read().rstrip()
assert (strin is not '')
ctr = int(strin)
ctr_ob.seek(0)
ctr_ob.write(str(ctr + 1) + "\n")
ctr_ob.close()
"""
flag_ob = open("flag.txt","w")
flag_ob.write("0\n")
flag_ob.close()
"""
new_file_ob = open("log_folder/log" + stringh + ".txt", "a+")
new_file_ob.write(str(ctr) + " " + new_st + "\n")
new_file_ob.close()
return ctr
def test_it_without_bp():
pop, stats = main()
stringh = "_without_bp"
fronts = tools.sortNondominated(pop, len(pop))
if len(fronts[0]) < 30:
pareto_front = fronts[0]
else:
pareto_front = random.sample(fronts[0], 30)
print("Pareto Front: ")
for i in range(len(pareto_front)):
print(pareto_front[i].fitness.values)
neter = Neterr(indim, outdim, n_hidden, random)
print("\ntest: test on one with min validation error", neter.test_err(min(pop, key=lambda x: x.fitness.values[1])))
tup = neter.test_on_pareto_patch(pareto_front)
print("\n test: avg on sampled pareto set", tup[0], "least found avg", tup[1])
st = str(neter.test_err(min(pop, key=lambda x: x.fitness.values[1]))) + " " + str(tup[0]) + " " + str(tup[1])
print(note_this_string(st, stringh))
def test_it_with_bp(play=1, NGEN=60, MU=4 * 15, play_with_whole_pareto=0, post_st = ''):
pop, stats = main(play=play, NGEN=NGEN, MU=MU)
stringh = "_with_bp" + str(play) + "_" + str(NGEN)+post_st
fronts = tools.sortNondominated(pop, len(pop))
'''file_ob = open("./log_folder/log_for_graph.txt", "w+")
for item in fronts[0]:
st = str(item.fitness.values[0]) + " " + str(item.fitness.values[1])+"\n"
file_ob.write( st )
file_ob.close()'''
if play_with_whole_pareto or len(fronts[0]) < 30:
pareto_front = fronts[0]
else:
pareto_front = random.sample(fronts[0], 30)
print("Pareto Front: ")
for i in range(len(pareto_front)):
print(pareto_front[i].fitness.values)
print("\ntest: test on one with min validation error",
network_obj_src.test_err(min(pop, key=lambda x: x.fitness.values[1])))
tup = network_obj_src.test_on_pareto_patch_correctone(pareto_front)
print("\n test: avg on sampled pareto set", tup)
st = str(network_obj_src.test_err(min(pop, key=lambda x: x.fitness.values[1]))) + " " + str(tup)
print(note_this_string(st, stringh))
if __name__ == "__main__":
logf = open("log_error_main.txt", "a")
try:
post_st = sys.argv[1]
test_it_with_bp(play=1, NGEN=200, MU=4 * 25, play_with_whole_pareto=1, post_st = post_st)
except Exception as e:
print("Error! Error! Error!")
logf.write('\n\n')
localtime = time.localtime(time.time())
logf.write(str(localtime) + '\n')
traceback.print_exc(file=logf)
logf.write('\n\n')
finally:
logf.close()
# file_ob.write( "test on one with min validation error " + str(neter.test_err(min(pop, key=lambda x: x.fitness.values[1]))))
# print(stats)
'''
import matplotlib.pyplot as plt
import numpy
front = numpy.array([ind.fitness.values for ind in pop])
plt.scatter(front[:,0], front[:,1], c="b")
plt.axis("tight")
plt.show()'''