-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.py
235 lines (195 loc) · 8.14 KB
/
Utilities.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
import csv
def global_variables(af_array, lr_array, dr_array):
"""
Initalize global variables
"""
global activation_function_array
global learning_rate_array
global dropout_rate_array
activation_function_array = af_array
learning_rate_array = lr_array
dropout_rate_array = dr_array
def csv_log(file_name, counter, max_acc):
"""
CSV log to keep track of geneartion and best accuracy
"""
temp = open(file_name)
with open(file_name, mode="a+")as file:
writter = csv.writer(file, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
"""
def global_variables(af_array, lr_array, dr_array):
global activation_function_array
global learning_rate_array
global dropout_rate_array
activation_function_array = af_array
learning_rate_array = lr_array
dropout_rate_array = dr_array
"""
#temp.close()
def int_to_bit(i):
"""
Convert and int to a bit
return
s String of binary 1/0 values
"""
if i == 0:
return "0"
s = ''
while i:
if i & 1 == 1:
s = "1" + s
else:
s = "0" + s
i //= 2
return s
def max_bi(binary):
"""
calculates the max unsigned binary value
returns
s string of all binary 1 for the length of the original binary value
"""
s = ''
for i in range(binary):
s = '1' + s
return s
def ANN_bit(num, max_num_nodes):
"""
Convert the user int values in to binary string denoting the ANN model as a chromosome
Segmentation of binary chromosome
Parameters number of bits
used 1
num_hidden_layers num
num_nodes_per_layer max_num_nodes >> List num size
learning_rate len(learning_rate)
activationFunction_per_layer len(activation_function)
dropout_rate len(dropout_rate)
Return
Parameter String of binary 1/0
"""
used = '1' #The ANN layer will ALWAYS be used!
#str(random.randint(0,1))
#print(used)
num_hidden_layers = int_to_bit(num)
#print(num_hidden_layers)
num_nodes_per_layer = ''
for i in range(num):
num_nodes_per_layer += int_to_bit(max_num_nodes)
#print(len(int_to_bit(max_num_nodes)))
#print(num_nodes_per_layer)
learning_rate = int_to_bit(len(learning_rate_array)-1)
#print(learning_rate)
activationFunction_per_layer = ''
for i in range(num):
activationFunction_per_layer += int_to_bit(len(activation_function_array)-1)
#print(activationFunction_per_layer)
dropout_rate = int_to_bit(len(dropout_rate_array)-1)
#print(dropout_rate)
#exit()
parameter = used+num_hidden_layers+num_nodes_per_layer+learning_rate+activationFunction_per_layer+dropout_rate
#print(parameter)
return parameter
def ANN_bit_to_model(bit, max_num_hidden_layers, max_num_nodes, output_layer):
"""
Convert binary chromosome into invalues for keras ANN model parametners
Segmentation of binary chromosome
Parameters starting Bit point Ending Bit Point
used 0 0
num_hidden_layers 1 max_num_hidden_layers
num_nodes_per_layer max_num_hidden_layers + 1 max_num_nodes * max_num_hidden_layers
learning_rate max_num_nodes * max_num_hidden_layers + 1 len(learning_rate) * max_num_nodes * max_num_hidden_layers
activationFunction_per_layer len(learning_rate) * max_num_nodes * max_num_hidden_layers + 1 len(activation_function) * len(learning_rate) * max_num_nodes * max_num_hidden_layers
dropout_rate len(activation_function) * len(learning_rate) * max_num_nodes * max_num_hidden_layers + 1 len(dropout_rate) * len(activation_function) * len(learning_rate) * max_num_nodes * max_num_hidden_layers
Return
num_hidden_layers int Number of hidden layers for the model
num_nodes_per_layer int array Number of nodes per each layer
learning_rate float learning rate for model
activation_function_per_layer str array Activation function per each layer
dropout_rate float Drop out rate
used int const For futrue use cases to add CNN
"""
used = 1
#print(used)
#bits 1 > max_num_nodes bit length
bit_hidden_layers = ''
bit_length = len(int_to_bit(max_num_hidden_layers))+1
i = 1
while i < bit_length:
bit_hidden_layers += str(bit[i])
i += 1
num_hidden_layers = int(bit_hidden_layers, 2)
if num_hidden_layers == 0:
num_hidden_layers = 1
#print(num_hidden_layers)
num_nodes_per_layer = []
i = 0
for i in range(max_num_hidden_layers):
j = 1
bit_num_nodes = ''
while j < (len(int_to_bit(max_num_nodes))+1):
bit_num_nodes += str(bit[bit_length])
bit_length += 1
j+=1
num_nodes_per_layer.append(int(bit_num_nodes, 2))
#print("TEST")
for i in range(len(num_nodes_per_layer)):
#print(num_nodes_per_layer)
if num_nodes_per_layer[i] == 0:
#print(num_nodes_per_layer[i])
num_nodes_per_layer[i] = 1
if num_hidden_layers == 1:
num_nodes_per_layer[0] = output_layer
else:
num_nodes_per_layer[num_hidden_layers-1] = output_layer
#print(num_hidden_layers)
#print(num_nodes_per_layer)
#bit_length = max_num_hidden_layers * (len(int_to_bit(max_num_nodes))+1)
prvious_math = bit_length
#print(len(int_to_bit(len(learning_rate_array)-1)))
bit_learning_rate = ''
i = 0
for i in range(len(int_to_bit(len(learning_rate_array)-1))):
bit_learning_rate += str(bit[bit_length])
bit_length += 1
learning_rate = int(bit_learning_rate, 2)
i = 0
for i in range(len(learning_rate_array)):
if learning_rate == i:
learning_rate = learning_rate_array[i]
#print(learning_rate)
#bit_length = prvious_math + len(int_to_bit(len(activation_function_array))) + 2
prvious_math = prvious_math + (len(int_to_bit(len(activation_function_array)))*max_num_hidden_layers)
i = 0
activation_function_int = []
for i in range(max_num_hidden_layers):
bit_activation_function = ''
j = 0
while j < len(int_to_bit(len(activation_function_array)-1)):
bit_activation_function += str(bit[bit_length])
bit_length += 1
j+=1
#print(bit_activation_function)
activation_function_int.append(int(bit_activation_function, 2))
i = 0
activation_function_per_layer = []
for i in range(max_num_hidden_layers):
j = 0
for j in range(len(activation_function_array)):
if activation_function_int[i] == j:
activation_function_per_layer.append(activation_function_array[j])
#print(activation_function_per_layer)
i = 0
bit_dropout_rate = ''
#bit_length = prvious_math + len(int_to_bit(len(dropout_rate_array)))
prvious_math = bit_length
#print(bit_length)
for i in range(len(int_to_bit(len(dropout_rate_array)))-1):
bit_dropout_rate += str(bit[bit_length])
bit_length += 1
dropout_rate = int(bit_dropout_rate, 2)
i = 0
for i in range(len(dropout_rate_array)):
if dropout_rate == i:
dropout_rate = dropout_rate_array[i]
#print(dropout_rate)
#ANN_model = networks.ANN(num_hidden_layers, num_nodes_per_layer, learning_rate,activation_function_per_layer, dropout_rate, used=used )
return num_hidden_layers, num_nodes_per_layer, learning_rate,activation_function_per_layer, dropout_rate, used