pyerualjetwork 4.0.5__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.
@@ -0,0 +1,463 @@
1
+ from tqdm import tqdm
2
+ import cupy as cp
3
+ from colorama import Fore, Style
4
+ import sys
5
+ import math
6
+ import numpy as np
7
+
8
+ def encode_one_hot(y_train, y_test=None, summary=False):
9
+ """
10
+ Performs one-hot encoding on y_train and y_test data.
11
+
12
+ Args:
13
+ y_train (cupy.ndarray): Train label data.
14
+ y_test (cupy.ndarray): Test label data. (optional).
15
+ summary (bool): If True, prints the class-to-index mapping. Default: False
16
+
17
+ Returns:
18
+ tuple: One-hot encoded y_train and (if given) y_test.
19
+ """
20
+
21
+ if len(y_train) < 256:
22
+ if y_train.dtype != cp.uint8:
23
+ y_train = cp.array(y_train, copy=False).astype(cp.uint8, copy=False)
24
+ elif len(y_train) <= 32767:
25
+ if y_train.dtype != cp.uint16:
26
+ y_train = cp.array(y_train, copy=False).astype(cp.uint16, copy=False)
27
+ else:
28
+ if y_train.dtype != cp.uint32:
29
+ y_train = cp.array(y_train, copy=False).astype(cp.uint32, copy=False)
30
+
31
+ if y_test is not None:
32
+ if len(y_test) < 256:
33
+ if y_test.dtype != cp.uint8:
34
+ y_test = cp.array(y_test, copy=False).astype(cp.uint8, copy=False)
35
+ elif len(y_test) <= 32767:
36
+ if y_test.dtype != cp.uint16:
37
+ y_test = cp.array(y_test, copy=False).astype(cp.uint16, copy=False)
38
+ else:
39
+ if y_test.dtype != cp.uint32:
40
+ y_test = cp.array(y_test, copy=False).astype(cp.uint32, copy=False)
41
+
42
+ y_train = y_train.get()
43
+ y_test = y_test.get()
44
+
45
+ classes = np.unique(y_train)
46
+ class_count = len(classes)
47
+
48
+ class_to_index = {cls: idx for idx, cls in enumerate(classes)}
49
+
50
+ if summary:
51
+ print("Class-to-index mapping:")
52
+ for cls, idx in class_to_index.items():
53
+ print(f" {idx}: {cls}")
54
+
55
+ y_train_encoded = np.zeros((y_train.shape[0], class_count))
56
+ for i, label in enumerate(y_train):
57
+ y_train_encoded[i, class_to_index[label]] = 1
58
+
59
+ if y_test is not None:
60
+ y_test_encoded = np.zeros((y_test.shape[0], class_count))
61
+ for i, label in enumerate(y_test):
62
+ y_test_encoded[i, class_to_index[label]] = 1
63
+ return cp.array(y_train_encoded), cp.array(y_test_encoded)
64
+
65
+ return cp.array(y_train_encoded)
66
+
67
+
68
+ def decode_one_hot(encoded_data):
69
+ """
70
+ Decodes one-hot encoded data to original categorical labels.
71
+
72
+ Args:
73
+ encoded_data (cupy.ndarray): One-hot encoded data with shape (n_samples, n_classes).
74
+
75
+ Returns:
76
+ cupy.ndarray: Decoded categorical labels with shape (n_samples,).
77
+ """
78
+
79
+ decoded_labels = cp.argmax(encoded_data, axis=1)
80
+
81
+ return decoded_labels
82
+
83
+
84
+ def split(X, y, test_size, random_state, dtype=cp.float32):
85
+ """
86
+ Splits the given X (features) and y (labels) data into training and testing subsets.
87
+
88
+ Args:
89
+ X (cupy.ndarray): Features data.
90
+ y (cupy.ndarray): Labels data.
91
+ test_size (float or int): Proportion or number of samples for the test subset.
92
+ random_state (int or None): Seed for random state.
93
+ 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)
94
+
95
+ Returns:
96
+ tuple: x_train, x_test, y_train, y_test as ordered training and testing data subsets.
97
+ """
98
+ X = cp.array(X, copy=False).astype(dtype, copy=False)
99
+ if len(y) < 256:
100
+ if y.dtype != cp.uint8:
101
+ y = cp.array(y, copy=False).astype(cp.uint8, copy=False)
102
+ elif len(y) <= 32767:
103
+ if y.dtype != cp.uint16:
104
+ y = cp.array(y, copy=False).astype(cp.uint16, copy=False)
105
+ else:
106
+ if y.dtype != cp.uint32:
107
+ y = cp.array(y, copy=False).astype(cp.uint32, copy=False)
108
+
109
+
110
+ num_samples = X.shape[0]
111
+
112
+ if isinstance(test_size, float):
113
+ test_size = int(test_size * num_samples)
114
+ elif isinstance(test_size, int):
115
+ if test_size > num_samples:
116
+ raise ValueError(
117
+ "test_size cannot be larger than the number of samples.")
118
+ else:
119
+ raise ValueError("test_size should be float or int.")
120
+
121
+ if random_state is not None:
122
+ cp.random.seed(random_state)
123
+
124
+ indices = cp.arange(num_samples)
125
+ cp.random.shuffle(indices)
126
+
127
+ test_indices = indices[:test_size]
128
+ train_indices = indices[test_size:]
129
+
130
+ x_train, x_test = X[train_indices], X[test_indices]
131
+ y_train, y_test = y[train_indices], y[test_indices]
132
+
133
+ return x_train, x_test, y_train, y_test
134
+
135
+
136
+ def manuel_balancer(x_train, y_train, target_samples_per_class, dtype=cp.float32):
137
+ """
138
+ Generates synthetic examples to balance classes to the specified number of examples per class.
139
+
140
+ Arguments:
141
+ x_train -- Input dataset (examples) - cupy array format
142
+ y_train -- Class labels (one-hot encoded) - cupy array format
143
+ target_samples_per_class -- Desired number of samples per class
144
+ 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)
145
+
146
+ Returns:
147
+ x_balanced -- Balanced input dataset (cupy array format)
148
+ y_balanced -- Balanced class labels (one-hot encoded, cupy array format)
149
+ """
150
+ from .ui import loading_bars
151
+
152
+ bar_format = loading_bars()[0]
153
+
154
+ x_train = cp.array(x_train, copy=False).astype(dtype, copy=False)
155
+
156
+ if len(y_train[0]) < 256:
157
+ if y_train.dtype != cp.uint8:
158
+ y_train = cp.array(y_train, copy=False).astype(cp.uint8, copy=False)
159
+ elif len(y_train[0]) <= 32767:
160
+ if y_train.dtype != cp.uint16:
161
+ y_train = cp.array(y_train, copy=False).astype(cp.uint16, copy=False)
162
+ else:
163
+ if y_train.dtype != cp.uint32:
164
+ y_train = cp.array(y_train, copy=False).astype(cp.uint32, copy=False)
165
+
166
+
167
+ classes = cp.arange(y_train.shape[1])
168
+ class_count = len(classes)
169
+
170
+ x_balanced = []
171
+ y_balanced = []
172
+
173
+ for class_label in tqdm(range(class_count),leave=False, ascii="▱▰",
174
+ bar_format=bar_format,desc='Augmenting Data',ncols= 52):
175
+ class_indices = cp.where(cp.argmax(y_train, axis=1) == class_label)[0]
176
+ num_samples = len(class_indices)
177
+
178
+ if num_samples > target_samples_per_class:
179
+
180
+ selected_indices = cp.random.choice(class_indices, target_samples_per_class, replace=False)
181
+ x_balanced.append(x_train[selected_indices])
182
+ y_balanced.append(y_train[selected_indices])
183
+
184
+ else:
185
+
186
+ x_balanced.append(x_train[class_indices])
187
+ y_balanced.append(y_train[class_indices])
188
+
189
+ if num_samples < target_samples_per_class:
190
+
191
+ samples_to_add = target_samples_per_class - num_samples
192
+ additional_samples = cp.zeros((samples_to_add, x_train.shape[1]))
193
+ additional_labels = cp.zeros((samples_to_add, y_train.shape[1]))
194
+
195
+ for i in range(samples_to_add):
196
+
197
+ random_indices = cp.random.choice(class_indices, 2, replace=False)
198
+ sample1 = x_train[random_indices[0]]
199
+ sample2 = x_train[random_indices[1]]
200
+
201
+
202
+ synthetic_sample = sample1 + (sample2 - sample1) * cp.random.rand()
203
+
204
+ additional_samples[i] = synthetic_sample
205
+ additional_labels[i] = y_train[class_indices[0]]
206
+
207
+
208
+ x_balanced.append(additional_samples)
209
+ y_balanced.append(additional_labels)
210
+
211
+ x_balanced = cp.vstack(x_balanced)
212
+ y_balanced = cp.vstack(y_balanced)
213
+
214
+ return x_balanced, y_balanced
215
+
216
+
217
+ def auto_balancer(x_train, y_train, dtype=cp.float32):
218
+
219
+ """
220
+ Function to balance the training data across different classes.
221
+
222
+ Arguments:
223
+ x_train (list): Input data for training.
224
+
225
+ y_train (list): Labels corresponding to the input data.
226
+
227
+ 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)
228
+
229
+ Returns:
230
+ tuple: A tuple containing balanced input data and labels.
231
+ """
232
+ from .ui import loading_bars
233
+
234
+ x_train = cp.array(x_train, copy=False).astype(dtype, copy=False)
235
+
236
+ if len(y_train[0]) < 256:
237
+ if y_train.dtype != cp.uint8:
238
+ y_train = cp.array(y_train, copy=False).astype(cp.uint8, copy=False)
239
+ elif len(y_train[0]) <= 32767:
240
+ if y_train.dtype != cp.uint16:
241
+ y_train = cp.array(y_train, copy=False).astype(cp.uint16, copy=False)
242
+ else:
243
+ if y_train.dtype != cp.uint32:
244
+ y_train = cp.array(y_train, copy=False).astype(cp.uint32, copy=False)
245
+
246
+ bar_format = loading_bars()[0]
247
+
248
+ classes = cp.arange(y_train.shape[1])
249
+ class_count = len(classes)
250
+
251
+ try:
252
+ ClassIndices = {i: cp.where(cp.array(y_train)[:, i] == 1)[
253
+ 0] for i in range(class_count)}
254
+ classes = [len(ClassIndices[i]) for i in range(class_count)]
255
+
256
+ if len(set(classes)) == 1:
257
+ print(Fore.WHITE + "INFO: Data have already balanced. from: auto_balancer" + Style.RESET_ALL)
258
+ return x_train, y_train
259
+
260
+ MinCount = min(classes)
261
+
262
+ BalancedIndices = []
263
+ for i in tqdm(range(class_count),leave=False, ascii="▱▰",
264
+ bar_format= bar_format, desc='Balancing Data',ncols=70):
265
+ if len(ClassIndices[i]) > MinCount:
266
+ SelectedIndices = cp.random.choice(
267
+ ClassIndices[i], MinCount, replace=False)
268
+ else:
269
+ SelectedIndices = ClassIndices[i]
270
+ BalancedIndices.extend(SelectedIndices)
271
+
272
+ BalancedInputs = [x_train[idx] for idx in BalancedIndices]
273
+ BalancedLabels = [y_train[idx] for idx in BalancedIndices]
274
+
275
+ permutation = cp.random.permutation(len(BalancedInputs))
276
+ BalancedInputs = cp.array(BalancedInputs)[permutation]
277
+ BalancedLabels = cp.array(BalancedLabels)[permutation]
278
+
279
+ print(Fore.GREEN + "Data Succesfully Balanced from: " + str(len(x_train)
280
+ ) + " to: " + str(len(BalancedInputs)) + ". from: auto_balancer " + Style.RESET_ALL)
281
+ except:
282
+ print(Fore.RED + "ERROR: Inputs and labels must be same length check parameters")
283
+ sys.exit()
284
+
285
+ return BalancedInputs, BalancedLabels
286
+
287
+
288
+ def synthetic_augmentation(x_train, y_train, dtype=cp.float32):
289
+ """
290
+ Generates synthetic examples to balance classes with fewer examples using CuPy.
291
+ Arguments:
292
+
293
+ x_train -- Input dataset (examples) - cupy array format
294
+
295
+ y_train -- Class labels (one-hot encoded) - cupy array format
296
+
297
+ 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)
298
+
299
+ Returns:
300
+ x_train_balanced -- Balanced input dataset (cupy array format)
301
+ y_train_balanced -- Balanced class labels (one-hot encoded, cupy array format)
302
+ """
303
+ from .ui import loading_bars
304
+
305
+ bar_format = loading_bars()[0]
306
+
307
+ x = x_train.astype(dtype, copy=False)
308
+
309
+ if len(y_train[0]) < 256:
310
+ if y_train.dtype != cp.uint8:
311
+ y_train = cp.array(y_train, copy=False).astype(cp.uint8, copy=False)
312
+ elif len(y_train[0]) <= 32767:
313
+ if y_train.dtype != cp.uint16:
314
+ y_train = cp.array(y_train, copy=False).astype(cp.uint16, copy=False)
315
+ else:
316
+ if y_train.dtype != cp.uint32:
317
+ y_train = cp.array(y_train, copy=False).astype(cp.uint32, copy=False)
318
+
319
+ y = y_train
320
+
321
+ classes = cp.arange(y_train.shape[1])
322
+ class_count = len(classes)
323
+ class_distribution = {i: 0 for i in range(class_count)}
324
+
325
+ y_cpu = cp.asnumpy(y) if isinstance(y, cp.ndarray) else y
326
+ for label in y_cpu:
327
+ class_distribution[cp.argmax(label).item()] += 1
328
+
329
+ max_class_count = max(class_distribution.values())
330
+ x_balanced = list(x)
331
+ y_balanced = list(y)
332
+
333
+ for class_label in tqdm(range(class_count), leave=False, ascii="▱▰",
334
+ bar_format=bar_format, desc='Augmenting Data', ncols=52):
335
+ class_indices = [i for i, label in enumerate(y) if cp.argmax(label) == class_label]
336
+ num_samples = len(class_indices)
337
+
338
+ if num_samples < max_class_count:
339
+ while num_samples < max_class_count:
340
+ random_indices = cp.random.choice(
341
+ cp.array(class_indices), 2, replace=False)
342
+ sample1 = x[random_indices[0]]
343
+ sample2 = x[random_indices[1]]
344
+
345
+ synthetic_sample = sample1 + \
346
+ (sample2 - sample1) * cp.random.rand()
347
+
348
+ x_balanced.append(synthetic_sample)
349
+ y_balanced.append(y[class_indices[0]])
350
+ num_samples += 1
351
+
352
+ x_balanced = cp.array(x_balanced)
353
+ y_balanced = cp.array(y_balanced)
354
+
355
+ return x_balanced, y_balanced
356
+
357
+ def standard_scaler(x_train=None, x_test=None, scaler_params=None, dtype=cp.float32):
358
+ """
359
+ Standardizes training and test datasets. x_test may be None.
360
+
361
+ Args:
362
+ x_train: cupy.ndarray
363
+
364
+ x_test: cupy.ndarray (optional)
365
+
366
+ scaler_params (optional for using model)
367
+
368
+ dtype (cupy.dtype): Data type for the arrays. cp.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
369
+
370
+ Returns:
371
+ list:
372
+ Scaler parameters: mean and std
373
+ tuple
374
+ Standardized training and test datasets
375
+ """
376
+ if x_train is not None and scaler_params is None and x_test is not None:
377
+ x_train = x_train.astype(dtype, copy=False)
378
+ x_test = x_test.astype(dtype, copy=False)
379
+
380
+ mean = cp.mean(x_train, axis=0)
381
+ std = cp.std(x_train, axis=0)
382
+
383
+ train_data_scaled = (x_train - mean) / std
384
+ test_data_scaled = (x_test - mean) / std
385
+
386
+ train_data_scaled = cp.nan_to_num(train_data_scaled, nan=0)
387
+ test_data_scaled = cp.nan_to_num(test_data_scaled, nan=0)
388
+
389
+ scaler_params = [mean, std]
390
+
391
+ return scaler_params, train_data_scaled, test_data_scaled
392
+
393
+ if scaler_params is None and x_train is None and x_test is not None:
394
+ return x_test.astype(dtype, copy=False) # sample data not scaled
395
+
396
+ if scaler_params is not None:
397
+ x_test = x_test.astype(dtype, copy=False)
398
+ scaled_data = (x_test - scaler_params[0]) / scaler_params[1]
399
+ scaled_data = cp.nan_to_num(scaled_data, nan=0)
400
+
401
+ return scaled_data # sample data scaled
402
+
403
+
404
+ def normalization(
405
+ Input, # num: Input data to be normalized.
406
+ dtype=cp.float32
407
+ ):
408
+ """
409
+ Normalizes the input data using maximum absolute scaling.
410
+
411
+ Args:
412
+ Input (num): Input data to be normalized.
413
+
414
+ dtype (cupy.dtype): Data type for the arrays. cp.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
415
+
416
+ Returns:
417
+ (num) Scaled input data after normalization.
418
+ """
419
+
420
+ MaxAbs = cp.max(cp.abs(Input.astype(dtype, copy=False)))
421
+ return (Input / MaxAbs)
422
+
423
+
424
+ def find_closest_factors(a):
425
+
426
+ root = int(math.sqrt(a))
427
+
428
+ for i in range(root, 0, -1):
429
+ if a % i == 0:
430
+ j = a // i
431
+ return i, j
432
+
433
+
434
+ def batcher(x_test, y_test, batch_size=1):
435
+ y_labels = cp.argmax(y_test, axis=1) # Sınıf etiketlerini belirle
436
+
437
+ unique_labels = cp.unique(y_labels) # Tüm sınıfları bul
438
+ total_samples = sum(
439
+ int(cp.sum(y_labels == class_label) * batch_size) for class_label in unique_labels
440
+ )
441
+
442
+ sampled_x = cp.empty((total_samples, x_test.shape[1]), dtype=x_test.dtype)
443
+ sampled_y = cp.empty((total_samples, y_test.shape[1]), dtype=y_test.dtype)
444
+
445
+ offset = 0
446
+ for class_label in unique_labels:
447
+ # Sınıfa ait indeksleri bulun
448
+ class_indices = cp.where(y_labels == class_label)[0]
449
+
450
+ # Örnek sayısını belirle
451
+ num_samples = int(len(class_indices) * batch_size)
452
+
453
+ # Rastgele örnek seç
454
+ sampled_indices = cp.random.choice(class_indices, num_samples, replace=False)
455
+
456
+ # Veriyi sampled dizilerine yaz
457
+ sampled_x[offset:offset + num_samples] = x_test[sampled_indices]
458
+ sampled_y[offset:offset + num_samples] = y_test[sampled_indices]
459
+
460
+ # Kaydırmayı güncelle
461
+ offset += num_samples
462
+
463
+ return sampled_x, sampled_y
pyerualjetwork/help.py ADDED
@@ -0,0 +1,16 @@
1
+ from .activation_functions import all_activations
2
+
3
+ def activation_potentiation():
4
+
5
+ activations_list = all_activations()
6
+
7
+ print('All available activations: ', activations_list, "\n\nYOU CAN COMBINE EVERY ACTIVATION. EXAMPLE: ['linear', 'tanh'] or ['waveakt', 'linear', 'sine'].")
8
+
9
+ return activations_list
10
+
11
+ def docs_and_examples():
12
+
13
+ print('PLAN document: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PLAN\n')
14
+ print('PLAN examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes\n')
15
+ print('PLANEAT examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes/PLANEAT\n')
16
+ print('PyerualJetwork document and examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork')
@@ -0,0 +1,21 @@
1
+
2
+ import numpy as np
3
+
4
+ def categorical_crossentropy(y_true_batch, y_pred_batch):
5
+ epsilon = 1e-7
6
+ y_pred_batch = np.clip(y_pred_batch, epsilon, 1. - epsilon)
7
+
8
+ losses = -np.sum(y_true_batch * np.log(y_pred_batch), axis=1)
9
+
10
+ mean_loss = np.mean(losses)
11
+ return mean_loss
12
+
13
+
14
+ def binary_crossentropy(y_true_batch, y_pred_batch):
15
+ epsilon = 1e-7
16
+ y_pred_batch = np.clip(y_pred_batch, epsilon, 1. - epsilon)
17
+
18
+ losses = -np.mean(y_true_batch * np.log(y_pred_batch) + (1 - y_true_batch) * np.log(1 - y_pred_batch), axis=1)
19
+
20
+ mean_loss = np.mean(losses)
21
+ return mean_loss
@@ -0,0 +1,21 @@
1
+
2
+ import cupy as cp
3
+
4
+ def categorical_crossentropy(y_true_batch, y_pred_batch):
5
+ epsilon = 1e-7
6
+ y_pred_batch = cp.clip(y_pred_batch, epsilon, 1. - epsilon)
7
+
8
+ losses = -cp.sum(y_true_batch * cp.log(y_pred_batch), axis=1)
9
+
10
+ mean_loss = cp.mean(losses)
11
+ return mean_loss
12
+
13
+
14
+ def binary_crossentropy(y_true_batch, y_pred_batch):
15
+ epsilon = 1e-7
16
+ y_pred_batch = cp.clip(y_pred_batch, epsilon, 1. - epsilon)
17
+
18
+ losses = -cp.mean(y_true_batch * cp.log(y_pred_batch) + (1 - y_true_batch) * cp.log(1 - y_pred_batch), axis=1)
19
+
20
+ mean_loss = cp.mean(losses)
21
+ return mean_loss