pyerualjetwork 5.0.3__py3-none-any.whl → 5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyerualjetwork/__init__.py +3 -3
- pyerualjetwork/activation_functions_cpu.py +3 -71
- pyerualjetwork/activation_functions_cuda.py +2 -74
- pyerualjetwork/data_operations_cpu.py +3 -5
- pyerualjetwork/ene_cuda.py +5 -5
- pyerualjetwork/fitness_functions.py +0 -1
- pyerualjetwork/issue_solver.py +1 -1
- pyerualjetwork/model_operations_cpu.py +123 -55
- pyerualjetwork/model_operations_cuda.py +120 -51
- pyerualjetwork/neu_cpu.py +169 -52
- pyerualjetwork/neu_cuda.py +170 -55
- {pyerualjetwork-5.0.3.dist-info → pyerualjetwork-5.2.dist-info}/METADATA +3 -3
- pyerualjetwork-5.2.dist-info/RECORD +26 -0
- pyerualjetwork-5.0.3.dist-info/RECORD +0 -26
- {pyerualjetwork-5.0.3.dist-info → pyerualjetwork-5.2.dist-info}/WHEEL +0 -0
- {pyerualjetwork-5.0.3.dist-info → pyerualjetwork-5.2.dist-info}/top_level.txt +0 -0
@@ -28,10 +28,11 @@ Module functions:
|
|
28
28
|
- get_acc()
|
29
29
|
- get_scaler()
|
30
30
|
- get_model_type()
|
31
|
-
- get_weights_type()
|
32
|
-
- get_weights_format()
|
33
|
-
- get_model_version()
|
34
|
-
- get_model_df()
|
31
|
+
- get_weights_type()
|
32
|
+
- get_weights_format()
|
33
|
+
- get_model_version()
|
34
|
+
- get_model_df()
|
35
|
+
- get_act_pot()
|
35
36
|
|
36
37
|
Examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes
|
37
38
|
|
@@ -61,6 +62,7 @@ def save_model(model_name,
|
|
61
62
|
test_acc=None,
|
62
63
|
model_path='',
|
63
64
|
activations=['linear'],
|
65
|
+
activation_potentiation=[],
|
64
66
|
weights_type='npy',
|
65
67
|
weights_format='raw',
|
66
68
|
show_architecture=False,
|
@@ -74,9 +76,9 @@ def save_model(model_name,
|
|
74
76
|
|
75
77
|
W: Weights of the model.
|
76
78
|
|
77
|
-
model_type: (str): Type of the model. Options: 'PLAN', 'MLP'.
|
79
|
+
model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'.
|
78
80
|
|
79
|
-
scaler_params: (list[num, num]): standard scaler params list: mean,std. If not used standard scaler then be:
|
81
|
+
scaler_params: (list[num, num]): standard scaler params list: mean,std. If not used standard scaler then be: [].
|
80
82
|
|
81
83
|
test_acc: (float): Test accuracy of the model. default: None
|
82
84
|
|
@@ -84,6 +86,8 @@ def save_model(model_name,
|
|
84
86
|
|
85
87
|
activations: (list[str]): For deeper PLAN networks, activation function parameters. Or activation function parameters for MLP layers. For more information please run this code: neu.activations_list() default: ['linear']
|
86
88
|
|
89
|
+
activation_potentiation (list, optional): Extra activation potentiation list (PLAN layers activations) for just PTNN models. Default = None.
|
90
|
+
|
87
91
|
weights_type: (str): Type of weights to save (options: 'txt', 'pkl', 'npy', 'mat'). default: 'npy'
|
88
92
|
|
89
93
|
weights_format: (str): Format of the weights (options: 'f', 'raw'). default: 'raw'
|
@@ -97,16 +101,28 @@ def save_model(model_name,
|
|
97
101
|
"""
|
98
102
|
|
99
103
|
from .visualizations_cpu import draw_model_architecture
|
104
|
+
from .__init__ import __version__
|
100
105
|
|
101
|
-
if model_type != 'PLAN' and model_type != 'MLP':
|
102
|
-
raise ValueError("model_type parameter must be 'PLAN' or '
|
106
|
+
if model_type != 'PLAN' and model_type != 'MLP' and model_type != 'PTNN':
|
107
|
+
raise ValueError("model_type parameter must be 'PLAN', 'MLP' or 'PTNN'.")
|
108
|
+
|
109
|
+
if model_type == 'PTNN' and activation_potentiation == []:
|
110
|
+
raise ValueError('PTNN models need extra activation_potentiation parameter.')
|
103
111
|
|
104
112
|
if isinstance(activations, str):
|
105
113
|
activations = [activations]
|
106
114
|
else:
|
107
115
|
activations = [item if isinstance(item, list) else [item] for item in activations]
|
108
116
|
|
109
|
-
|
117
|
+
activations = activations.copy()
|
118
|
+
|
119
|
+
if model_type == 'PTNN':
|
120
|
+
if isinstance(activation_potentiation, str):
|
121
|
+
activation_potentiation = [activation_potentiation]
|
122
|
+
else:
|
123
|
+
activation_potentiation = [item if isinstance(item, list) else [item] for item in activation_potentiation]
|
124
|
+
|
125
|
+
activation_potentiation = activation_potentiation.copy()
|
110
126
|
|
111
127
|
if test_acc != None:
|
112
128
|
test_acc= float(test_acc)
|
@@ -134,7 +150,7 @@ def save_model(model_name,
|
|
134
150
|
print(Fore.RED + "ERROR: Weight matrices have a problem from: save_model" + Style.RESET_ALL)
|
135
151
|
sys.exit()
|
136
152
|
|
137
|
-
elif model_type == 'MLP':
|
153
|
+
elif model_type == 'MLP' or model_type == 'PTNN':
|
138
154
|
|
139
155
|
class_count = W[-1].shape[0]
|
140
156
|
|
@@ -152,36 +168,42 @@ def save_model(model_name,
|
|
152
168
|
|
153
169
|
SynapseCount.append(' ')
|
154
170
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
if len(activation) == 1 and model_type == 'PLAN':
|
159
|
-
activation = [activation]
|
160
|
-
activation.append('')
|
171
|
+
activations.append('')
|
172
|
+
activations.insert(0, '')
|
161
173
|
|
162
|
-
if len(
|
163
|
-
|
174
|
+
if len(activations) == 1 and model_type == 'PLAN':
|
175
|
+
activations = [activations]
|
176
|
+
activations.append('')
|
177
|
+
|
178
|
+
if model_type == 'PTNN':
|
179
|
+
if len(activations) > len(activation_potentiation):
|
180
|
+
for i in range(len(activations) - len(activation_potentiation)):
|
181
|
+
activation_potentiation.append('')
|
182
|
+
|
183
|
+
if len(activation_potentiation) > len(activations):
|
184
|
+
for i in range(len(activation_potentiation) - len(activations)):
|
185
|
+
activations.append('')
|
186
|
+
|
187
|
+
if len(activations) > len(NeuronCount):
|
188
|
+
for i in range(len(activations) - len(NeuronCount)):
|
164
189
|
NeuronCount.append('')
|
165
190
|
|
166
|
-
if len(
|
167
|
-
for i in range(len(
|
191
|
+
if len(activations) > len(SynapseCount):
|
192
|
+
for i in range(len(activations) - len(SynapseCount)):
|
168
193
|
SynapseCount.append('')
|
169
194
|
|
170
|
-
|
171
195
|
if scaler_params != None:
|
172
196
|
|
173
|
-
if len(scaler_params) > len(
|
197
|
+
if len(scaler_params) > len(activations):
|
174
198
|
|
175
|
-
|
199
|
+
activations += ['']
|
176
200
|
|
177
|
-
elif len(
|
201
|
+
elif len(activations) > len(scaler_params):
|
178
202
|
|
179
|
-
for i in range(len(
|
203
|
+
for i in range(len(activations) - len(scaler_params)):
|
180
204
|
|
181
205
|
scaler_params.append(' ')
|
182
206
|
|
183
|
-
from .__init__ import __version__
|
184
|
-
|
185
207
|
data = {'MODEL NAME': model_name,
|
186
208
|
'MODEL TYPE': model_type,
|
187
209
|
'CLASS COUNT': class_count,
|
@@ -192,9 +214,9 @@ def save_model(model_name,
|
|
192
214
|
'SAVE DATE': datetime.now(),
|
193
215
|
'WEIGHTS TYPE': weights_type,
|
194
216
|
'WEIGHTS FORMAT': weights_format,
|
195
|
-
'MODEL PATH': model_path,
|
196
217
|
'STANDARD SCALER': scaler_params,
|
197
|
-
'ACTIVATION FUNCTIONS':
|
218
|
+
'ACTIVATION FUNCTIONS': activations,
|
219
|
+
'ACTIVATION POTENTIATION': activation_potentiation
|
198
220
|
}
|
199
221
|
|
200
222
|
df = pd.DataFrame(data)
|
@@ -293,13 +315,6 @@ def load_model(model_name,
|
|
293
315
|
|
294
316
|
sys.exit()
|
295
317
|
|
296
|
-
try:
|
297
|
-
activations = list(df['ACTIVATION FUNCTIONS']) # for PyerualJetwork >=5 Versions.
|
298
|
-
except KeyError:
|
299
|
-
activations = list(df['ACTIVATION POTENTIATION']) # for PyerualJetwork <5 Versions.
|
300
|
-
|
301
|
-
activations = [x for x in activations if not (isinstance(x, float) and np.isnan(x))]
|
302
|
-
activations = [item for item in activations if item != '']
|
303
318
|
|
304
319
|
scaler_params = df['STANDARD SCALER'].tolist()
|
305
320
|
|
@@ -317,6 +332,14 @@ def load_model(model_name,
|
|
317
332
|
WeightFormat = str(df['WEIGHTS FORMAT'].iloc[0])
|
318
333
|
test_acc = str(df['TEST ACCURACY'].iloc[0])
|
319
334
|
|
335
|
+
activations = list(df['ACTIVATION FUNCTIONS'])
|
336
|
+
activations = [x for x in activations if not (isinstance(x, float) and np.isnan(x))]
|
337
|
+
activations = [item for item in activations if item != '']
|
338
|
+
|
339
|
+
activation_potentiation = list(df['ACTIVATION POTENTIATION'])
|
340
|
+
activation_potentiation = [x for x in activation_potentiation if not (isinstance(x, float) and np.isnan(x))]
|
341
|
+
activation_potentiation = [item for item in activation_potentiation if item != '']
|
342
|
+
|
320
343
|
device_version = __version__
|
321
344
|
|
322
345
|
try:
|
@@ -331,7 +354,7 @@ def load_model(model_name,
|
|
331
354
|
except:
|
332
355
|
pass # Version check only in >= 5.0.2
|
333
356
|
|
334
|
-
if model_type == 'MLP': allow_pickle = True
|
357
|
+
if model_type == 'MLP' or model_type == 'PTNN': allow_pickle = True
|
335
358
|
else: allow_pickle = False
|
336
359
|
|
337
360
|
if WeightType == 'txt':
|
@@ -351,7 +374,7 @@ def load_model(model_name,
|
|
351
374
|
if WeightType == 'mat':
|
352
375
|
W = W['w']
|
353
376
|
|
354
|
-
return W, None, test_acc, activations, scaler_params, None, model_type, WeightType, WeightFormat, device_version, df
|
377
|
+
return W, None, test_acc, activations, scaler_params, None, model_type, WeightType, WeightFormat, device_version, df, activation_potentiation
|
355
378
|
|
356
379
|
|
357
380
|
|
@@ -374,7 +397,7 @@ def predict_from_storage(Input, model_name, model_path=''):
|
|
374
397
|
|
375
398
|
from .activation_functions_cpu import apply_activation
|
376
399
|
from .data_operations_cpu import standard_scaler
|
377
|
-
|
400
|
+
|
378
401
|
try:
|
379
402
|
|
380
403
|
model = load_model(model_name, model_path)
|
@@ -383,30 +406,48 @@ def predict_from_storage(Input, model_name, model_path=''):
|
|
383
406
|
scaler_params = model[get_scaler()]
|
384
407
|
W = model[get_weights()]
|
385
408
|
model_type = model[get_model_type()]
|
409
|
+
activation_potentiation = model[get_act_pot()]
|
386
410
|
|
387
411
|
if isinstance(activations, str):
|
388
412
|
activations = [activations]
|
389
413
|
elif isinstance(activations, list):
|
390
414
|
activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
|
391
|
-
|
415
|
+
|
392
416
|
Input = standard_scaler(None, Input, scaler_params)
|
393
417
|
|
394
418
|
if model_type == 'MLP':
|
395
|
-
|
396
419
|
layer = Input
|
397
420
|
for i in range(len(W)):
|
398
421
|
if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
|
422
|
+
|
399
423
|
layer = layer @ W[i].T
|
400
|
-
|
401
|
-
return layer
|
402
424
|
|
403
|
-
|
425
|
+
result = layer
|
426
|
+
|
427
|
+
if model_type == 'PLAN':
|
404
428
|
|
405
429
|
Input = apply_activation(Input, activations)
|
406
430
|
result = Input @ W.T
|
431
|
+
|
432
|
+
if model_type == 'PTNN':
|
433
|
+
|
434
|
+
if isinstance(activation_potentiation, str):
|
435
|
+
activation_potentiation = [activation_potentiation]
|
436
|
+
elif isinstance(activation_potentiation, list):
|
437
|
+
activation_potentiation = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activation_potentiation]
|
438
|
+
|
439
|
+
Input = apply_activation(Input, activation_potentiation)
|
440
|
+
layer = Input @ W[0].T
|
441
|
+
|
442
|
+
for i in range(1, len(W)):
|
443
|
+
if i != len(W) - 1: layer = apply_activation(layer, activations[i])
|
444
|
+
|
445
|
+
layer = layer @ W[i].T
|
407
446
|
|
408
|
-
|
447
|
+
result = layer
|
409
448
|
|
449
|
+
return result
|
450
|
+
|
410
451
|
except:
|
411
452
|
print(Fore.RED + "ERROR: Unexpected Output or wrong model parameters from: predict_model_storage." + Style.RESET_ALL)
|
412
453
|
sys.exit()
|
@@ -441,7 +482,7 @@ def reverse_predict_from_storage(output, model_name, model_path=''):
|
|
441
482
|
|
442
483
|
|
443
484
|
|
444
|
-
def predict_from_memory(Input, W, scaler_params=None, activations=['linear'],
|
485
|
+
def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], activation_potentiation=None, model_type='PLAN'):
|
445
486
|
|
446
487
|
"""
|
447
488
|
Function to make a prediction.
|
@@ -456,7 +497,10 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
|
|
456
497
|
|
457
498
|
activations (list[str]): activation list for deep PLAN or activation list for MLP layers. Default: ['linear']
|
458
499
|
|
459
|
-
|
500
|
+
activation_potentiation (list, optional): Extra activation potentiation list (PLAN layers activations) for just PTNN models. Default = None.
|
501
|
+
|
502
|
+
model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'. Default: PLAN
|
503
|
+
|
460
504
|
Returns:
|
461
505
|
ndarray: Output from the model.
|
462
506
|
"""
|
@@ -464,6 +508,8 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
|
|
464
508
|
from .data_operations_cpu import standard_scaler
|
465
509
|
from .activation_functions_cpu import apply_activation
|
466
510
|
|
511
|
+
if model_type != 'PLAN' and model_type != 'MLP' and model_type != 'PTNN': raise ValueError("model_type parameter must be 'PLAN', 'MLP' or 'PTNN'.")
|
512
|
+
|
467
513
|
try:
|
468
514
|
|
469
515
|
Input = standard_scaler(None, Input, scaler_params)
|
@@ -472,23 +518,40 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
|
|
472
518
|
activations = [activations]
|
473
519
|
elif isinstance(activations, list):
|
474
520
|
activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
|
475
|
-
|
476
|
-
if
|
477
|
-
|
521
|
+
|
522
|
+
if model_type == 'MLP':
|
478
523
|
layer = Input
|
479
524
|
for i in range(len(W)):
|
480
525
|
if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
|
526
|
+
|
481
527
|
layer = layer @ W[i].T
|
482
|
-
|
483
|
-
return layer
|
484
528
|
|
485
|
-
|
529
|
+
result = layer
|
530
|
+
|
531
|
+
if model_type == 'PLAN':
|
486
532
|
|
487
533
|
Input = apply_activation(Input, activations)
|
488
534
|
result = Input @ W.T
|
535
|
+
|
536
|
+
if model_type == 'PTNN':
|
537
|
+
|
538
|
+
if isinstance(activation_potentiation, str):
|
539
|
+
activation_potentiation = [activation_potentiation]
|
540
|
+
elif isinstance(activation_potentiation, list):
|
541
|
+
activation_potentiation = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activation_potentiation]
|
542
|
+
|
543
|
+
Input = apply_activation(Input, activation_potentiation)
|
544
|
+
layer = Input @ W[0].T
|
545
|
+
|
546
|
+
for i in range(1, len(W)):
|
547
|
+
if i != len(W) - 1: layer = apply_activation(layer, activations[i])
|
489
548
|
|
490
|
-
|
549
|
+
layer = layer @ W[i].T
|
550
|
+
|
551
|
+
result = layer
|
491
552
|
|
553
|
+
return result
|
554
|
+
|
492
555
|
except:
|
493
556
|
print(Fore.RED + "ERROR: Unexpected input or wrong model parameters from: predict_model_memory." + Style.RESET_ALL)
|
494
557
|
sys.exit()
|
@@ -569,4 +632,9 @@ def get_model_version():
|
|
569
632
|
|
570
633
|
def get_model_df():
|
571
634
|
|
572
|
-
return 10
|
635
|
+
return 10
|
636
|
+
|
637
|
+
|
638
|
+
def get_act_pot():
|
639
|
+
|
640
|
+
return 11
|
@@ -28,10 +28,11 @@ Module functions:
|
|
28
28
|
- get_acc()
|
29
29
|
- get_scaler()
|
30
30
|
- get_model_type()
|
31
|
-
- get_weights_type()
|
32
|
-
- get_weights_format()
|
33
|
-
- get_model_version()
|
34
|
-
- get_model_df()
|
31
|
+
- get_weights_type()
|
32
|
+
- get_weights_format()
|
33
|
+
- get_model_version()
|
34
|
+
- get_model_df()
|
35
|
+
- get_act_pot()
|
35
36
|
|
36
37
|
|
37
38
|
Examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes
|
@@ -63,6 +64,7 @@ def save_model(model_name,
|
|
63
64
|
test_acc=None,
|
64
65
|
model_path='',
|
65
66
|
activations=['linear'],
|
67
|
+
activation_potentiation=[],
|
66
68
|
weights_type='npy',
|
67
69
|
weights_format='raw',
|
68
70
|
show_architecture=False,
|
@@ -76,7 +78,7 @@ def save_model(model_name,
|
|
76
78
|
model_name: (str): Name of the model.
|
77
79
|
W: Weights of the model.
|
78
80
|
|
79
|
-
model_type: (str): Type of the model. Options: 'PLAN', 'MLP'.
|
81
|
+
model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'.
|
80
82
|
|
81
83
|
scaler_params: (list[num, num]): standard scaler params list: mean,std. If not used standard scaler then be: None.
|
82
84
|
|
@@ -88,6 +90,8 @@ def save_model(model_name,
|
|
88
90
|
|
89
91
|
activations: (list[str]): For deeper PLAN networks, activation function parameters. Or activation function parameters for MLP layers. For more information please run this code: plan.activations_list() default: ['linear']
|
90
92
|
|
93
|
+
activation_potentiation (list, optional): Extra activation potentiation list (PLAN layers activations) for just PTNN models. Default = None.
|
94
|
+
|
91
95
|
weights_type: (str): Type of weights to save (options: 'txt', 'pkl', 'npy', 'mat'). default: 'npy'
|
92
96
|
|
93
97
|
weights_format: (str): Format of the weights (options: 'f', 'raw'). default: 'raw'
|
@@ -102,10 +106,14 @@ def save_model(model_name,
|
|
102
106
|
"""
|
103
107
|
|
104
108
|
from .visualizations_cuda import draw_model_architecture
|
109
|
+
from .__init__ import __version__
|
105
110
|
|
106
|
-
if model_type != 'PLAN' and model_type != 'MLP':
|
107
|
-
raise ValueError("model_type parameter must be 'PLAN' or '
|
111
|
+
if model_type != 'PLAN' and model_type != 'MLP' and model_type != 'PTNN':
|
112
|
+
raise ValueError("model_type parameter must be 'PLAN', 'MLP' or 'PTNN'.")
|
108
113
|
|
114
|
+
if model_type == 'PTNN' and activation_potentiation == []:
|
115
|
+
raise ValueError('PTNN models need extra activation_potentiation parameter.')
|
116
|
+
|
109
117
|
if isinstance(W, list):
|
110
118
|
W = W.copy()
|
111
119
|
|
@@ -117,7 +125,15 @@ def save_model(model_name,
|
|
117
125
|
else:
|
118
126
|
activations = [item if isinstance(item, list) else [item] for item in activations]
|
119
127
|
|
120
|
-
|
128
|
+
activations = activations.copy()
|
129
|
+
|
130
|
+
if model_type == 'PTNN':
|
131
|
+
if isinstance(activation_potentiation, str):
|
132
|
+
activation_potentiation = [activation_potentiation]
|
133
|
+
else:
|
134
|
+
activation_potentiation = [item if isinstance(item, list) else [item] for item in activation_potentiation]
|
135
|
+
|
136
|
+
activation_potentiation = activation_potentiation.copy()
|
121
137
|
|
122
138
|
if test_acc is not None:
|
123
139
|
test_acc= float(test_acc)
|
@@ -145,7 +161,7 @@ def save_model(model_name,
|
|
145
161
|
print(Fore.RED + "ERROR: Weight matrices have a problem from: save_model" + Style.RESET_ALL)
|
146
162
|
sys.exit()
|
147
163
|
|
148
|
-
elif model_type == 'MLP':
|
164
|
+
elif model_type == 'MLP' or model_type == 'PTNN':
|
149
165
|
|
150
166
|
for i in range(len(W)):
|
151
167
|
W[i] = W[i].get()
|
@@ -167,37 +183,45 @@ def save_model(model_name,
|
|
167
183
|
|
168
184
|
SynapseCount.append(' ')
|
169
185
|
|
170
|
-
|
171
|
-
|
186
|
+
activations.append('')
|
187
|
+
activations.insert(0, '')
|
172
188
|
|
173
|
-
if len(
|
174
|
-
|
175
|
-
|
189
|
+
if len(activations) == 1 and model_type == 'PLAN':
|
190
|
+
activations = [activations]
|
191
|
+
activations.append('')
|
176
192
|
|
177
|
-
if
|
178
|
-
|
193
|
+
if model_type == 'PTNN':
|
194
|
+
if len(activations) > len(activation_potentiation):
|
195
|
+
for i in range(len(activations) - len(activation_potentiation)):
|
196
|
+
activation_potentiation.append('')
|
197
|
+
|
198
|
+
if len(activation_potentiation) > len(activations):
|
199
|
+
for i in range(len(activation_potentiation) - len(activations)):
|
200
|
+
activations.append('')
|
201
|
+
|
202
|
+
if len(activations) > len(NeuronCount):
|
203
|
+
for i in range(len(activations) - len(NeuronCount)):
|
179
204
|
NeuronCount.append('')
|
180
|
-
|
181
|
-
if len(
|
182
|
-
for i in range(len(
|
205
|
+
|
206
|
+
if len(activations) > len(SynapseCount):
|
207
|
+
for i in range(len(activations) - len(SynapseCount)):
|
183
208
|
SynapseCount.append('')
|
184
209
|
|
185
210
|
if scaler_params != None:
|
186
211
|
|
187
|
-
if len(scaler_params) > len(
|
212
|
+
if len(scaler_params) > len(activations):
|
188
213
|
|
189
|
-
|
214
|
+
activations += ['']
|
190
215
|
|
191
|
-
elif len(
|
216
|
+
elif len(activations) > len(scaler_params):
|
192
217
|
|
193
|
-
for i in range(len(
|
218
|
+
for i in range(len(activations) - len(scaler_params)):
|
194
219
|
|
195
220
|
scaler_params.append(' ')
|
196
221
|
|
197
222
|
scaler_params[0] = scaler_params[0].get()
|
198
223
|
scaler_params[1] = scaler_params[1].get()
|
199
224
|
|
200
|
-
from .__init__ import __version__
|
201
225
|
|
202
226
|
data = {'MODEL NAME': model_name,
|
203
227
|
'MODEL TYPE': model_type,
|
@@ -209,9 +233,9 @@ def save_model(model_name,
|
|
209
233
|
'SAVE DATE': datetime.now(),
|
210
234
|
'WEIGHTS TYPE': weights_type,
|
211
235
|
'WEIGHTS FORMAT': weights_format,
|
212
|
-
'MODEL PATH': model_path,
|
213
236
|
'STANDARD SCALER': scaler_params,
|
214
|
-
'ACTIVATION FUNCTIONS':
|
237
|
+
'ACTIVATION FUNCTIONS': activations,
|
238
|
+
'ACTIVATION POTENTIATION': activation_potentiation
|
215
239
|
}
|
216
240
|
|
217
241
|
df = pd.DataFrame(data)
|
@@ -304,13 +328,6 @@ def load_model(model_name,
|
|
304
328
|
|
305
329
|
sys.exit()
|
306
330
|
|
307
|
-
try:
|
308
|
-
activations = list(df['ACTIVATION FUNCTIONS']) # for PyerualJetwork >=5 Versions.
|
309
|
-
except KeyError:
|
310
|
-
activations = list(df['ACTIVATION POTENTIATION']) # for PyerualJetwork <5 Versions.
|
311
|
-
|
312
|
-
activations = [x for x in activations if not (isinstance(x, float) and cp.isnan(x))]
|
313
|
-
activations = [item for item in activations if item != '']
|
314
331
|
|
315
332
|
scaler_params_cpu = df['STANDARD SCALER'].tolist()
|
316
333
|
|
@@ -331,6 +348,15 @@ def load_model(model_name,
|
|
331
348
|
WeightFormat = str(df['WEIGHTS FORMAT'].iloc[0])
|
332
349
|
test_acc = str(df['TEST ACCURACY'].iloc[0])
|
333
350
|
|
351
|
+
activations = list(df['ACTIVATION FUNCTIONS'])
|
352
|
+
activations = [x for x in activations if not (isinstance(x, float) and np.isnan(x))]
|
353
|
+
activations = [item for item in activations if item != '']
|
354
|
+
|
355
|
+
activation_potentiation = list(df['ACTIVATION POTENTIATION'])
|
356
|
+
activation_potentiation = [x for x in activation_potentiation if not (isinstance(x, float) and np.isnan(x))]
|
357
|
+
activation_potentiation = [item for item in activation_potentiation if item != '']
|
358
|
+
|
359
|
+
|
334
360
|
device_version = __version__
|
335
361
|
|
336
362
|
try:
|
@@ -345,7 +371,7 @@ def load_model(model_name,
|
|
345
371
|
except:
|
346
372
|
pass # Version check only in >= 5.0.2
|
347
373
|
|
348
|
-
if model_type == 'MLP': allow_pickle = True
|
374
|
+
if model_type == 'MLP' or model_type == 'PTNN': allow_pickle = True
|
349
375
|
else: allow_pickle = False
|
350
376
|
|
351
377
|
if WeightType == 'txt':
|
@@ -369,7 +395,7 @@ def load_model(model_name,
|
|
369
395
|
W = W.tolist()
|
370
396
|
W = [cp.array(item) for item in W]
|
371
397
|
|
372
|
-
return W, None, test_acc, activations, scaler_params, None, model_type, WeightType, WeightFormat, device_version, df
|
398
|
+
return W, None, test_acc, activations, scaler_params, None, model_type, WeightType, WeightFormat, device_version, df, activation_potentiation
|
373
399
|
|
374
400
|
|
375
401
|
|
@@ -404,6 +430,7 @@ def predict_from_storage(Input, model_name, model_path='', dtype=cp.float32):
|
|
404
430
|
scaler_params = model[get_scaler()]
|
405
431
|
W = model[get_weights()]
|
406
432
|
model_type = model[get_model_type()]
|
433
|
+
activation_potentiation = model[get_act_pot()]
|
407
434
|
|
408
435
|
if isinstance(activations, str):
|
409
436
|
activations = [activations]
|
@@ -413,20 +440,37 @@ def predict_from_storage(Input, model_name, model_path='', dtype=cp.float32):
|
|
413
440
|
Input = standard_scaler(None, Input, scaler_params)
|
414
441
|
|
415
442
|
if model_type == 'MLP':
|
416
|
-
|
417
443
|
layer = Input
|
418
444
|
for i in range(len(W)):
|
419
445
|
if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
|
446
|
+
|
420
447
|
layer = layer @ W[i].T
|
421
|
-
|
422
|
-
return layer
|
423
448
|
|
424
|
-
|
449
|
+
result = layer
|
450
|
+
|
451
|
+
if model_type == 'PLAN':
|
425
452
|
|
426
453
|
Input = apply_activation(Input, activations)
|
427
454
|
result = Input @ W.T
|
455
|
+
|
456
|
+
if model_type == 'PTNN':
|
428
457
|
|
429
|
-
|
458
|
+
if isinstance(activation_potentiation, str):
|
459
|
+
activation_potentiation = [activation_potentiation]
|
460
|
+
elif isinstance(activation_potentiation, list):
|
461
|
+
activation_potentiation = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activation_potentiation]
|
462
|
+
|
463
|
+
Input = apply_activation(Input, activation_potentiation)
|
464
|
+
layer = Input @ W[0].T
|
465
|
+
|
466
|
+
for i in range(1, len(W)):
|
467
|
+
if i != len(W) - 1: layer = apply_activation(layer, activations[i])
|
468
|
+
|
469
|
+
layer = layer @ W[i].T
|
470
|
+
|
471
|
+
result = layer
|
472
|
+
|
473
|
+
return result
|
430
474
|
|
431
475
|
except:
|
432
476
|
print(Fore.RED + "ERROR: Unexpected Output or wrong model parameters from: predict_model_ssd." + Style.RESET_ALL)
|
@@ -466,7 +510,7 @@ def reverse_predict_from_storage(output, model_name, model_path='', dtype=cp.flo
|
|
466
510
|
sys.exit()
|
467
511
|
|
468
512
|
|
469
|
-
def predict_from_memory(Input, W, scaler_params=None, activations=['linear'],
|
513
|
+
def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], activation_potentiation=None, model_type='PLAN'):
|
470
514
|
|
471
515
|
"""
|
472
516
|
Function to make a prediction
|
@@ -481,7 +525,10 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
|
|
481
525
|
|
482
526
|
activations (list[str]): activation list for deep PLAN or activation list for MLP layers. Default: ['linear']
|
483
527
|
|
484
|
-
|
528
|
+
activation_potentiation (list, optional): Extra activation potentiation list (PLAN layers activations) for just PTNN models. Default = None.
|
529
|
+
|
530
|
+
model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'. Default: PLAN
|
531
|
+
|
485
532
|
Returns:
|
486
533
|
cupyarray: Output from the model.
|
487
534
|
"""
|
@@ -495,24 +542,41 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
|
|
495
542
|
activations = [activations]
|
496
543
|
elif isinstance(activations, list):
|
497
544
|
activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
|
498
|
-
|
545
|
+
|
499
546
|
Input = standard_scaler(None, Input, scaler_params)
|
500
|
-
|
501
|
-
if
|
502
|
-
|
547
|
+
|
548
|
+
if model_type == 'MLP':
|
503
549
|
layer = Input
|
504
550
|
for i in range(len(W)):
|
505
551
|
if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
|
552
|
+
|
506
553
|
layer = layer @ W[i].T
|
507
|
-
|
508
|
-
return layer
|
509
554
|
|
510
|
-
|
555
|
+
result = layer
|
556
|
+
|
557
|
+
if model_type == 'PLAN':
|
511
558
|
|
512
559
|
Input = apply_activation(Input, activations)
|
513
560
|
result = Input @ W.T
|
561
|
+
|
562
|
+
if model_type == 'PTNN':
|
563
|
+
|
564
|
+
if isinstance(activation_potentiation, str):
|
565
|
+
activation_potentiation = [activation_potentiation]
|
566
|
+
elif isinstance(activation_potentiation, list):
|
567
|
+
activation_potentiation = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activation_potentiation]
|
568
|
+
|
569
|
+
Input = apply_activation(Input, activation_potentiation)
|
570
|
+
layer = Input @ W[0].T
|
571
|
+
|
572
|
+
for i in range(1, len(W)):
|
573
|
+
if i != len(W) - 1: layer = apply_activation(layer, activations[i])
|
514
574
|
|
515
|
-
|
575
|
+
layer = layer @ W[i].T
|
576
|
+
|
577
|
+
result = layer
|
578
|
+
|
579
|
+
return result
|
516
580
|
|
517
581
|
except:
|
518
582
|
print(Fore.RED + "ERROR: Unexpected input or wrong model parameters from: predict_model_ram." + Style.RESET_ALL)
|
@@ -599,4 +663,9 @@ def get_model_version():
|
|
599
663
|
|
600
664
|
def get_model_df():
|
601
665
|
|
602
|
-
return 10
|
666
|
+
return 10
|
667
|
+
|
668
|
+
|
669
|
+
def get_act_pot():
|
670
|
+
|
671
|
+
return 11
|