pyerualjetwork 4.0.5__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- pyerualjetwork/__init__.py +71 -0
- pyerualjetwork/activation_functions.py +367 -0
- pyerualjetwork/activation_functions_cuda.py +363 -0
- pyerualjetwork/data_operations.py +439 -0
- pyerualjetwork/data_operations_cuda.py +463 -0
- pyerualjetwork/help.py +16 -0
- pyerualjetwork/loss_functions.py +21 -0
- pyerualjetwork/loss_functions_cuda.py +21 -0
- pyerualjetwork/metrics.py +190 -0
- pyerualjetwork/metrics_cuda.py +165 -0
- pyerualjetwork/model_operations.py +408 -0
- pyerualjetwork/model_operations_cuda.py +414 -0
- pyerualjetwork/plan.py +681 -0
- pyerualjetwork/plan_cuda.py +677 -0
- pyerualjetwork/planeat.py +734 -0
- pyerualjetwork/planeat_cuda.py +736 -0
- pyerualjetwork/ui.py +22 -0
- pyerualjetwork/visualizations.py +799 -0
- pyerualjetwork/visualizations_cuda.py +799 -0
- pyerualjetwork-4.0.5.dist-info/METADATA +95 -0
- pyerualjetwork-4.0.5.dist-info/RECORD +23 -0
- pyerualjetwork-4.0.5.dist-info/WHEEL +5 -0
- pyerualjetwork-4.0.5.dist-info/top_level.txt +1 -0
@@ -0,0 +1,408 @@
|
|
1
|
+
import numpy as np
|
2
|
+
from colorama import Fore, Style
|
3
|
+
import sys
|
4
|
+
from datetime import datetime
|
5
|
+
import pickle
|
6
|
+
from scipy import io
|
7
|
+
import scipy.io as sio
|
8
|
+
import pandas as pd
|
9
|
+
|
10
|
+
|
11
|
+
def save_model(model_name,
|
12
|
+
W,
|
13
|
+
scaler_params=None,
|
14
|
+
model_type='PLAN',
|
15
|
+
test_acc=None,
|
16
|
+
model_path='',
|
17
|
+
activation_potentiation=['linear'],
|
18
|
+
weights_type='npy',
|
19
|
+
weights_format='raw',
|
20
|
+
show_architecture=None,
|
21
|
+
show_info=True
|
22
|
+
):
|
23
|
+
|
24
|
+
"""
|
25
|
+
Function to save a potentiation learning artificial neural network model.
|
26
|
+
|
27
|
+
Arguments:
|
28
|
+
|
29
|
+
model_name (str): Name of the model.
|
30
|
+
|
31
|
+
model_type (str): Type of the model. default: 'PLAN'
|
32
|
+
|
33
|
+
test_acc (float): Test accuracy of the model. default: None
|
34
|
+
|
35
|
+
weights_type (str): Type of weights to save (options: 'txt', 'pkl', 'npy', 'mat'). default: 'npy'
|
36
|
+
|
37
|
+
WeightFormat (str): Format of the weights (options: 'f', 'raw'). default: 'raw'
|
38
|
+
|
39
|
+
model_path (str): Path where the model will be saved. For example: C:/Users/beydili/Desktop/denemePLAN/ default: ''
|
40
|
+
|
41
|
+
scaler_params (list[num, num]): standard scaler params list: mean,std. If not used standard scaler then be: None.
|
42
|
+
|
43
|
+
W: Weights of the model.
|
44
|
+
|
45
|
+
activation_potentiation (list): For deeper PLAN networks, activation function parameters. For more information please run this code: plan.activations_list() default: ['linear']
|
46
|
+
|
47
|
+
show_architecture (str): It draws model architecture. Takes 2 value='basic' or 'detailed'. Default: None(not drawing)
|
48
|
+
|
49
|
+
show_info (bool): Prints model details into console. default: True
|
50
|
+
|
51
|
+
Returns:
|
52
|
+
No return.
|
53
|
+
"""
|
54
|
+
|
55
|
+
from .visualizations import draw_model_architecture
|
56
|
+
|
57
|
+
class_count = W.shape[0]
|
58
|
+
|
59
|
+
if test_acc != None:
|
60
|
+
test_acc= float(test_acc)
|
61
|
+
|
62
|
+
if weights_type != 'txt' and weights_type != 'npy' and weights_type != 'mat' and weights_type != 'pkl':
|
63
|
+
print(Fore.RED + "ERROR110: Save Weight type (File Extension) Type must be 'txt' or 'npy' or 'mat' or 'pkl' from: save_model" + Style.RESET_ALL)
|
64
|
+
sys.exit()
|
65
|
+
|
66
|
+
if weights_format != 'd' and weights_format != 'f' and weights_format != 'raw':
|
67
|
+
print(Fore.RED + "ERROR111: Weight Format Type must be 'd' or 'f' or 'raw' from: save_model" + Style.RESET_ALL)
|
68
|
+
sys.exit()
|
69
|
+
|
70
|
+
NeuronCount = 0
|
71
|
+
SynapseCount = 0
|
72
|
+
|
73
|
+
|
74
|
+
try:
|
75
|
+
NeuronCount += np.shape(W)[0] + np.shape(W)[1]
|
76
|
+
SynapseCount += np.shape(W)[0] * np.shape(W)[1]
|
77
|
+
except:
|
78
|
+
|
79
|
+
print(Fore.RED + "ERROR: Weight matrices has a problem from: save_model" + Style.RESET_ALL)
|
80
|
+
sys.exit()
|
81
|
+
|
82
|
+
if scaler_params != None:
|
83
|
+
|
84
|
+
if len(scaler_params) > len(activation_potentiation):
|
85
|
+
|
86
|
+
activation_potentiation += ['']
|
87
|
+
|
88
|
+
elif len(activation_potentiation) > len(scaler_params):
|
89
|
+
|
90
|
+
for i in range(len(activation_potentiation) - len(scaler_params)):
|
91
|
+
|
92
|
+
scaler_params.append(' ')
|
93
|
+
|
94
|
+
data = {'MODEL NAME': model_name,
|
95
|
+
'MODEL TYPE': model_type,
|
96
|
+
'CLASS COUNT': class_count,
|
97
|
+
'NEURON COUNT': NeuronCount,
|
98
|
+
'SYNAPSE COUNT': SynapseCount,
|
99
|
+
'TEST ACCURACY': test_acc,
|
100
|
+
'SAVE DATE': datetime.now(),
|
101
|
+
'WEIGHTS TYPE': weights_type,
|
102
|
+
'WEIGHTS FORMAT': weights_format,
|
103
|
+
'MODEL PATH': model_path,
|
104
|
+
'STANDARD SCALER': scaler_params,
|
105
|
+
'ACTIVATION POTENTIATION': activation_potentiation
|
106
|
+
}
|
107
|
+
|
108
|
+
df = pd.DataFrame(data)
|
109
|
+
df.to_pickle(model_path + model_name + '.pkl')
|
110
|
+
|
111
|
+
|
112
|
+
try:
|
113
|
+
|
114
|
+
if weights_type == 'txt' and weights_format == 'f':
|
115
|
+
|
116
|
+
np.savetxt(model_path + model_name + '_weights.txt', W, fmt='%f')
|
117
|
+
|
118
|
+
if weights_type == 'txt' and weights_format == 'raw':
|
119
|
+
|
120
|
+
np.savetxt(model_path + model_name + '_weights.txt', W)
|
121
|
+
|
122
|
+
###
|
123
|
+
|
124
|
+
|
125
|
+
if weights_type == 'pkl' and weights_format == 'f':
|
126
|
+
|
127
|
+
with open(model_path + model_name + '_weights.pkl', 'wb') as f:
|
128
|
+
pickle.dump(W.astype(float), f)
|
129
|
+
|
130
|
+
if weights_type == 'pkl' and weights_format =='raw':
|
131
|
+
|
132
|
+
with open(model_path + model_name + '_weights.pkl', 'wb') as f:
|
133
|
+
pickle.dump(W, f)
|
134
|
+
|
135
|
+
###
|
136
|
+
|
137
|
+
if weights_type == 'npy' and weights_format == 'f':
|
138
|
+
|
139
|
+
np.save(model_path + model_name + '_weights.npy', W, W.astype(float))
|
140
|
+
|
141
|
+
if weights_type == 'npy' and weights_format == 'raw':
|
142
|
+
|
143
|
+
np.save(model_path + model_name + '_weights.npy', W)
|
144
|
+
|
145
|
+
###
|
146
|
+
|
147
|
+
if weights_type == 'mat' and weights_format == 'f':
|
148
|
+
|
149
|
+
w = {'w': W.astype(float)}
|
150
|
+
io.savemat(model_path + model_name + '_weights.mat', w)
|
151
|
+
|
152
|
+
if weights_type == 'mat' and weights_format == 'raw':
|
153
|
+
|
154
|
+
w = {'w': W}
|
155
|
+
io.savemat(model_path + model_name + '_weights.mat', w)
|
156
|
+
|
157
|
+
except:
|
158
|
+
|
159
|
+
print(Fore.RED + "ERROR: Model Weights not saved. Check the Weight parameters. SaveFilePath expl: 'C:/Users/hasancanbeydili/Desktop/denemePLAN/' from: save_model" + Style.RESET_ALL)
|
160
|
+
sys.exit()
|
161
|
+
|
162
|
+
if show_info:
|
163
|
+
print(df)
|
164
|
+
|
165
|
+
message = (
|
166
|
+
Fore.GREEN + "Model Saved Successfully\n" +
|
167
|
+
Fore.MAGENTA + "Don't forget, if you want to load model: model log file and weight files must be in the same directory." +
|
168
|
+
Style.RESET_ALL
|
169
|
+
)
|
170
|
+
|
171
|
+
print(message)
|
172
|
+
|
173
|
+
if show_architecture is not None:
|
174
|
+
draw_model_architecture(model_name=model_name, model_path=model_path, style=show_architecture)
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
def load_model(model_name,
|
179
|
+
model_path,
|
180
|
+
):
|
181
|
+
"""
|
182
|
+
Function to load a potentiation learning model.
|
183
|
+
|
184
|
+
Arguments:
|
185
|
+
|
186
|
+
model_name (str): Name of the model.
|
187
|
+
|
188
|
+
model_path (str): Path where the model is saved.
|
189
|
+
|
190
|
+
Returns:
|
191
|
+
lists: W(list[num]), activation_potentiation, DataFrame of the model
|
192
|
+
"""
|
193
|
+
|
194
|
+
try:
|
195
|
+
|
196
|
+
df = pd.read_pickle(model_path + model_name + '.pkl')
|
197
|
+
|
198
|
+
except:
|
199
|
+
|
200
|
+
print(Fore.RED + "ERROR: Model Path error. acceptable form: 'C:/Users/hasancanbeydili/Desktop/denemePLAN/' from: load_model" + Style.RESET_ALL)
|
201
|
+
|
202
|
+
sys.exit()
|
203
|
+
|
204
|
+
activation_potentiation = list(df['ACTIVATION POTENTIATION'])
|
205
|
+
activation_potentiation = [x for x in activation_potentiation if not (isinstance(x, float) and np.isnan(x))]
|
206
|
+
activation_potentiation = [item for item in activation_potentiation if item != '']
|
207
|
+
|
208
|
+
scaler_params = df['STANDARD SCALER'].tolist()
|
209
|
+
|
210
|
+
try:
|
211
|
+
if scaler_params[0] == None:
|
212
|
+
scaler_params = scaler_params[0]
|
213
|
+
|
214
|
+
except:
|
215
|
+
scaler_params = [item for item in scaler_params if isinstance(item, np.ndarray)]
|
216
|
+
|
217
|
+
|
218
|
+
model_name = str(df['MODEL NAME'].iloc[0])
|
219
|
+
WeightType = str(df['WEIGHTS TYPE'].iloc[0])
|
220
|
+
|
221
|
+
if WeightType == 'txt':
|
222
|
+
W = np.loadtxt(model_path + model_name + '_weights.txt')
|
223
|
+
elif WeightType == 'npy':
|
224
|
+
W = np.load(model_path + model_name + '_weights.npy')
|
225
|
+
elif WeightType == 'mat':
|
226
|
+
W = sio.loadmat(model_path + model_name + '_weights.mat')
|
227
|
+
elif WeightType == 'pkl':
|
228
|
+
with open(model_path + model_name + '_weights.pkl', 'rb') as f:
|
229
|
+
W = pickle.load(f)
|
230
|
+
else:
|
231
|
+
|
232
|
+
raise ValueError(
|
233
|
+
Fore.RED + "Incorrect weight type value. Value must be 'txt', 'npy', 'pkl' or 'mat' from: load_model." + Style.RESET_ALL)
|
234
|
+
|
235
|
+
if WeightType == 'mat':
|
236
|
+
W = W['w']
|
237
|
+
|
238
|
+
return W, None, None, activation_potentiation, scaler_params
|
239
|
+
|
240
|
+
|
241
|
+
def predict_model_ssd(Input, model_name, model_path='', dtype=np.float32):
|
242
|
+
|
243
|
+
"""
|
244
|
+
Function to make a prediction using a potentiation learning artificial neural network (PLAN).
|
245
|
+
from storage
|
246
|
+
|
247
|
+
Arguments:
|
248
|
+
|
249
|
+
Input (list or ndarray): Input data for the model (single vector or single matrix).
|
250
|
+
|
251
|
+
model_name (str): Name of the model.
|
252
|
+
|
253
|
+
model_path (str): Path of the model. Default: ''
|
254
|
+
|
255
|
+
dtype (numpy.dtype): Data type for the arrays. np.float32 by default. Example: np.float64 or np.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!]
|
256
|
+
|
257
|
+
Returns:
|
258
|
+
ndarray: Output from the model.
|
259
|
+
"""
|
260
|
+
|
261
|
+
from .plan import feed_forward
|
262
|
+
from .data_operations import standard_scaler
|
263
|
+
|
264
|
+
model = load_model(model_name, model_path)
|
265
|
+
|
266
|
+
activation_potentiation = model[get_act_pot()]
|
267
|
+
scaler_params = model[get_scaler()]
|
268
|
+
W = model[get_weights()]
|
269
|
+
|
270
|
+
Input = standard_scaler(None, Input, scaler_params, dtype=dtype)
|
271
|
+
|
272
|
+
neural_layer = Input
|
273
|
+
neural_layer = np.array(neural_layer, copy=False)
|
274
|
+
neural_layer = neural_layer.ravel()
|
275
|
+
|
276
|
+
try:
|
277
|
+
neural_layer = feed_forward(neural_layer, np.copy(W.astype(dtype, copy=False)), is_training=False, Class='?', activation_potentiation=activation_potentiation)
|
278
|
+
return neural_layer
|
279
|
+
except:
|
280
|
+
print(Fore.RED + "ERROR: Unexpected Output or wrong model parameters from: predict_model_ssd." + Style.RESET_ALL)
|
281
|
+
sys.exit()
|
282
|
+
|
283
|
+
|
284
|
+
def reverse_predict_model_ssd(output, model_name, model_path='', dtype=np.float32):
|
285
|
+
|
286
|
+
"""
|
287
|
+
reverse prediction function from storage
|
288
|
+
Arguments:
|
289
|
+
|
290
|
+
output (list or ndarray): output layer for the model (single probability vector, output layer of trained model).
|
291
|
+
|
292
|
+
model_name (str): Name of the model.
|
293
|
+
|
294
|
+
model_path (str): Path of the model. Default: ''
|
295
|
+
|
296
|
+
dtype (numpy.dtype): Data type for the arrays. np.float32 by default. Example: np.float64 or np.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!]
|
297
|
+
|
298
|
+
Returns:
|
299
|
+
ndarray: Input from the model.
|
300
|
+
"""
|
301
|
+
|
302
|
+
model = load_model(model_name, model_path)
|
303
|
+
|
304
|
+
W = model[get_weights()]
|
305
|
+
|
306
|
+
try:
|
307
|
+
Input = np.dot(output.astype(dtype, copy=False), np.copy(W.astype(dtype, copy=False)))
|
308
|
+
return Input
|
309
|
+
except:
|
310
|
+
print(Fore.RED + "ERROR: Unexpected Output or wrong model parameters from: reverse_predict_model_ssd." + Style.RESET_ALL)
|
311
|
+
sys.exit()
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
def predict_model_ram(Input, W, scaler_params=None, activation_potentiation=['linear'], dtype=np.float32):
|
316
|
+
|
317
|
+
"""
|
318
|
+
Function to make a prediction using a potentiation learning artificial neural network (PLAN).
|
319
|
+
from memory.
|
320
|
+
|
321
|
+
Arguments:
|
322
|
+
|
323
|
+
Input (list or ndarray): Input data for the model (single vector or single matrix).
|
324
|
+
|
325
|
+
W (list of ndarrays): Weights of the model.
|
326
|
+
|
327
|
+
scaler_params (list): standard scaler params list: mean,std. (optional) Default: None.
|
328
|
+
|
329
|
+
activation_potentiation (list): ac list for deep PLAN. default: [None] ('linear') (optional)
|
330
|
+
|
331
|
+
dtype (numpy.dtype): Data type for the arrays. np.float32 by default. Example: np.float64 or np.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!]
|
332
|
+
|
333
|
+
Returns:
|
334
|
+
ndarray: Output from the model.
|
335
|
+
"""
|
336
|
+
|
337
|
+
from .data_operations import standard_scaler
|
338
|
+
from .plan import feed_forward
|
339
|
+
|
340
|
+
Input = standard_scaler(None, Input, scaler_params, dtype=dtype)
|
341
|
+
|
342
|
+
try:
|
343
|
+
|
344
|
+
neural_layer = Input
|
345
|
+
neural_layer = np.array(neural_layer, copy=False)
|
346
|
+
neural_layer = neural_layer.ravel()
|
347
|
+
|
348
|
+
neural_layer = feed_forward(neural_layer, np.copy(W.astype(dtype, copy=False)), is_training=False, Class='?', activation_potentiation=activation_potentiation)
|
349
|
+
|
350
|
+
return neural_layer
|
351
|
+
|
352
|
+
except:
|
353
|
+
print(Fore.RED + "ERROR: Unexpected input or wrong model parameters from: predict_model_ram." + Style.RESET_ALL)
|
354
|
+
sys.exit()
|
355
|
+
|
356
|
+
def reverse_predict_model_ram(output, W, dtype=np.float32):
|
357
|
+
|
358
|
+
"""
|
359
|
+
reverse prediction function from memory
|
360
|
+
|
361
|
+
Arguments:
|
362
|
+
|
363
|
+
output (list or ndarray): output layer for the model (single probability vector, output layer of trained model).
|
364
|
+
|
365
|
+
W (list of ndarrays): Weights of the model.
|
366
|
+
|
367
|
+
dtype (numpy.dtype): Data type for the arrays. np.float32 by default. Example: np.float64 or np.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!]
|
368
|
+
|
369
|
+
Returns:
|
370
|
+
ndarray: Input from the model.
|
371
|
+
"""
|
372
|
+
|
373
|
+
try:
|
374
|
+
Input = np.dot(output.astype(dtype, copy=False), np.copy(W.astype(dtype, copy=False)))
|
375
|
+
return Input
|
376
|
+
|
377
|
+
except:
|
378
|
+
print(Fore.RED + "ERROR: Unexpected Output or wrong model parameters from: reverse_predict_model_ram." + Style.RESET_ALL)
|
379
|
+
sys.exit()
|
380
|
+
|
381
|
+
|
382
|
+
def get_weights():
|
383
|
+
|
384
|
+
return 0
|
385
|
+
|
386
|
+
|
387
|
+
def get_preds():
|
388
|
+
|
389
|
+
return 1
|
390
|
+
|
391
|
+
|
392
|
+
def get_acc():
|
393
|
+
|
394
|
+
return 2
|
395
|
+
|
396
|
+
|
397
|
+
def get_act_pot():
|
398
|
+
|
399
|
+
return 3
|
400
|
+
|
401
|
+
|
402
|
+
def get_scaler():
|
403
|
+
|
404
|
+
return 4
|
405
|
+
|
406
|
+
def get_preds_softmax():
|
407
|
+
|
408
|
+
return 5
|