tinymeow 0.1.0__tar.gz
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.
- tinymeow-0.1.0/PKG-INFO +45 -0
- tinymeow-0.1.0/README.md +37 -0
- tinymeow-0.1.0/activations/__init__.py +0 -0
- tinymeow-0.1.0/activations/relu.py +6 -0
- tinymeow-0.1.0/activations/sigmoid.py +7 -0
- tinymeow-0.1.0/activations/softmax.py +14 -0
- tinymeow-0.1.0/activations/tanh.py +6 -0
- tinymeow-0.1.0/loss_funtions/Bce.py +19 -0
- tinymeow-0.1.0/loss_funtions/__init__.py +4 -0
- tinymeow-0.1.0/loss_funtions/mse.py +7 -0
- tinymeow-0.1.0/neuralnet.py +217 -0
- tinymeow-0.1.0/pyproject.toml +18 -0
- tinymeow-0.1.0/setup.cfg +4 -0
- tinymeow-0.1.0/tinymeow/__init__.py +23 -0
- tinymeow-0.1.0/tinymeow.egg-info/PKG-INFO +45 -0
- tinymeow-0.1.0/tinymeow.egg-info/SOURCES.txt +17 -0
- tinymeow-0.1.0/tinymeow.egg-info/dependency_links.txt +1 -0
- tinymeow-0.1.0/tinymeow.egg-info/requires.txt +1 -0
- tinymeow-0.1.0/tinymeow.egg-info/top_level.txt +4 -0
tinymeow-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinymeow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small NumPy-based neural network library
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: numpy>=1.24
|
|
8
|
+
|
|
9
|
+
# tinymeow
|
|
10
|
+
|
|
11
|
+
`tinymeow` is a small NumPy-based neural network library with common activation
|
|
12
|
+
functions, mean squared error, and binary cross-entropy.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install tinymeow
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import numpy as np
|
|
24
|
+
from tinymeow import bce, neuralnet, sigmoid
|
|
25
|
+
|
|
26
|
+
model = neuralnet([2, 4, 1], output_activation=sigmoid, loss=bce)
|
|
27
|
+
model.init_params(seed=1)
|
|
28
|
+
|
|
29
|
+
features = np.array([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
|
|
30
|
+
labels = np.array([[0.0], [1.0], [1.0], [0.0]])
|
|
31
|
+
|
|
32
|
+
model.train(features, labels, epochs=100)
|
|
33
|
+
predictions = model.predict(features)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Save a model
|
|
37
|
+
|
|
38
|
+
Save the model configuration and learned parameters to a `.tm` file:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
model.model_save("xor_model.tm")
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The file is a compressed NumPy archive containing JSON metadata and numeric
|
|
45
|
+
parameter arrays. Saving supports the built-in activations and loss functions.
|
tinymeow-0.1.0/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# tinymeow
|
|
2
|
+
|
|
3
|
+
`tinymeow` is a small NumPy-based neural network library with common activation
|
|
4
|
+
functions, mean squared error, and binary cross-entropy.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install tinymeow
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import numpy as np
|
|
16
|
+
from tinymeow import bce, neuralnet, sigmoid
|
|
17
|
+
|
|
18
|
+
model = neuralnet([2, 4, 1], output_activation=sigmoid, loss=bce)
|
|
19
|
+
model.init_params(seed=1)
|
|
20
|
+
|
|
21
|
+
features = np.array([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
|
|
22
|
+
labels = np.array([[0.0], [1.0], [1.0], [0.0]])
|
|
23
|
+
|
|
24
|
+
model.train(features, labels, epochs=100)
|
|
25
|
+
predictions = model.predict(features)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Save a model
|
|
29
|
+
|
|
30
|
+
Save the model configuration and learned parameters to a `.tm` file:
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
model.model_save("xor_model.tm")
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The file is a compressed NumPy archive containing JSON metadata and numeric
|
|
37
|
+
parameter arrays. Saving supports the built-in activations and loss functions.
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
def softmax(x):
|
|
3
|
+
values = np.asarray(x)
|
|
4
|
+
shifted = values - np.max(values, axis=-1, keepdims=True)
|
|
5
|
+
exponentials = np.exp(shifted)
|
|
6
|
+
return exponentials / np.sum(exponentials, axis=-1, keepdims=True)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def softmax_derivative(x):
|
|
10
|
+
probabilities = softmax(x)
|
|
11
|
+
identity = np.eye(probabilities.shape[-1])
|
|
12
|
+
return probabilities[..., :, np.newaxis] * (
|
|
13
|
+
identity - probabilities[..., np.newaxis, :]
|
|
14
|
+
)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def bce(y_true, y_pred):
|
|
5
|
+
y_true = np.asarray(y_true)
|
|
6
|
+
y_pred = np.asarray(y_pred, dtype=float)
|
|
7
|
+
epsilon = np.finfo(float).eps
|
|
8
|
+
y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
|
|
9
|
+
return -np.mean(
|
|
10
|
+
y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def bce_derivative(y_true, y_pred):
|
|
15
|
+
y_true = np.asarray(y_true)
|
|
16
|
+
y_pred = np.asarray(y_pred, dtype=float)
|
|
17
|
+
epsilon = np.finfo(float).eps
|
|
18
|
+
y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
|
|
19
|
+
return (y_pred - y_true) / (y_pred * (1 - y_pred) * y_true.size)
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from activations.relu import relu, relu_derivative
|
|
5
|
+
from activations.tanh import tanh, tanh_derivative
|
|
6
|
+
from activations.sigmoid import sigmoid, sigmoid_derivative
|
|
7
|
+
from activations.softmax import softmax
|
|
8
|
+
from loss_funtions.Bce import bce, bce_derivative
|
|
9
|
+
from loss_funtions.mse import mse, mse_derivative
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
_LOSS_DERIVATIVES = {
|
|
14
|
+
mse: mse_derivative,
|
|
15
|
+
bce: bce_derivative,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _bce_sigmoid_gradient(y_true, prediction):
|
|
20
|
+
return (prediction - y_true) / np.size(y_true)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
_FUSED_OUTPUT_GRADIENTS = {
|
|
24
|
+
(bce, sigmoid): _bce_sigmoid_gradient,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class neuralnet:
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
layers,
|
|
32
|
+
activation=relu,
|
|
33
|
+
output_activation=None,
|
|
34
|
+
loss=mse,
|
|
35
|
+
delta=0.01,
|
|
36
|
+
loss_derivative=None,
|
|
37
|
+
):
|
|
38
|
+
self.layers = layers
|
|
39
|
+
self.activation = activation
|
|
40
|
+
self.output_activation = output_activation
|
|
41
|
+
self.loss = loss
|
|
42
|
+
self.loss_derivative = (
|
|
43
|
+
loss_derivative
|
|
44
|
+
if loss_derivative is not None
|
|
45
|
+
else _LOSS_DERIVATIVES.get(loss)
|
|
46
|
+
)
|
|
47
|
+
self.fused_output_gradient = _FUSED_OUTPUT_GRADIENTS.get(
|
|
48
|
+
(loss, output_activation)
|
|
49
|
+
)
|
|
50
|
+
self.delta = delta
|
|
51
|
+
self.weights = []
|
|
52
|
+
self.bias = []
|
|
53
|
+
|
|
54
|
+
def init_params(self, seed=None):
|
|
55
|
+
rng = np.random.default_rng(seed)
|
|
56
|
+
self.weights = []
|
|
57
|
+
self.bias=[]
|
|
58
|
+
for i in range(len(self.layers)-1):
|
|
59
|
+
weight_matrix = rng.standard_normal(size=(self.layers[i], self.layers[i + 1])) * np.sqrt(2 / self.layers[i])
|
|
60
|
+
self.weights.append(weight_matrix)
|
|
61
|
+
bias_vector = np.zeros(self.layers[i+1])
|
|
62
|
+
self.bias.append(bias_vector)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def forward(self, x):
|
|
66
|
+
activations = [x]
|
|
67
|
+
for i in range(len(self.weights)):
|
|
68
|
+
z = np.dot(x, self.weights[i]) + self.bias[i]
|
|
69
|
+
if i == len(self.weights) - 1:
|
|
70
|
+
if self.output_activation is not None:
|
|
71
|
+
x = self.output_activation(z)
|
|
72
|
+
else:
|
|
73
|
+
x = z
|
|
74
|
+
else:
|
|
75
|
+
x = self.activation(z)
|
|
76
|
+
|
|
77
|
+
activations.append(x)
|
|
78
|
+
|
|
79
|
+
return activations
|
|
80
|
+
|
|
81
|
+
def predict(self, x):
|
|
82
|
+
return self.forward(x)[-1]
|
|
83
|
+
|
|
84
|
+
def model_save(self, filename="model.tm"):
|
|
85
|
+
path = Path(filename)
|
|
86
|
+
if not path.suffix:
|
|
87
|
+
path = path.with_suffix(".tm")
|
|
88
|
+
elif path.suffix.lower() != ".tm":
|
|
89
|
+
raise ValueError("Model files must use the .tm extension")
|
|
90
|
+
|
|
91
|
+
if (
|
|
92
|
+
len(self.weights) != len(self.layers) - 1
|
|
93
|
+
or len(self.bias) != len(self.layers) - 1
|
|
94
|
+
):
|
|
95
|
+
raise ValueError("Initialize the model parameters before saving")
|
|
96
|
+
|
|
97
|
+
activation_names = {
|
|
98
|
+
relu: "relu",
|
|
99
|
+
sigmoid: "sigmoid",
|
|
100
|
+
tanh: "tanh",
|
|
101
|
+
softmax: "softmax",
|
|
102
|
+
}
|
|
103
|
+
loss_names = {mse: "mse", bce: "bce"}
|
|
104
|
+
|
|
105
|
+
if self.activation not in activation_names:
|
|
106
|
+
raise ValueError("Saving requires a supported built-in activation")
|
|
107
|
+
if (
|
|
108
|
+
self.output_activation is not None
|
|
109
|
+
and self.output_activation not in activation_names
|
|
110
|
+
):
|
|
111
|
+
raise ValueError("Saving requires a supported built-in output activation")
|
|
112
|
+
if self.loss not in loss_names:
|
|
113
|
+
raise ValueError("Saving requires a supported built-in loss")
|
|
114
|
+
if self.loss_derivative is not _LOSS_DERIVATIVES[self.loss]:
|
|
115
|
+
raise ValueError("Saving custom loss derivatives is not supported")
|
|
116
|
+
|
|
117
|
+
metadata = {
|
|
118
|
+
"format_version": 1,
|
|
119
|
+
"layers": [int(size) for size in self.layers],
|
|
120
|
+
"activation": activation_names[self.activation],
|
|
121
|
+
"output_activation": (
|
|
122
|
+
activation_names[self.output_activation]
|
|
123
|
+
if self.output_activation is not None
|
|
124
|
+
else None
|
|
125
|
+
),
|
|
126
|
+
"loss": loss_names[self.loss],
|
|
127
|
+
"delta": float(self.delta),
|
|
128
|
+
}
|
|
129
|
+
arrays = {"metadata": np.asarray(json.dumps(metadata))}
|
|
130
|
+
for i, (weight, bias) in enumerate(zip(self.weights, self.bias)):
|
|
131
|
+
weight = np.asarray(weight)
|
|
132
|
+
bias = np.asarray(bias)
|
|
133
|
+
if not np.issubdtype(weight.dtype, np.number) or not np.issubdtype(
|
|
134
|
+
bias.dtype, np.number
|
|
135
|
+
):
|
|
136
|
+
raise ValueError("Model parameters must be numeric arrays")
|
|
137
|
+
arrays[f"weight_{i}"] = weight
|
|
138
|
+
arrays[f"bias_{i}"] = bias
|
|
139
|
+
|
|
140
|
+
with path.open("wb") as model_file:
|
|
141
|
+
np.savez_compressed(model_file, **arrays)
|
|
142
|
+
return path
|
|
143
|
+
|
|
144
|
+
def activation_derivative(self, activation, x):
|
|
145
|
+
if activation == relu:
|
|
146
|
+
return relu_derivative(x)
|
|
147
|
+
elif activation == sigmoid:
|
|
148
|
+
return sigmoid_derivative(x)
|
|
149
|
+
elif activation == tanh:
|
|
150
|
+
return tanh_derivative(x)
|
|
151
|
+
elif activation == softmax:
|
|
152
|
+
return np.diag(x) - np.outer(x, x)
|
|
153
|
+
else:
|
|
154
|
+
raise ValueError("Unsupported activation function")
|
|
155
|
+
|
|
156
|
+
def _loss_gradient(self, y_true, prediction):
|
|
157
|
+
if self.loss_derivative is None:
|
|
158
|
+
raise ValueError(
|
|
159
|
+
"A loss_derivative must be provided when using a custom loss"
|
|
160
|
+
)
|
|
161
|
+
return self.loss_derivative(y_true, prediction)
|
|
162
|
+
|
|
163
|
+
def _activation_gradient(self, activation, activation_output, error):
|
|
164
|
+
if activation is None:
|
|
165
|
+
return error
|
|
166
|
+
|
|
167
|
+
derivative = self.activation_derivative(activation, activation_output)
|
|
168
|
+
if activation == softmax:
|
|
169
|
+
return np.dot(derivative.T, error)
|
|
170
|
+
return error * derivative
|
|
171
|
+
|
|
172
|
+
def backprop(self, x, y, learning_rate=None):
|
|
173
|
+
|
|
174
|
+
if learning_rate is None:
|
|
175
|
+
learning_rate = self.delta
|
|
176
|
+
|
|
177
|
+
activations = self.forward(x)
|
|
178
|
+
|
|
179
|
+
prediction = activations[-1]
|
|
180
|
+
error = self._loss_gradient(y, prediction)
|
|
181
|
+
|
|
182
|
+
for i in reversed(range(len(self.weights))):
|
|
183
|
+
if i == len(self.weights) - 1:
|
|
184
|
+
activation = self.output_activation
|
|
185
|
+
else:
|
|
186
|
+
activation = self.activation
|
|
187
|
+
|
|
188
|
+
if i == len(self.weights) - 1 and self.fused_output_gradient is not None:
|
|
189
|
+
layer_delta = self.fused_output_gradient(y, prediction)
|
|
190
|
+
else:
|
|
191
|
+
layer_delta = self._activation_gradient(
|
|
192
|
+
activation, activations[i + 1], error
|
|
193
|
+
)
|
|
194
|
+
error = np.dot(layer_delta, self.weights[i].T)
|
|
195
|
+
self.weights[i] -= learning_rate * np.outer(activations[i], layer_delta)
|
|
196
|
+
self.bias[i] -= learning_rate * layer_delta
|
|
197
|
+
|
|
198
|
+
def calculate_loss(self, x, y):
|
|
199
|
+
prediction = self.forward(x)[-1]
|
|
200
|
+
return self.loss(y, prediction)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def train(self, X, Y, epochs=10, learning_rate=None):
|
|
204
|
+
if learning_rate is None:
|
|
205
|
+
learning_rate = self.delta
|
|
206
|
+
for epoch in range(epochs):
|
|
207
|
+
for x, y in zip(X, Y):
|
|
208
|
+
self.backprop(x, y, learning_rate)
|
|
209
|
+
|
|
210
|
+
epoch_loss = np.mean([self.calculate_loss(x, y) for x, y in zip(X, Y)])
|
|
211
|
+
print(f"Epoch {epoch + 1}/{epochs} | Loss: {epoch_loss:.6f}")
|
|
212
|
+
|
|
213
|
+
if __name__ == "__main__":
|
|
214
|
+
net = neuralnet([3, 5, 2])
|
|
215
|
+
net.init_params()
|
|
216
|
+
result = net.forward(np.array([1, 2, 3]))
|
|
217
|
+
print(result)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tinymeow"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A small NumPy-based neural network library"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
dependencies = ["numpy>=1.24"]
|
|
12
|
+
|
|
13
|
+
[tool.setuptools]
|
|
14
|
+
py-modules = ["neuralnet"]
|
|
15
|
+
|
|
16
|
+
[tool.setuptools.packages.find]
|
|
17
|
+
where = ["."]
|
|
18
|
+
include = ["tinymeow*", "activations*", "loss_funtions*"]
|
tinymeow-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from activations.relu import relu, relu_derivative
|
|
2
|
+
from activations.sigmoid import sigmoid, sigmoid_derivative
|
|
3
|
+
from activations.softmax import softmax, softmax_derivative
|
|
4
|
+
from activations.tanh import tanh, tanh_derivative
|
|
5
|
+
from loss_funtions.Bce import bce, bce_derivative
|
|
6
|
+
from loss_funtions.mse import mse, mse_derivative
|
|
7
|
+
from neuralnet import neuralnet
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"bce",
|
|
11
|
+
"bce_derivative",
|
|
12
|
+
"mse",
|
|
13
|
+
"mse_derivative",
|
|
14
|
+
"neuralnet",
|
|
15
|
+
"relu",
|
|
16
|
+
"relu_derivative",
|
|
17
|
+
"sigmoid",
|
|
18
|
+
"sigmoid_derivative",
|
|
19
|
+
"softmax",
|
|
20
|
+
"softmax_derivative",
|
|
21
|
+
"tanh",
|
|
22
|
+
"tanh_derivative",
|
|
23
|
+
]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinymeow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small NumPy-based neural network library
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: numpy>=1.24
|
|
8
|
+
|
|
9
|
+
# tinymeow
|
|
10
|
+
|
|
11
|
+
`tinymeow` is a small NumPy-based neural network library with common activation
|
|
12
|
+
functions, mean squared error, and binary cross-entropy.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install tinymeow
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import numpy as np
|
|
24
|
+
from tinymeow import bce, neuralnet, sigmoid
|
|
25
|
+
|
|
26
|
+
model = neuralnet([2, 4, 1], output_activation=sigmoid, loss=bce)
|
|
27
|
+
model.init_params(seed=1)
|
|
28
|
+
|
|
29
|
+
features = np.array([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
|
|
30
|
+
labels = np.array([[0.0], [1.0], [1.0], [0.0]])
|
|
31
|
+
|
|
32
|
+
model.train(features, labels, epochs=100)
|
|
33
|
+
predictions = model.predict(features)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Save a model
|
|
37
|
+
|
|
38
|
+
Save the model configuration and learned parameters to a `.tm` file:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
model.model_save("xor_model.tm")
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The file is a compressed NumPy archive containing JSON metadata and numeric
|
|
45
|
+
parameter arrays. Saving supports the built-in activations and loss functions.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
neuralnet.py
|
|
3
|
+
pyproject.toml
|
|
4
|
+
activations/__init__.py
|
|
5
|
+
activations/relu.py
|
|
6
|
+
activations/sigmoid.py
|
|
7
|
+
activations/softmax.py
|
|
8
|
+
activations/tanh.py
|
|
9
|
+
loss_funtions/Bce.py
|
|
10
|
+
loss_funtions/__init__.py
|
|
11
|
+
loss_funtions/mse.py
|
|
12
|
+
tinymeow/__init__.py
|
|
13
|
+
tinymeow.egg-info/PKG-INFO
|
|
14
|
+
tinymeow.egg-info/SOURCES.txt
|
|
15
|
+
tinymeow.egg-info/dependency_links.txt
|
|
16
|
+
tinymeow.egg-info/requires.txt
|
|
17
|
+
tinymeow.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
numpy>=1.24
|