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