pyyeast 0.1.0__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,40 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyyeast
3
+ Version: 0.1.0
4
+ Summary: PyYeast - A Python library, based on PyTorch, to build and manage, for now (I'm in the Beta version), self-expanding linear layers.
5
+ Author-email: Alessio Parolini <alepar.ap9@gmail.com>
6
+ Requires-Python: >=3.12
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: torch==2.14.1
9
+ Requires-Dist: numpy===2.1.0
10
+
11
+ ![swell logo](./assets/yeast_logo.png)
12
+
13
+ ![Python](https://img.shields.io/badge/Python-3.12-FFD43B?style=for-the-badge&logo=python&logoColor=306998)
14
+ ![PyTorch](https://img.shields.io/badge/PyTorch-2.14-EE4C2C?style=for-the-badge&logo=pytorch&logoColor=red)
15
+ ![Numpy](https://img.shields.io/badge/NumPy-2.1-AA0244?style=for-the-badge&logo=numpy&logoColor=white)
16
+
17
+ ## The project
18
+
19
+ **What's this stuff?**
20
+
21
+ Good question. `PyYeast` is a Python package built for create and handle *SENN*, aka *Self Expanding Neural Networks*.
22
+ A *SENN* can be used to create an **AI model that expand itself during the training**, to improve its capacity.
23
+
24
+ I built it because I think SENN are very useful and powerful, so **my goal is to create a package to make easier build and manage them**, also implementing them in already existing NN.
25
+
26
+ **I hope you'll find my project useful. Remember to leave a star ★!**
27
+
28
+ ## The features
29
+
30
+ Here you can find **the resources the package provides**:
31
+
32
+ from sub-package **`layers`**
33
+
34
+ + **XLinear**: a linear layer that can expand his structure by itself ("*X*" stands for "e*X*panding"). It does it in **2 ways**:
35
+ + **out features expanding**: it increases the number of neurons, **improving the layer capacity**
36
+ + **in features expanding**: it automatically detects if the input tensor is change, **adding new weights to process it correctly**
37
+
38
+ + **XAdapter**: a XLinear layer, but with a special feature:
39
+ + **the filter layer**: it also has a "hidden" XLinear layer, that takes the output and keep it always identical. Thanks of this, **you can use an XAdapter in a static neural network, avoiding crashes**, because the static after the XAdapter receive always the same sizes tensor.
40
+
@@ -0,0 +1,16 @@
1
+ yeast/__init__.py,sha256=IioG1Waa18uGZLWPQPAXDgSV9DpE91QxYsZfJtiU794,62
2
+ yeast/__main__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ yeast/exceptions/__init__.py,sha256=DS7UccwvF3QM4rbS3peNCdkQrHCAU53ZexpZQBQ7QUk,120
4
+ yeast/exceptions/errors.py,sha256=E4xGOoSCBT4hYPyfQSW8bb5VvmZFq3LVaszy57Y0VyE,695
5
+ yeast/layers/__init__.py,sha256=2Zz3OSNWk4biD6-BZg5hY_sRL1K-UeaRHsi5sQqlO2I,108
6
+ yeast/layers/adapter.py,sha256=NwuFsY6wjIaVQOzm8R8UPGgs5NjSQw2M-zPhMRtfVV4,1778
7
+ yeast/layers/linear.py,sha256=6ditMz37axKLJkazC1SFzGgNghdMHwpSInUuMNg4V6o,4699
8
+ yeast/playground/_01_tensors.py,sha256=83aENnUHjUgkXVZB__FSAVKEZanJpPV7-CXRcWOTWkM,1677
9
+ yeast/playground/_02_operations.py,sha256=oCun5pcWXqExabw0uDGDDIET6e9wuONPqzGP1E6CM1U,1439
10
+ yeast/playground/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ yeast/utils/__init__.py,sha256=WHJeksdtlkGL4v7gzVmQTGShaLhT1HwUod03EOrtaG4,64
12
+ yeast/utils/randomdata.py,sha256=HRsWQAi82m3u-b04jjiZB0gWZaYQuwAtcF0auS8ryF8,522
13
+ pyyeast-0.1.0.dist-info/METADATA,sha256=fpBiFwZlhajJJZduRge4O0d-rBTnWOqUxp9Ro6B9Jmg,2126
14
+ pyyeast-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
15
+ pyyeast-0.1.0.dist-info/top_level.txt,sha256=4qrDNePtz2Cwfxe1MOP_fUQUCIXuw9zINI9dic977j0,6
16
+ pyyeast-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ yeast
yeast/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from yeast import layers, utils
2
+
3
+ __all__ = ["sell", "utils"]
yeast/__main__.py ADDED
File without changes
@@ -0,0 +1,3 @@
1
+ from yeast.exceptions.errors import *
2
+
3
+ __all__ = ["SwellError", "XLayersError", "LayersTypeError", "LayersValueError"]
@@ -0,0 +1,41 @@
1
+ """
2
+ errors.py
3
+ Authors: Alessio Parolini
4
+ This file contains the customized Exception classes for the package.
5
+ """
6
+
7
+ __all__ = ["SwellError", "XLayersError", "LayersTypeError", "LayersValueError"]
8
+
9
+ class SwellError(Exception):
10
+
11
+ """
12
+ The default package exception
13
+ """
14
+
15
+ pass
16
+
17
+ # --- LAYERS ERRORS ---
18
+
19
+ class XLayersError(SwellError):
20
+
21
+ """
22
+ The default error for layers resources
23
+ """
24
+
25
+ pass
26
+
27
+ class LayersTypeError(XLayersError, TypeError):
28
+
29
+ """
30
+ The exception for wrong types in layers
31
+ """
32
+
33
+ pass
34
+
35
+ class LayersValueError(XLayersError, ValueError):
36
+
37
+ """
38
+ The exception for wrong values in layers
39
+ """
40
+
41
+ pass
@@ -0,0 +1,5 @@
1
+ from yeast.layers.linear import *
2
+
3
+ from yeast.layers.adapter import *
4
+
5
+ __all__ = ["XLinear", "XAdapter"]
@@ -0,0 +1,63 @@
1
+ """
2
+ adapter.py
3
+ Authors: Alessio Parolini
4
+ Contains the class that groups ExpLinear and FilterLayer for an easier implementation
5
+ """
6
+
7
+ import torch
8
+
9
+ from torch import nn
10
+
11
+ from torch.nn import functional as f
12
+
13
+ from .linear import XLinear
14
+
15
+ from yeast.exceptions import XLayersError, LayersValueError, LayersTypeError
16
+
17
+ class XAdapter(XLinear):
18
+
19
+ """
20
+ This class is designed to create XLinear layers that can be inserted in a classic NN without input sizes errors.
21
+ For ensure this, the XAdapter layer has a second "filter" XLinear layer that keep the output identical during all the training
22
+ """
23
+
24
+ def __init__(self, in_features: int | None, out_features:int, exp_rate:int = 1):
25
+
26
+ """
27
+ The passed arguments are the same for XLinear
28
+ """
29
+
30
+ super().__init__(in_features=in_features, out_features=out_features, exp_rate=exp_rate)
31
+
32
+ self.weights = nn.Parameter(data=torch.randn(self.out_features, self.in_features))
33
+
34
+ self.filter = XLinear(out_features=self.out_features) #auto-detect the input the first time
35
+
36
+
37
+ def forward(self, x):
38
+
39
+ """
40
+ It's the same of XLinear, only adds the ReLU in the middle and the filter layer
41
+ """
42
+
43
+ try:
44
+
45
+ x = f.linear(x, self.weights)
46
+
47
+ x = f.relu(x)
48
+
49
+ x = self.filter.forward(x)
50
+
51
+ return x
52
+
53
+ except ValueError as v:
54
+
55
+ raise LayersValueError(f"Value error during a XLayer forward function:\n{v}")
56
+
57
+ except TypeError as t:
58
+
59
+ raise LayersTypeError(f"Type error during a XLayer forward function:\n{t}")
60
+
61
+ except Exception as e:
62
+
63
+ raise XLayersError(f"Generic error during a XLayer forward function:\n{e}")
yeast/layers/linear.py ADDED
@@ -0,0 +1,148 @@
1
+ """
2
+ linear.py
3
+ Authors: Alessio Parolini
4
+ This file contains the resources to create a XLayer (a self expanding linear layer)
5
+ """
6
+
7
+ __all__ = ["XLinear"]
8
+
9
+ import torch
10
+
11
+ from torch import nn
12
+
13
+ from torch.nn import functional as f
14
+
15
+ from yeast.exceptions import XLayersError, LayersValueError, LayersTypeError
16
+
17
+
18
+ class XLinear(nn.Module):
19
+
20
+ """
21
+ This class creates a XLinear, abbreviation of expanding linear (layer).
22
+ This is a special linear layer, because it can expand its structure automatically,
23
+ both for user instructions and for the expansion of previous layers
24
+ """
25
+
26
+ def __init__(self, out_features:int, in_features:int | None = None, exp_rate:int = 1):
27
+
28
+ """
29
+ :param in_features: the number of starting inputs (with None it detects it by itself)
30
+ :param out_features: the number of out features (this must be entered!)
31
+ :param exp_rate: how much is the extension increased (for the optimizer)
32
+ """
33
+
34
+ super().__init__()
35
+
36
+ if in_features is not None and not isinstance(in_features, int):
37
+
38
+ raise LayersTypeError("Wrong argument: 'in_features' must be an integer")
39
+
40
+ self.in_features = in_features if in_features else 0
41
+
42
+ if not out_features:
43
+
44
+ raise LayersValueError("Missing argument: 'out_features' cannot be None!")
45
+
46
+ if not isinstance(out_features, int):
47
+
48
+ raise LayersTypeError("Wrong argument: 'out_features' must be a integer!")
49
+
50
+ self.out_features = out_features
51
+
52
+ if not isinstance(exp_rate, int):
53
+
54
+ raise LayersTypeError("Wrong argument: 'exp_rate' must be an integer!")
55
+
56
+ if exp_rate <= 0:
57
+
58
+ raise LayersValueError("Wrong value: 'exp_rate' must be higher than zero!")
59
+
60
+ self.exp_rate = exp_rate
61
+
62
+ self.weights = nn.Parameter(data=(torch.randn(self.out_features, self.in_features)))
63
+
64
+ self._expansion = 0
65
+
66
+ def expand(self, expansion: int)-> None:
67
+
68
+ """
69
+ Adds the new neurons, making expansion possible.
70
+ :expansion: the expansion is given by the SENN optimizer to choose how much expand the layer
71
+ :return: None
72
+ """
73
+
74
+ try:
75
+
76
+ self._expansion = expansion * self.exp_rate
77
+
78
+ new_neurons = torch.zeros(self._expansion, self.in_features, device=self.weights.device, dtype=self.weights.dtype) # new neurons creating
79
+
80
+ self.weights = nn.Parameter(data=torch.cat([self.weights, new_neurons], dim=0)) # add them to the layers weights
81
+
82
+ self.out_features = self.weights.size(0) # out_features updating
83
+
84
+ except ValueError as v:
85
+
86
+ raise LayersValueError(f"Value error during a XLayer expansion:\n{v}")
87
+
88
+ except TypeError as t:
89
+
90
+ raise LayersTypeError(f"Type error during a XLayer expansion:\n{t}")
91
+
92
+ except Exception as e:
93
+
94
+ raise XLayersError(f"Generic error during a XLayer expansion:\n{e}")
95
+
96
+ def _input_adapting(self, x)-> None:
97
+
98
+ """
99
+ Detect if the input tensor is different (because of a previous layer expansion) and adapt the structure
100
+ to avoid crashes.
101
+ :param x: the input tensor
102
+ :return: None
103
+ """
104
+
105
+ x_size = x.size(1) # get the number of inputs in the input tensor
106
+
107
+ extra_size = x_size - self.in_features # check if are more than the previous epoch
108
+
109
+ if extra_size == 0: # if it's not return
110
+
111
+ return
112
+
113
+ new_param = torch.zeros(self.weights.size(0), extra_size, device=self.weights.device, dtype=self.weights.dtype)
114
+
115
+ self.weights = nn.Parameter(torch.cat([self.weights, new_param], dim=1)) # add new weights
116
+
117
+ self.in_features = self.weights.size(1) # update the in_features value
118
+
119
+ def forward(self, x):
120
+
121
+ """
122
+ Get the input and process it using the layer parameters.
123
+ It auto-adapts itself if the X isn't the same of the precedent epoch (for example: a previous layer expanded itself)
124
+ :param x: the input tensor
125
+ :return: the layer output
126
+ """
127
+
128
+ try:
129
+
130
+ self._input_adapting(x)
131
+
132
+ x = f.linear(x, self.weights)
133
+
134
+ self._expansion = 0
135
+
136
+ return x
137
+
138
+ except ValueError as v:
139
+
140
+ raise LayersValueError(f"Value error during a XLayer forward function:\n{v}")
141
+
142
+ except TypeError as t:
143
+
144
+ raise LayersTypeError(f"Type error during a XLayer forward function:\n{t}")
145
+
146
+ except Exception as e:
147
+
148
+ raise XLayersError(f"Generic error during a XLayer forward function:\n{e}")
@@ -0,0 +1,47 @@
1
+ """
2
+ _01_tensor.py
3
+ This file contains exercises I've done for learn how to really use PyTorch Tensors
4
+ following documentation at: https://docs.pytorch.org/tutorials/beginner/basics/tensorqs_tutorial.html
5
+ """
6
+ import numpy as np
7
+ import torch
8
+
9
+ print("Playground: 01 Tensors and datas\n")
10
+
11
+ # creating a list and converting it into a tensor
12
+
13
+ data = [[1,2],[3,4]]
14
+
15
+ data_tensor = torch.tensor(data)
16
+
17
+ print(f"\nThis is a tensor: \n{data_tensor}\n")
18
+
19
+ # creating a numpy array and converting it into a tensor
20
+
21
+ np_data = np.array([[1,2],[3,4],[5,6]])
22
+
23
+ np_tensor = torch.from_numpy(np_data)
24
+
25
+ print(f"\nThis is a Numpy array: \n{np_data}\n and this is a tensor from a that Numpy array: \n{np_tensor}\n")
26
+
27
+ # creating two tensor with ones and random numbers
28
+
29
+ ones_tensor = torch.ones_like(data_tensor) # gets the properties from data_tensor
30
+
31
+ rand_tensor = torch.rand_like(data_tensor, dtype=torch.float) # this tensor has also a specific data type
32
+
33
+ print(f"\nThis is a ones tensor: \n{ones_tensor}.\nThis is instead a random generated one: \n{rand_tensor}\nThey are both generated from the properties of the first tensor.\n")
34
+
35
+ # creating tensors, not from an existing one
36
+
37
+ shape = (3, 2)
38
+
39
+ new_rand_ten = torch.rand(shape)
40
+
41
+ new_zeros_ten = torch.zeros(shape)
42
+
43
+ new_ones_ten = torch.ones(shape)
44
+
45
+ print(f"\nThis is a random tensor created without starting form an existing one: \n{new_rand_ten}\nThe same, but with zeros: \n{new_zeros_ten}\nand ones: \n{new_ones_ten}\n")
46
+
47
+ print(f"\nThese are some of the attributes of the first tensor:\nThe shape: {data_tensor.shape}\nThe data type: {data_tensor.dtype}\n And is stored on: {data_tensor.device}\n")
@@ -0,0 +1,46 @@
1
+ """
2
+ _02_tensor.py
3
+ This file contains exercises I've done for learn how to really use PyTorch Tensors operations
4
+ following documentation at: https://docs.pytorch.org/tutorials/beginner/basics/tensorqs_tutorial.html
5
+ """
6
+
7
+ import torch
8
+
9
+ print("Let's do some operations with Tensors!")
10
+
11
+ # creating 2 random Tensors with integer values from 0 to 9
12
+
13
+ a_tensor = torch.randint(0, 10, (2,3), dtype=torch.int8)
14
+
15
+ b_tensor = torch.randint(0, 10, (2,3), dtype=torch.int8)
16
+
17
+ print(f"""\nThese are two Tensors:\n
18
+ A = {a_tensor}\n
19
+ B = {b_tensor}\n""")
20
+
21
+ print(f"""
22
+ \nWe can slice them:\n
23
+ First row: A= {a_tensor[0]}, B= {b_tensor[0]}\n
24
+ First column: A = {a_tensor[:,0]}, B = {b_tensor[:,0]}""")
25
+
26
+ print(f"""
27
+ We can concat them:\n
28
+ {torch.cat([a_tensor, b_tensor], dim=1)}""")
29
+
30
+ matmul_result = a_tensor @ b_tensor.T # T stands for "transpose" because number of columns in the first must be the same of rows of the second (m, p) @ (p, n) = (m, n)
31
+
32
+ mul_result = a_tensor * b_tensor # cause they must have the same shape
33
+
34
+ print(f"""
35
+ There are 2 types of multiplication:\n
36
+ @ and *.
37
+ The first one does a real matrix multiplication (I transposed the second Tensor):\n
38
+ {matmul_result}\n
39
+ And the second a element-by-element multiplication:\n
40
+ {mul_result}""")
41
+
42
+ print(f"""
43
+ You can also do an simple operations and store the result directly in the operand.\n
44
+ Like add the same value to all numbers:\n
45
+ {a_tensor.add_(5)}""")
46
+
File without changes
@@ -0,0 +1,3 @@
1
+ from yeast.utils.randomdata import *
2
+
3
+ __all__ = ["random_xor"]
@@ -0,0 +1,23 @@
1
+ """
2
+ randomdata.py
3
+ Contains the function to generate random datas for test trainings
4
+ """
5
+
6
+ import torch
7
+
8
+ __all__ = ["random_xor"]
9
+
10
+
11
+ def random_xor(values: int = 2)-> tuple[torch.Tensor, torch.Tensor]:
12
+
13
+ """
14
+ Generates a random tensor and use it to create an XOR pattern.
15
+ Has more data than the used ones for a real training experience.
16
+ :return: the two tensors
17
+ """
18
+
19
+ x = torch.randn(1000, 20)
20
+
21
+ y = ((x[:, :values] > 0).sum(dim=1) % 2 == 1).float()
22
+
23
+ return x, y.unsqueeze(1)