pyerualjetwork 5.0.2b1__py3-none-any.whl → 5.1__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.
@@ -28,6 +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()
35
+ - get_act_pot()
31
36
 
32
37
  Examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes
33
38
 
@@ -57,6 +62,7 @@ def save_model(model_name,
57
62
  test_acc=None,
58
63
  model_path='',
59
64
  activations=['linear'],
65
+ activation_potentiation=[],
60
66
  weights_type='npy',
61
67
  weights_format='raw',
62
68
  show_architecture=False,
@@ -70,9 +76,9 @@ def save_model(model_name,
70
76
 
71
77
  W: Weights of the model.
72
78
 
73
- model_type: (str): Type of the model. Options: 'PLAN', 'MLP'.
79
+ model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'.
74
80
 
75
- scaler_params: (list[num, num]): standard scaler params list: mean,std. If not used standard scaler then be: None.
81
+ scaler_params: (list[num, num]): standard scaler params list: mean,std. If not used standard scaler then be: [].
76
82
 
77
83
  test_acc: (float): Test accuracy of the model. default: None
78
84
 
@@ -80,6 +86,8 @@ def save_model(model_name,
80
86
 
81
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']
82
88
 
89
+ activation_potentiation (list, optional): Extra activation potentiation list (PLAN layers activations) for just PTNN models. Default = None.
90
+
83
91
  weights_type: (str): Type of weights to save (options: 'txt', 'pkl', 'npy', 'mat'). default: 'npy'
84
92
 
85
93
  weights_format: (str): Format of the weights (options: 'f', 'raw'). default: 'raw'
@@ -93,16 +101,28 @@ def save_model(model_name,
93
101
  """
94
102
 
95
103
  from .visualizations_cpu import draw_model_architecture
104
+ from .__init__ import __version__
96
105
 
97
- if model_type != 'PLAN' and model_type != 'MLP':
98
- raise ValueError("model_type parameter must be 'PLAN' or 'MLP'.")
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.')
99
111
 
100
112
  if isinstance(activations, str):
101
113
  activations = [activations]
102
114
  else:
103
115
  activations = [item if isinstance(item, list) else [item] for item in activations]
104
116
 
105
- activation = activations.copy()
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()
106
126
 
107
127
  if test_acc != None:
108
128
  test_acc= float(test_acc)
@@ -130,7 +150,7 @@ def save_model(model_name,
130
150
  print(Fore.RED + "ERROR: Weight matrices have a problem from: save_model" + Style.RESET_ALL)
131
151
  sys.exit()
132
152
 
133
- elif model_type == 'MLP':
153
+ elif model_type == 'MLP' or model_type == 'PTNN':
134
154
 
135
155
  class_count = W[-1].shape[0]
136
156
 
@@ -148,36 +168,42 @@ def save_model(model_name,
148
168
 
149
169
  SynapseCount.append(' ')
150
170
 
151
- activation.append('')
152
- activation.insert(0, '')
153
-
154
- if len(activation) == 1 and model_type == 'PLAN':
155
- activation = [activation]
156
- activation.append('')
171
+ activations.append('')
172
+ activations.insert(0, '')
157
173
 
158
- if len(activation) > len(NeuronCount):
159
- for i in range(len(activation) - len(NeuronCount)):
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)):
160
189
  NeuronCount.append('')
161
190
 
162
- if len(activation) > len(SynapseCount):
163
- for i in range(len(activation) - len(SynapseCount)):
191
+ if len(activations) > len(SynapseCount):
192
+ for i in range(len(activations) - len(SynapseCount)):
164
193
  SynapseCount.append('')
165
194
 
166
-
167
195
  if scaler_params != None:
168
196
 
169
- if len(scaler_params) > len(activation):
197
+ if len(scaler_params) > len(activations):
170
198
 
171
- activation += ['']
199
+ activations += ['']
172
200
 
173
- elif len(activation) > len(scaler_params):
201
+ elif len(activations) > len(scaler_params):
174
202
 
175
- for i in range(len(activation) - len(scaler_params)):
203
+ for i in range(len(activations) - len(scaler_params)):
176
204
 
177
205
  scaler_params.append(' ')
178
206
 
179
- from .__init__ import __version__
180
-
181
207
  data = {'MODEL NAME': model_name,
182
208
  'MODEL TYPE': model_type,
183
209
  'CLASS COUNT': class_count,
@@ -188,9 +214,9 @@ def save_model(model_name,
188
214
  'SAVE DATE': datetime.now(),
189
215
  'WEIGHTS TYPE': weights_type,
190
216
  'WEIGHTS FORMAT': weights_format,
191
- 'MODEL PATH': model_path,
192
217
  'STANDARD SCALER': scaler_params,
193
- 'ACTIVATION FUNCTIONS': activation
218
+ 'ACTIVATION FUNCTIONS': activations,
219
+ 'ACTIVATION POTENTIATION': activation_potentiation
194
220
  }
195
221
 
196
222
  df = pd.DataFrame(data)
@@ -274,26 +300,21 @@ def load_model(model_name,
274
300
  model_path (str): Path where the model is saved.
275
301
 
276
302
  Returns:
277
- lists: (list[df_elements]), DataFrame of the model
303
+ lists: Weights, None, test_accuracy, activations, scaler_params, None, model_type, weight_type, weight_format, device_version, (list[df_elements])=Pandas DataFrame of the model
278
304
  """
279
305
 
306
+ from .__init__ import __version__
307
+
280
308
  try:
281
309
 
282
310
  df = pd.read_pickle(model_path + model_name + '.pkl')
283
311
 
284
312
  except:
285
313
 
286
- print(Fore.RED + "ERROR: Model Path error. acceptable form: 'C:/Users/hasancanbeydili/Desktop/denemePLAN/' from: load_model" + Style.RESET_ALL)
314
+ print(Fore.RED + "ERROR: Model Path or Model Name error. acceptable form: 'C:/Users/hasancanbeydili/Desktop/denemePLAN/' from: load_model" + Style.RESET_ALL)
287
315
 
288
316
  sys.exit()
289
317
 
290
- try:
291
- activations = list(df['ACTIVATION FUNCTIONS']) # for PyerualJetwork >=5 Versions.
292
- except KeyError:
293
- activations = list(df['ACTIVATION POTENTIATION']) # for PyerualJetwork <5 Versions.
294
-
295
- activations = [x for x in activations if not (isinstance(x, float) and np.isnan(x))]
296
- activations = [item for item in activations if item != '']
297
318
 
298
319
  scaler_params = df['STANDARD SCALER'].tolist()
299
320
 
@@ -311,7 +332,13 @@ def load_model(model_name,
311
332
  WeightFormat = str(df['WEIGHTS FORMAT'].iloc[0])
312
333
  test_acc = str(df['TEST ACCURACY'].iloc[0])
313
334
 
314
- from .__init__ import __version__
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 != '']
315
342
 
316
343
  device_version = __version__
317
344
 
@@ -327,7 +354,7 @@ def load_model(model_name,
327
354
  except:
328
355
  pass # Version check only in >= 5.0.2
329
356
 
330
- if model_type == 'MLP': allow_pickle = True
357
+ if model_type == 'MLP' or model_type == 'PTNN': allow_pickle = True
331
358
  else: allow_pickle = False
332
359
 
333
360
  if WeightType == 'txt':
@@ -347,7 +374,7 @@ def load_model(model_name,
347
374
  if WeightType == 'mat':
348
375
  W = W['w']
349
376
 
350
- 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
351
378
 
352
379
 
353
380
 
@@ -370,7 +397,7 @@ def predict_from_storage(Input, model_name, model_path=''):
370
397
 
371
398
  from .activation_functions_cpu import apply_activation
372
399
  from .data_operations_cpu import standard_scaler
373
-
400
+
374
401
  try:
375
402
 
376
403
  model = load_model(model_name, model_path)
@@ -379,30 +406,48 @@ def predict_from_storage(Input, model_name, model_path=''):
379
406
  scaler_params = model[get_scaler()]
380
407
  W = model[get_weights()]
381
408
  model_type = model[get_model_type()]
409
+ activation_potentiation = model[get_act_pot()]
382
410
 
383
411
  if isinstance(activations, str):
384
412
  activations = [activations]
385
413
  elif isinstance(activations, list):
386
414
  activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
387
-
415
+
388
416
  Input = standard_scaler(None, Input, scaler_params)
389
417
 
390
418
  if model_type == 'MLP':
391
-
392
419
  layer = Input
393
420
  for i in range(len(W)):
394
421
  if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
422
+
395
423
  layer = layer @ W[i].T
396
-
397
- return layer
398
424
 
399
- else:
425
+ result = layer
426
+
427
+ if model_type == 'PLAN':
400
428
 
401
429
  Input = apply_activation(Input, activations)
402
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
403
446
 
404
- return result
447
+ result = layer
405
448
 
449
+ return result
450
+
406
451
  except:
407
452
  print(Fore.RED + "ERROR: Unexpected Output or wrong model parameters from: predict_model_storage." + Style.RESET_ALL)
408
453
  sys.exit()
@@ -437,7 +482,7 @@ def reverse_predict_from_storage(output, model_name, model_path=''):
437
482
 
438
483
 
439
484
 
440
- def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is_mlp=False):
485
+ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], activation_potentiation=None, model_type='PLAN'):
441
486
 
442
487
  """
443
488
  Function to make a prediction.
@@ -452,7 +497,10 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
452
497
 
453
498
  activations (list[str]): activation list for deep PLAN or activation list for MLP layers. Default: ['linear']
454
499
 
455
- is_mlp (bool, optional): Predict from PLAN model or MLP model ? Default: False (PLAN)
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
+
456
504
  Returns:
457
505
  ndarray: Output from the model.
458
506
  """
@@ -460,6 +508,8 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
460
508
  from .data_operations_cpu import standard_scaler
461
509
  from .activation_functions_cpu import apply_activation
462
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
+
463
513
  try:
464
514
 
465
515
  Input = standard_scaler(None, Input, scaler_params)
@@ -468,23 +518,40 @@ def predict_from_memory(Input, W, scaler_params=None, activations=['linear'], is
468
518
  activations = [activations]
469
519
  elif isinstance(activations, list):
470
520
  activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
471
-
472
- if is_mlp:
473
-
521
+
522
+ if model_type == 'MLP':
474
523
  layer = Input
475
524
  for i in range(len(W)):
476
525
  if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
526
+
477
527
  layer = layer @ W[i].T
478
-
479
- return layer
480
528
 
481
- else:
529
+ result = layer
530
+
531
+ if model_type == 'PLAN':
482
532
 
483
533
  Input = apply_activation(Input, activations)
484
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])
485
548
 
486
- return result
549
+ layer = layer @ W[i].T
550
+
551
+ result = layer
487
552
 
553
+ return result
554
+
488
555
  except:
489
556
  print(Fore.RED + "ERROR: Unexpected input or wrong model parameters from: predict_model_memory." + Style.RESET_ALL)
490
557
  sys.exit()
@@ -565,4 +632,9 @@ def get_model_version():
565
632
 
566
633
  def get_model_df():
567
634
 
568
- return 10
635
+ return 10
636
+
637
+
638
+ def get_act_pot():
639
+
640
+ return 11