NeuralNetworks 0.2.5__py3-none-any.whl → 0.2.6__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.
@@ -6,7 +6,6 @@
6
6
  # (at your option) any later version.
7
7
 
8
8
  import os
9
- os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
10
9
 
11
10
  import platform
12
11
 
@@ -20,6 +19,8 @@ from torch.utils.data import TensorDataset, DataLoader
20
19
  from torchmetrics.image import PeakSignalNoiseRatio as PSNR
21
20
  from torchvision.transforms import ToTensor, Resize, Compose
22
21
 
22
+ import visualtorch
23
+
23
24
  torch.cuda.empty_cache()
24
25
  def get_best_device():
25
26
 
@@ -51,6 +52,7 @@ def get_best_device():
51
52
 
52
53
  # =========== Unknown OS ===========
53
54
  return torch.device("cpu")
55
+
54
56
  device = get_best_device()
55
57
 
56
58
  # --- Optimisations CUDA ---
@@ -10,7 +10,9 @@ from .FourierFeatures import encode
10
10
  from .Layers import create_layers
11
11
  from .inference import infer
12
12
 
13
- class MLP (nn.Module):
13
+ from ..shared import Module
14
+
15
+ class MLP (Module):
14
16
  """
15
17
  Multi-Layer Perceptron avec encodage de Fourier optionnel.
16
18
 
@@ -49,18 +51,13 @@ class MLP (nn.Module):
49
51
  nb_fourier = 8,
50
52
  norm = "Relu",
51
53
  name = "Net"):
52
- super ().__init__ ()
54
+ super ().__init__ (name)
53
55
 
54
56
  # --- Activation ---
55
57
  self.norm = norm_list.get (norm)
56
58
  if self.norm is None:
57
- print (f"Warning: '{norm}' not recognized, falling back to 'Relu'")
59
+ print (f"Warning: '{norm}' not recognized, fRelung back to 'm is'")
58
60
  self.norm = norm_list.get ("Relu")
59
-
60
- # --- Attributs ---
61
- self.losses = []
62
- self.learnings = []
63
- self.name = name
64
61
 
65
62
  ## --- Encodage Fourier ou passthrough ---
66
63
  self.encodings, self.f = encode (
@@ -82,18 +79,8 @@ class MLP (nn.Module):
82
79
  self.norm
83
80
  )
84
81
 
85
- def forward (self, x):
86
- """
87
- Effectue une passe avant du réseau.
88
-
89
- Parameters
90
- ----------
91
- x : torch.Tensor
92
- Entrées du réseau de shape `(N, input_size)`.
82
+ def _forward (self, x):
83
+ return infer (self, x)
93
84
 
94
- Returns
95
- -------
96
- torch.Tensor
97
- Sortie du MLP de shape `(N, output_size)`.
98
- """
85
+ def train_forward (self, x):
99
86
  return infer (self, x)
@@ -8,19 +8,5 @@
8
8
  from ..Dependances import torch, np, device
9
9
 
10
10
  def infer (net, x):
11
- with torch.no_grad ():
12
- x = x.unsqueeze (0) if x.dim () == 1 else x
13
-
14
- net = net.to (device)
15
- x = x.to (device)
16
- results_list = [net.model (encoding (x)) for encoding in net.encodings]
17
- x = x.to ('cpu')
18
-
19
- output = np.array (
20
- net.f (
21
- torch.cat (results_list, dim = 1)
22
- ).cpu ().numpy ().flatten ()
23
- )
24
- net = net.to ('cpu')
25
-
26
- return output
11
+ results_list = [net.model (encoding (x)) for encoding in net.encodings]
12
+ return net.f (torch.cat (results_list, dim = 1))
@@ -5,7 +5,7 @@
5
5
  # the Free Software Foundation, either version 3 of the License, or
6
6
  # (at your option) any later version.
7
7
 
8
- from ..Dependances import crit_list, optim_list
8
+ from ..Dependances import crit_list, optim_list, np
9
9
  from .train import train_f
10
10
  from .sample_data import sample_data
11
11
 
@@ -15,7 +15,7 @@ class Trainer:
15
15
  *nets,
16
16
  inputs,
17
17
  outputs,
18
- test_size = None,
18
+ train_size = None,
19
19
  optim = 'Adam',
20
20
  init_lr = 0.01,
21
21
  crit = 'MSE',
@@ -24,12 +24,19 @@ class Trainer:
24
24
  self.batch_size = batch_size
25
25
  self.nets = nets
26
26
  self.init_lr = init_lr
27
-
28
- self.X_train, self.X_test, self.y_train, self.y_test = sample_data (
29
- inputs,
30
- outputs,
31
- test_size
32
- )
27
+ if train_size is None:
28
+
29
+ self.X_train = inputs
30
+ self.X_test = inputs
31
+ self.y_train = outputs
32
+ self.y_test = outputs
33
+ else:
34
+
35
+ self.X_train, self.X_test, self.y_train, self.y_test = sample_data (
36
+ inputs,
37
+ outputs,
38
+ min(1,np.abs(1 - train_size))
39
+ )
33
40
 
34
41
  # --- Fonction de perte ---
35
42
  self.crit = crit_list.get(crit)
@@ -27,7 +27,8 @@ def train_f (Trainer, num_epochs = 1, activate_tqdm = True):
27
27
  pbar = tqdm (
28
28
  range (num_epochs),
29
29
  desc = f"train epoch",
30
- disable = not (activate_tqdm)
30
+ disable = not (activate_tqdm),
31
+ mininterval=0.5
31
32
  )
32
33
 
33
34
  for epoch in pbar:
@@ -43,14 +44,8 @@ def train_f (Trainer, num_epochs = 1, activate_tqdm = True):
43
44
  def closure ():
44
45
  Trainer.optims [k].zero_grad (set_to_none = True)
45
46
  with autocast (dev):
46
- loss = Trainer.crit (
47
- net.f (
48
- torch.cat (
49
- [net.model (encoding (Trainer.X_train [idx]))for encoding in net.encodings],
50
- dim = 1
51
- )
52
- ),
53
- Trainer.y_train[idx]
47
+ loss = Trainer.crit (net.train_forward (Trainer.X_train [idx]),
48
+ Trainer.y_train [idx]
54
49
  )
55
50
  scaler.scale (loss).backward ()
56
51
  return loss
@@ -66,7 +61,7 @@ def train_f (Trainer, num_epochs = 1, activate_tqdm = True):
66
61
  for param_group in Trainer.optims [k].param_groups:
67
62
  param_group ['lr'] = net.learnings[-1]
68
63
 
69
- pbar.set_postfix(loss=f"{epoch_loss:.5f}",lr=f"{net.learnings[-1]:.5f}")
64
+ #pbar.set_postfix(loss=f"{epoch_loss:.5f}",lr=f"{net.learnings[-1]:.5f}")
70
65
 
71
66
  net = net.to ('cpu')
72
67
  net.learnings.pop(-1)
@@ -15,4 +15,4 @@ from .Trainer import Trainer
15
15
 
16
16
  from .UI import *
17
17
 
18
- __version__ = "0.2.5"
18
+ __version__ = "0.2.6"
@@ -0,0 +1,8 @@
1
+ # NeuralNetworks - Multi-Layer Perceptrons avec encodage Fourier
2
+ # Copyright (C) 2026 Alexandre Brun
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ from .module import Module
@@ -0,0 +1,41 @@
1
+ # NeuralNetworks - Multi-Layer Perceptrons avec encodage Fourier
2
+ # Copyright (C) 2026 Alexandre Brun
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ from ..Dependances import norm_list, nn, torch, device
9
+
10
+ class Module (nn.Module):
11
+
12
+ def __init__ (self, name = "Net"):
13
+ super ().__init__ ()
14
+
15
+ # --- Attributs ---
16
+ self.losses = []
17
+ self.learnings = []
18
+ self.name = name
19
+
20
+ def _forward (self,x):
21
+ raise Exception ("_forward n'est pas défini dans la classe")
22
+
23
+ def train_forward (self,x):
24
+ raise Exception ("train_forward n'est pas défini dans la classe")
25
+
26
+ def forward (self, x):
27
+
28
+ with torch.no_grad ():
29
+ x = x.unsqueeze (0) if x.dim () == 1 else x
30
+ self = self.to (device)
31
+ x = x.to (device)
32
+
33
+ output = self._forward(x).cpu ().numpy ().flatten ()
34
+
35
+ x = x.to ('cpu')
36
+ self = self.to ('cpu')
37
+
38
+ return output
39
+
40
+
41
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: NeuralNetworks
3
- Version: 0.2.5
3
+ Version: 0.2.6
4
4
  Summary: Multi-Layer Perceptrons with multi-Fourier encoding, variable learning rate, visualization and PyTorch compilation
5
5
  Author-email: Alexandre Brun <alexandre51160@gmail.com>
6
6
  License: GPL-3.0-or-later
@@ -0,0 +1,22 @@
1
+ NeuralNetworks/__init__.py,sha256=DFqmUzb9LQDti9PGdZgvR-1irNeurxsVhvwPEuBj-ro,668
2
+ NeuralNetworks/Dependances/__init__.py,sha256=qEpDbSD8cCq-E5XVisNUVf3kZOYopDnQWToyRefPgKE,1227
3
+ NeuralNetworks/Dependances/matplot.py,sha256=elS8u6DZHYP-8mHEpYNOw3jDzhCAWTld9tm3OAD46zw,957
4
+ NeuralNetworks/Dependances/pytorch.py,sha256=r07s7EYeSQJh0PqODaMby4RttZf8m1mUyHrM3uXm370,3287
5
+ NeuralNetworks/MLP/FourierFeatures.py,sha256=klgRM1HK09oA2NRMDxQMjJJ-WoUd5hV1ip5hHe9rHjI,3250
6
+ NeuralNetworks/MLP/Layers.py,sha256=WAksXsiMxaClyYTxPhlyQbwwj9qTtXs3EWCO1RqjUHY,945
7
+ NeuralNetworks/MLP/__init__.py,sha256=zQUczdC-5xkMBZ-cOFpHJrbgJz0grmVRABEdqk7080c,2739
8
+ NeuralNetworks/MLP/inference.py,sha256=pZja1yhkT3BUh9E2X__2LO5QuAamPHITI32rT5Q_cQo,543
9
+ NeuralNetworks/Trainer/__init__.py,sha256=XYfwidMxpI30y8j3FUgvhtRMrrWLvszZE2EwM72tFq4,1952
10
+ NeuralNetworks/Trainer/dynamic_learning_rate.py,sha256=1JAD-k0cjdL_71zGeeCUFOa61H4PzFITDjZ2nK0TzXU,2340
11
+ NeuralNetworks/Trainer/sample_data.py,sha256=7waC9colb7DXU4yKMcgcCnPG3Guv-isipcgVHJPPCNE,673
12
+ NeuralNetworks/Trainer/train.py,sha256=dpo1VDu0VNlzXPqJWGkmcY_y18Y05DTjA5wDg7AbLco,2989
13
+ NeuralNetworks/UI/Learnings.py,sha256=4TBR5pcjyoBeL7eikNKM6xn25jnqL-mWT7hbrt9q-Gw,1418
14
+ NeuralNetworks/UI/Losses.py,sha256=Tu5xuDiutR9a4xcZKpyWN_tzSDu3_fImEf8FbAEehio,1378
15
+ NeuralNetworks/UI/__init__.py,sha256=L96xwQZJ-HoqqOGxaheosiDKHR3mRopuXkif--rO1J4,409
16
+ NeuralNetworks/shared/__init__.py,sha256=LKnIP46kbzOy7O3bSALwUg7iu8_wpis0LzUVVaW20Ws,376
17
+ NeuralNetworks/shared/module.py,sha256=Y6A1LJRFPWOhwNOjPTTyzxPVafEZMbko_fLXKtXoDYI,1196
18
+ neuralnetworks-0.2.6.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
19
+ neuralnetworks-0.2.6.dist-info/METADATA,sha256=1p55SI9TCwSSkj6N1bR0AgXOWA5fFKUYs1ZPlvSUx40,18349
20
+ neuralnetworks-0.2.6.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
21
+ neuralnetworks-0.2.6.dist-info/top_level.txt,sha256=h18nmC1BX7avyAAwKh0OQWezxgXmOpmVtbFq-8Mcbms,15
22
+ neuralnetworks-0.2.6.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.9.0)
2
+ Generator: setuptools (80.10.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,20 +0,0 @@
1
- NeuralNetworks/__init__.py,sha256=DSCQD_dezpOEjqAGUSQsocNYmNq8STPHZaLjIXXLCF8,668
2
- NeuralNetworks/Dependances/__init__.py,sha256=qEpDbSD8cCq-E5XVisNUVf3kZOYopDnQWToyRefPgKE,1227
3
- NeuralNetworks/Dependances/matplot.py,sha256=elS8u6DZHYP-8mHEpYNOw3jDzhCAWTld9tm3OAD46zw,957
4
- NeuralNetworks/Dependances/pytorch.py,sha256=RQlSV3-8uHAoEgK0FBae7O4Mdug7h_MY--sN1fK59qw,3329
5
- NeuralNetworks/MLP/FourierFeatures.py,sha256=klgRM1HK09oA2NRMDxQMjJJ-WoUd5hV1ip5hHe9rHjI,3250
6
- NeuralNetworks/MLP/Layers.py,sha256=WAksXsiMxaClyYTxPhlyQbwwj9qTtXs3EWCO1RqjUHY,945
7
- NeuralNetworks/MLP/__init__.py,sha256=v7h4Vcmay0_A83spe67HhT7TQ7cSKQX4eVVBJhzEWfk,3052
8
- NeuralNetworks/MLP/inference.py,sha256=9aL7pUx1LTVvrc6UYHX049UjODTgHY6cweFcp2gequQ,853
9
- NeuralNetworks/Trainer/__init__.py,sha256=v0qKqx9XkYWkuouNNy0jTHQ_cZqYhFj98qrwSXlDXy0,1711
10
- NeuralNetworks/Trainer/dynamic_learning_rate.py,sha256=1JAD-k0cjdL_71zGeeCUFOa61H4PzFITDjZ2nK0TzXU,2340
11
- NeuralNetworks/Trainer/sample_data.py,sha256=7waC9colb7DXU4yKMcgcCnPG3Guv-isipcgVHJPPCNE,673
12
- NeuralNetworks/Trainer/train.py,sha256=NAbHFKg4hl96OXq_i63lcRYwrPHiuKu7ihexakhpgDY,3182
13
- NeuralNetworks/UI/Learnings.py,sha256=4TBR5pcjyoBeL7eikNKM6xn25jnqL-mWT7hbrt9q-Gw,1418
14
- NeuralNetworks/UI/Losses.py,sha256=Tu5xuDiutR9a4xcZKpyWN_tzSDu3_fImEf8FbAEehio,1378
15
- NeuralNetworks/UI/__init__.py,sha256=L96xwQZJ-HoqqOGxaheosiDKHR3mRopuXkif--rO1J4,409
16
- neuralnetworks-0.2.5.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
17
- neuralnetworks-0.2.5.dist-info/METADATA,sha256=FjTIFWlGmzIjQmWhFLnTuUI7MfNl0U5jb7EjRQJ7lh8,18349
18
- neuralnetworks-0.2.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
- neuralnetworks-0.2.5.dist-info/top_level.txt,sha256=h18nmC1BX7avyAAwKh0OQWezxgXmOpmVtbFq-8Mcbms,15
20
- neuralnetworks-0.2.5.dist-info/RECORD,,