NeuralEngine 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.
@@ -0,0 +1,24 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Eduardo Leitao da Cunha Opice Leao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice, this permission notice, and the following attribution clause shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ Attribution Clause:
16
+ Any public use, distribution, or derivative work of this software must give appropriate credit to the original developer, Prajjwal Pratap Shah, by including a visible acknowledgment in documentation, websites, or other materials accompanying the software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ exclude NeuralEngine.jpg
@@ -0,0 +1,275 @@
1
+ Metadata-Version: 2.1
2
+ Name: NeuralEngine
3
+ Version: 0.1.0
4
+ Summary: A framework/library for building and training neural networks.
5
+ Home-page: https://github.com/Prajjwal2404/NeuralEngine
6
+ Author: Prajjwal Pratap Shah
7
+ Author-email: Prajjwal Pratap Shah <prajjwalpratapshah@outlook.com>
8
+ Maintainer: Prajjwal Pratap Shah
9
+ License: MIT License
10
+
11
+ Copyright (c) 2023 Eduardo Leitao da Cunha Opice Leao
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice, this permission notice, and the following attribution clause shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ Attribution Clause:
24
+ Any public use, distribution, or derivative work of this software must give appropriate credit to the original developer, Prajjwal Pratap Shah, by including a visible acknowledgment in documentation, websites, or other materials accompanying the software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
33
+
34
+ Project-URL: Homepage, https://github.com/Prajjwal2404/NeuralEngine
35
+ Keywords: deep-learning,neural-networks,machine-learning,numpy,cupy,autograd
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Operating System :: OS Independent
38
+ Requires-Python: >=3.10
39
+ Description-Content-Type: text/markdown
40
+ License-File: LICENSE
41
+ Requires-Dist: numpy>=1.26.4
42
+ Provides-Extra: cuda
43
+ Requires-Dist: cupy-cuda12x>=12.0.0; extra == "cuda"
44
+
45
+ <p align="center">
46
+ <img src="./NeuralEngine.jpg" alt="NeuralEngine Cover" width="600" />
47
+ </p>
48
+
49
+ <p align="center">
50
+ <a href="https://github.com/Prajjwal2404/NeuralEngine/pulse" alt="Activity">
51
+ <img src="https://img.shields.io/github/commit-activity/m/Prajjwal2404/NeuralEngine" /></a>
52
+ <a href="https://github.com/Prajjwal2404/NeuralEngine/graphs/contributors" alt="Contributors">
53
+ <img src="https://img.shields.io/github/contributors/Prajjwal2404/NeuralEngine" /></a>
54
+ <a href="https://www.python.org/">
55
+ <img src="https://img.shields.io/badge/language-Python-blue">
56
+ </a>
57
+ <a href="mailto:prajjwalpratapshah@outlook.com">
58
+ <img src="https://img.shields.io/badge/-Email-red?style=flat-square&logo=gmail&logoColor=white">
59
+ </a>
60
+ <a href="https://www.linkedin.com/in/prajjwal2404/">
61
+ <img src="https://img.shields.io/badge/-Linkedin-blue?style=flat-square&logo=linkedin">
62
+ </a>
63
+ </p>
64
+
65
+
66
+ # NeuralEngine
67
+
68
+ A framework/library for building and training neural networks in Python. NeuralEngine provides core components for constructing, training, and evaluating neural networks, with support for both CPU and GPU (CUDA) acceleration. Designed for extensibility, performance, and ease of use, it is suitable for research, prototyping, and production.
69
+
70
+ ## Table of Contents
71
+ - [Features](#features)
72
+ - [Installation](#installation)
73
+ - [Example Usage](#example-usage)
74
+ - [Project Structure](#project-structure)
75
+ - [Capabilities & Documentation](#capabilities--documentation)
76
+ - [Contribution Guide](#contribution-guide)
77
+ - [License](#license)
78
+ - [Attribution](#attribution)
79
+
80
+ ## Features
81
+ - Custom tensor operations (CPU/GPU support via NumPy and optional CuPy)
82
+ - Configurable neural network layers (Linear, Flatten, etc.)
83
+ - Built-in loss functions, metrics, and optimizers
84
+ - Model class for easy training and evaluation
85
+ - Device management (CPU/CUDA)
86
+ - Utilities for deep learning workflows
87
+ - Autograd capabilities using dynamic computational graphs
88
+ - Extensible design for custom layers, losses, and optimizers
89
+
90
+ ## Installation
91
+ Install via pip:
92
+ ```bash
93
+ pip install NeuralEngine
94
+ ```
95
+ Or clone and install locally:
96
+ ```bash
97
+ pip install .
98
+ ```
99
+
100
+ ### Optional CUDA Support
101
+ To enable GPU acceleration, Install via pip:
102
+ ```bash
103
+ pip install NeuralEngine[cuda]
104
+ ```
105
+ Or install the optional dependency
106
+ ```bash
107
+ pip install cupy-cuda12x
108
+ ```
109
+
110
+ ## Example Usage
111
+ ```python
112
+ import neuralengine as ne
113
+
114
+ # Set device (CPU or CUDA)
115
+ ne.set_device(ne.Device.CUDA)
116
+
117
+ # Load your dataset (example: MNIST)
118
+ x_train, y_train, x_test, y_test = load_mnist_data()
119
+
120
+ y_train = ne.one_hot(y_train)
121
+ y_test = ne.one_hot(y_test)
122
+
123
+ # Build your model
124
+ model = ne.Model(
125
+ input_size=(28, 28),
126
+ optimizer=ne.Adam(),
127
+ loss=ne.CrossEntropy(),
128
+ metrics=ne.ClassificationMetrics()
129
+ )
130
+ model(
131
+ ne.Flatten(),
132
+ ne.Linear(64, activation=ne.RELU()),
133
+ ne.Linear(10, activation=ne.Softmax()),
134
+ )
135
+
136
+ # Train and evaluate
137
+ model.train(x_train, y_train, epochs=30, batch_size=10000)
138
+ result = model.eval(x_test, y_test)
139
+ ```
140
+
141
+ ## Project Structure
142
+ ```
143
+ neuralengine/
144
+ __init__.py
145
+ config.py
146
+ tensor.py
147
+ utils.py
148
+ nn/
149
+ __init__.py
150
+ layers.py
151
+ loss.py
152
+ metrics.py
153
+ model.py
154
+ optim.py
155
+ setup.py
156
+ requirements.txt
157
+ pyproject.toml
158
+ MANIFEST.in
159
+ LICENSE
160
+ README.md
161
+ ```
162
+
163
+ ## Capabilities & Documentation
164
+ NeuralEngine offers the following core capabilities:
165
+
166
+ ### Device Management
167
+ - `ne.set_device(device)`: Switch between CPU and GPU (CUDA) for computation.
168
+ - Device enum: `ne.Device.CPU`, `ne.Device.CUDA`.
169
+
170
+ ### Tensors & Autograd
171
+ - Custom tensor implementation supporting NumPy and CuPy backends.
172
+ - Automatic differentiation (autograd) using dynamic computational graphs for backpropagation.
173
+ - Supports gradients, parameter updates, and custom operations.
174
+ - Supported tensor operations:
175
+ - Arithmetic: `+`, `-`, `*`, `/`, `**` (power)
176
+ - Matrix multiplication: `@`
177
+ - Mathematical: `log`, `sqrt`, `exp`, `abs`
178
+ - Reductions: `sum`, `max`, `min`, `mean`, `var`
179
+ - Shape: `transpose`, `reshape`, `concatenate`, `stack`, `slice`
180
+ - Elementwise: `where`, `masked_fill`
181
+ - Comparison: `>`, `>=`, `<`, `<=`
182
+ - Utility: `zero_grad()` (reset gradients)
183
+ - Autograd: `backward()` (compute gradients for the computation graph)
184
+
185
+ ### Layers
186
+ - `ne.Flatten()`: Flattens input tensors to 2D (batch, features).
187
+ - `ne.Linear(out_features, activation=None)`: Fully connected layer with optional activation.
188
+ - `ne.LSTM(...)`: Long Short-Term Memory layer with options for attention, bidirectionality, sequence/state output. You can build deep LSTM networks by stacking multiple LSTM layers. When stacking, ensure that the hidden units for subsequent layers are set correctly:
189
+ - For a standard LSTM, the hidden state shape for the last timestep is `(batch, hidden_units)`.
190
+ - For a bidirectional LSTM, the hidden and cell state shape becomes `(batch, hidden_units * 2)`.
191
+ - If attention is enabled, the hidden state shape is `(batch, hidden_units + input_size[-1])`.
192
+ - If subsequent layers require state initializations from prior layers, set the hidden units accordingly to match the output shape of the previous LSTM (including adjustments for bidirectionality and attention).
193
+ - `ne.MultiplicativeAttention(units)`: Dot-product attention mechanism for sequence models.
194
+ - `ne.Embedding(embed_size, vocab_size, n_timesteps=None)`: Embedding layer for mapping indices to dense vectors, with optional positional encoding.
195
+ - `ne.LayerNorm(norm_shape, eps=1e-7)`: Layer normalization for stabilizing training.
196
+ - `ne.Dropout(prob=0.5)`: Dropout regularization for reducing overfitting.
197
+ - All layers inherit from a common base and support extensibility for custom architectures.
198
+
199
+ ### Activations
200
+ - `ne.Sigmoid()`: Sigmoid activation function.
201
+ - `ne.Tanh()`: Tanh activation function.
202
+ - `ne.RELU(alpha=0, parametric=False)`: ReLU, Leaky ReLU, or Parametric ReLU activation.
203
+ - `ne.Softmax(axis=-1)`: Softmax activation for classification tasks.
204
+ - All activations inherit from a common base and support extensibility for custom architectures.
205
+
206
+ ### Loss Functions
207
+ - `ne.CrossEntropy(binary=False, eps=1e-7)`: Categorical and binary cross-entropy loss for classification tasks.
208
+ - `ne.MSE()`: Mean Squared Error loss for regression.
209
+ - `ne.MAE()`: Mean Absolute Error loss for regression.
210
+ - `ne.Huber(delta=1.0)`: Huber loss, robust to outliers.
211
+ - `ne.GaussianNLL(eps=1e-7)`: Gaussian Negative Log Likelihood loss for probabilistic regression.
212
+ - `ne.KLDivergence(eps=1e-7)`: Kullback-Leibler Divergence loss for measuring distribution differences.
213
+ - All loss functions inherit from a common base and support autograd.
214
+
215
+ ### Optimizers
216
+ - `ne.Adam(lr=1e-3, betas=(0.9, 0.99), eps=1e-7, reg=0)`: Adam optimizer (switches to RMSProp if only one beta is provided).
217
+ - `ne.SGD(lr=1e-2, reg=0, momentum=0, nesterov=False)`: Stochastic Gradient Descent with optional momentum and Nesterov acceleration.
218
+ - All optimizers support L2 regularization and gradient reset.
219
+
220
+ ### Metrics
221
+ - `ne.ClassificationMetrics(num_classes=None, acc=True, prec=False, rec=False, f1=False, cm=False)`: Computes accuracy, precision, recall, F1 score, and confusion matrix for classification tasks.
222
+ - `ne.RMSE()`: Root Mean Squared Error for regression.
223
+ - `ne.R2()`: R2 Score for regression.
224
+ - All metrics return results as dictionaries and support batch evaluation.
225
+
226
+ ### Model API
227
+ - `ne.Model(input_size, optimizer, loss, metrics)`: Create a model specifying input size, optimizer, loss function, and metrics.
228
+ - Add layers by calling the model instance: `model(layer1, layer2, ...)` or using `model.build(layer1, layer2, ...)`.
229
+ - `model.train(x, y, epochs=10, batch_size=64, random_seed=None)`: Train the model on data, with support for batching, shuffling, and metric/loss reporting per epoch.
230
+ - `model.eval(x, y)`: Evaluate the model on data, prints loss and metrics, and returns output tensor. Also prints confusion matrix if enabled in metrics.
231
+ - Layers are set to training or evaluation mode automatically during `train` and `eval`.
232
+
233
+ ### Utilities
234
+ - Tensor creation: `tensor(data, requires_grad=False)`, `zeros(shape)`, `ones(shape)`, `rand(shape)`, `randn(shape, xavier=False)`, `randint(low, high, shape)` and their `_like` variants for matching shapes.
235
+ - Tensor operations: `sum`, `max`, `min`, `mean`, `var`, `log`, `sqrt`, `exp`, `abs`, `concat`, `stack`, `where`, `clip` for elementwise and reduction operations.
236
+ - Encoding: `one_hot(labels, num_classes=None)` for converting integer labels to one-hot encoding.
237
+
238
+ ### Extensibility
239
+ NeuralEngine is designed for easy extension and customization:
240
+ - **Custom Layers**: Create new layers by inheriting from the `Layer` base class and implementing the `forward(self, x)` method. You can add parameters, initialization logic, and custom computations as needed. All built-in layers follow this pattern, making it simple to add your own.
241
+ - **Custom Losses**: Define new loss functions by inheriting from the `Loss` base class and implementing the `compute(self, z, y)` method. This allows you to integrate any custom loss logic with autograd support.
242
+ - **Custom Optimizers**: Implement new optimization algorithms by inheriting from the `Optimizer` base class and providing your own `step(self)` method. You can manage optimizer state and parameter updates as required.
243
+ - **Custom Metrics**: Add new metrics by inheriting from the `Metric` base class and implementing the `compute(self, z, y)` method. Alternatively, you can pass a function of the form `func(x, y) -> dict[str, float | np.ndarray]` directly to the model's metrics argument for flexible evaluation.
244
+ - All core components are modular and can be replaced or extended for research, experimentation, or production use.
245
+
246
+ ## Contribution Guide
247
+
248
+ NeuralEngine is an open-source project, and I warmly welcome all kinds of contributions whether it's code, documentation, bug reports, feature ideas, or sharing cool examples. If you want to help make NeuralEngine better, you're in the right place!
249
+
250
+ ### How to Contribute
251
+ - **Fork the repository** and create a new branch for your feature, fix, or documentation update.
252
+ - **Keep it clean and consistent**: Try to follow the existing code style, naming conventions, and documentation patterns. Well-commented, readable code is always appreciated!
253
+ - **Add tests** for new features or bug fixes if you can.
254
+ - **Document your changes**: Update or add docstrings and README sections so others can easily understand your work.
255
+ - **Open a pull request** describing what you've changed and why it's awesome.
256
+
257
+ ### What Can You Contribute?
258
+ - New layers, loss functions, optimizers, metrics, or utility functions
259
+ - Improvements to existing components
260
+ - Bug fixes and performance tweaks
261
+ - Documentation updates and tutorials
262
+ - Example scripts and notebooks
263
+ - Feature requests, feedback, and ideas
264
+
265
+ Every contribution is reviewed for quality and consistency, but don't worry—if you have questions or need help, just open an issue or start a discussion. I'm happy to help and love seeing new faces in the community!
266
+
267
+ Thanks for making NeuralEngine better, together! 🚀
268
+
269
+ ## License
270
+ MIT License with attribution clause. See LICENSE file for details.
271
+
272
+ ## Attribution
273
+ If you use this project, please credit the original developer: Prajjwal Pratap Shah.
274
+
275
+ Special thanks to the Autograd Framework From Scratch project by Eduardo Leitão da Cunha Opice Leão, which served as a reference for tensor operations and autograd implementations.
@@ -0,0 +1,20 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ setup.py
6
+ NeuralEngine.egg-info/PKG-INFO
7
+ NeuralEngine.egg-info/SOURCES.txt
8
+ NeuralEngine.egg-info/dependency_links.txt
9
+ NeuralEngine.egg-info/requires.txt
10
+ NeuralEngine.egg-info/top_level.txt
11
+ neuralengine/__init__.py
12
+ neuralengine/config.py
13
+ neuralengine/tensor.py
14
+ neuralengine/utils.py
15
+ neuralengine/nn/__init__.py
16
+ neuralengine/nn/layers.py
17
+ neuralengine/nn/loss.py
18
+ neuralengine/nn/metrics.py
19
+ neuralengine/nn/model.py
20
+ neuralengine/nn/optim.py
@@ -0,0 +1,4 @@
1
+ numpy>=1.26.4
2
+
3
+ [cuda]
4
+ cupy-cuda12x>=12.0.0
@@ -0,0 +1 @@
1
+ neuralengine
@@ -0,0 +1,275 @@
1
+ Metadata-Version: 2.1
2
+ Name: NeuralEngine
3
+ Version: 0.1.0
4
+ Summary: A framework/library for building and training neural networks.
5
+ Home-page: https://github.com/Prajjwal2404/NeuralEngine
6
+ Author: Prajjwal Pratap Shah
7
+ Author-email: Prajjwal Pratap Shah <prajjwalpratapshah@outlook.com>
8
+ Maintainer: Prajjwal Pratap Shah
9
+ License: MIT License
10
+
11
+ Copyright (c) 2023 Eduardo Leitao da Cunha Opice Leao
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice, this permission notice, and the following attribution clause shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ Attribution Clause:
24
+ Any public use, distribution, or derivative work of this software must give appropriate credit to the original developer, Prajjwal Pratap Shah, by including a visible acknowledgment in documentation, websites, or other materials accompanying the software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
33
+
34
+ Project-URL: Homepage, https://github.com/Prajjwal2404/NeuralEngine
35
+ Keywords: deep-learning,neural-networks,machine-learning,numpy,cupy,autograd
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Operating System :: OS Independent
38
+ Requires-Python: >=3.10
39
+ Description-Content-Type: text/markdown
40
+ License-File: LICENSE
41
+ Requires-Dist: numpy>=1.26.4
42
+ Provides-Extra: cuda
43
+ Requires-Dist: cupy-cuda12x>=12.0.0; extra == "cuda"
44
+
45
+ <p align="center">
46
+ <img src="./NeuralEngine.jpg" alt="NeuralEngine Cover" width="600" />
47
+ </p>
48
+
49
+ <p align="center">
50
+ <a href="https://github.com/Prajjwal2404/NeuralEngine/pulse" alt="Activity">
51
+ <img src="https://img.shields.io/github/commit-activity/m/Prajjwal2404/NeuralEngine" /></a>
52
+ <a href="https://github.com/Prajjwal2404/NeuralEngine/graphs/contributors" alt="Contributors">
53
+ <img src="https://img.shields.io/github/contributors/Prajjwal2404/NeuralEngine" /></a>
54
+ <a href="https://www.python.org/">
55
+ <img src="https://img.shields.io/badge/language-Python-blue">
56
+ </a>
57
+ <a href="mailto:prajjwalpratapshah@outlook.com">
58
+ <img src="https://img.shields.io/badge/-Email-red?style=flat-square&logo=gmail&logoColor=white">
59
+ </a>
60
+ <a href="https://www.linkedin.com/in/prajjwal2404/">
61
+ <img src="https://img.shields.io/badge/-Linkedin-blue?style=flat-square&logo=linkedin">
62
+ </a>
63
+ </p>
64
+
65
+
66
+ # NeuralEngine
67
+
68
+ A framework/library for building and training neural networks in Python. NeuralEngine provides core components for constructing, training, and evaluating neural networks, with support for both CPU and GPU (CUDA) acceleration. Designed for extensibility, performance, and ease of use, it is suitable for research, prototyping, and production.
69
+
70
+ ## Table of Contents
71
+ - [Features](#features)
72
+ - [Installation](#installation)
73
+ - [Example Usage](#example-usage)
74
+ - [Project Structure](#project-structure)
75
+ - [Capabilities & Documentation](#capabilities--documentation)
76
+ - [Contribution Guide](#contribution-guide)
77
+ - [License](#license)
78
+ - [Attribution](#attribution)
79
+
80
+ ## Features
81
+ - Custom tensor operations (CPU/GPU support via NumPy and optional CuPy)
82
+ - Configurable neural network layers (Linear, Flatten, etc.)
83
+ - Built-in loss functions, metrics, and optimizers
84
+ - Model class for easy training and evaluation
85
+ - Device management (CPU/CUDA)
86
+ - Utilities for deep learning workflows
87
+ - Autograd capabilities using dynamic computational graphs
88
+ - Extensible design for custom layers, losses, and optimizers
89
+
90
+ ## Installation
91
+ Install via pip:
92
+ ```bash
93
+ pip install NeuralEngine
94
+ ```
95
+ Or clone and install locally:
96
+ ```bash
97
+ pip install .
98
+ ```
99
+
100
+ ### Optional CUDA Support
101
+ To enable GPU acceleration, Install via pip:
102
+ ```bash
103
+ pip install NeuralEngine[cuda]
104
+ ```
105
+ Or install the optional dependency
106
+ ```bash
107
+ pip install cupy-cuda12x
108
+ ```
109
+
110
+ ## Example Usage
111
+ ```python
112
+ import neuralengine as ne
113
+
114
+ # Set device (CPU or CUDA)
115
+ ne.set_device(ne.Device.CUDA)
116
+
117
+ # Load your dataset (example: MNIST)
118
+ x_train, y_train, x_test, y_test = load_mnist_data()
119
+
120
+ y_train = ne.one_hot(y_train)
121
+ y_test = ne.one_hot(y_test)
122
+
123
+ # Build your model
124
+ model = ne.Model(
125
+ input_size=(28, 28),
126
+ optimizer=ne.Adam(),
127
+ loss=ne.CrossEntropy(),
128
+ metrics=ne.ClassificationMetrics()
129
+ )
130
+ model(
131
+ ne.Flatten(),
132
+ ne.Linear(64, activation=ne.RELU()),
133
+ ne.Linear(10, activation=ne.Softmax()),
134
+ )
135
+
136
+ # Train and evaluate
137
+ model.train(x_train, y_train, epochs=30, batch_size=10000)
138
+ result = model.eval(x_test, y_test)
139
+ ```
140
+
141
+ ## Project Structure
142
+ ```
143
+ neuralengine/
144
+ __init__.py
145
+ config.py
146
+ tensor.py
147
+ utils.py
148
+ nn/
149
+ __init__.py
150
+ layers.py
151
+ loss.py
152
+ metrics.py
153
+ model.py
154
+ optim.py
155
+ setup.py
156
+ requirements.txt
157
+ pyproject.toml
158
+ MANIFEST.in
159
+ LICENSE
160
+ README.md
161
+ ```
162
+
163
+ ## Capabilities & Documentation
164
+ NeuralEngine offers the following core capabilities:
165
+
166
+ ### Device Management
167
+ - `ne.set_device(device)`: Switch between CPU and GPU (CUDA) for computation.
168
+ - Device enum: `ne.Device.CPU`, `ne.Device.CUDA`.
169
+
170
+ ### Tensors & Autograd
171
+ - Custom tensor implementation supporting NumPy and CuPy backends.
172
+ - Automatic differentiation (autograd) using dynamic computational graphs for backpropagation.
173
+ - Supports gradients, parameter updates, and custom operations.
174
+ - Supported tensor operations:
175
+ - Arithmetic: `+`, `-`, `*`, `/`, `**` (power)
176
+ - Matrix multiplication: `@`
177
+ - Mathematical: `log`, `sqrt`, `exp`, `abs`
178
+ - Reductions: `sum`, `max`, `min`, `mean`, `var`
179
+ - Shape: `transpose`, `reshape`, `concatenate`, `stack`, `slice`
180
+ - Elementwise: `where`, `masked_fill`
181
+ - Comparison: `>`, `>=`, `<`, `<=`
182
+ - Utility: `zero_grad()` (reset gradients)
183
+ - Autograd: `backward()` (compute gradients for the computation graph)
184
+
185
+ ### Layers
186
+ - `ne.Flatten()`: Flattens input tensors to 2D (batch, features).
187
+ - `ne.Linear(out_features, activation=None)`: Fully connected layer with optional activation.
188
+ - `ne.LSTM(...)`: Long Short-Term Memory layer with options for attention, bidirectionality, sequence/state output. You can build deep LSTM networks by stacking multiple LSTM layers. When stacking, ensure that the hidden units for subsequent layers are set correctly:
189
+ - For a standard LSTM, the hidden state shape for the last timestep is `(batch, hidden_units)`.
190
+ - For a bidirectional LSTM, the hidden and cell state shape becomes `(batch, hidden_units * 2)`.
191
+ - If attention is enabled, the hidden state shape is `(batch, hidden_units + input_size[-1])`.
192
+ - If subsequent layers require state initializations from prior layers, set the hidden units accordingly to match the output shape of the previous LSTM (including adjustments for bidirectionality and attention).
193
+ - `ne.MultiplicativeAttention(units)`: Dot-product attention mechanism for sequence models.
194
+ - `ne.Embedding(embed_size, vocab_size, n_timesteps=None)`: Embedding layer for mapping indices to dense vectors, with optional positional encoding.
195
+ - `ne.LayerNorm(norm_shape, eps=1e-7)`: Layer normalization for stabilizing training.
196
+ - `ne.Dropout(prob=0.5)`: Dropout regularization for reducing overfitting.
197
+ - All layers inherit from a common base and support extensibility for custom architectures.
198
+
199
+ ### Activations
200
+ - `ne.Sigmoid()`: Sigmoid activation function.
201
+ - `ne.Tanh()`: Tanh activation function.
202
+ - `ne.RELU(alpha=0, parametric=False)`: ReLU, Leaky ReLU, or Parametric ReLU activation.
203
+ - `ne.Softmax(axis=-1)`: Softmax activation for classification tasks.
204
+ - All activations inherit from a common base and support extensibility for custom architectures.
205
+
206
+ ### Loss Functions
207
+ - `ne.CrossEntropy(binary=False, eps=1e-7)`: Categorical and binary cross-entropy loss for classification tasks.
208
+ - `ne.MSE()`: Mean Squared Error loss for regression.
209
+ - `ne.MAE()`: Mean Absolute Error loss for regression.
210
+ - `ne.Huber(delta=1.0)`: Huber loss, robust to outliers.
211
+ - `ne.GaussianNLL(eps=1e-7)`: Gaussian Negative Log Likelihood loss for probabilistic regression.
212
+ - `ne.KLDivergence(eps=1e-7)`: Kullback-Leibler Divergence loss for measuring distribution differences.
213
+ - All loss functions inherit from a common base and support autograd.
214
+
215
+ ### Optimizers
216
+ - `ne.Adam(lr=1e-3, betas=(0.9, 0.99), eps=1e-7, reg=0)`: Adam optimizer (switches to RMSProp if only one beta is provided).
217
+ - `ne.SGD(lr=1e-2, reg=0, momentum=0, nesterov=False)`: Stochastic Gradient Descent with optional momentum and Nesterov acceleration.
218
+ - All optimizers support L2 regularization and gradient reset.
219
+
220
+ ### Metrics
221
+ - `ne.ClassificationMetrics(num_classes=None, acc=True, prec=False, rec=False, f1=False, cm=False)`: Computes accuracy, precision, recall, F1 score, and confusion matrix for classification tasks.
222
+ - `ne.RMSE()`: Root Mean Squared Error for regression.
223
+ - `ne.R2()`: R2 Score for regression.
224
+ - All metrics return results as dictionaries and support batch evaluation.
225
+
226
+ ### Model API
227
+ - `ne.Model(input_size, optimizer, loss, metrics)`: Create a model specifying input size, optimizer, loss function, and metrics.
228
+ - Add layers by calling the model instance: `model(layer1, layer2, ...)` or using `model.build(layer1, layer2, ...)`.
229
+ - `model.train(x, y, epochs=10, batch_size=64, random_seed=None)`: Train the model on data, with support for batching, shuffling, and metric/loss reporting per epoch.
230
+ - `model.eval(x, y)`: Evaluate the model on data, prints loss and metrics, and returns output tensor. Also prints confusion matrix if enabled in metrics.
231
+ - Layers are set to training or evaluation mode automatically during `train` and `eval`.
232
+
233
+ ### Utilities
234
+ - Tensor creation: `tensor(data, requires_grad=False)`, `zeros(shape)`, `ones(shape)`, `rand(shape)`, `randn(shape, xavier=False)`, `randint(low, high, shape)` and their `_like` variants for matching shapes.
235
+ - Tensor operations: `sum`, `max`, `min`, `mean`, `var`, `log`, `sqrt`, `exp`, `abs`, `concat`, `stack`, `where`, `clip` for elementwise and reduction operations.
236
+ - Encoding: `one_hot(labels, num_classes=None)` for converting integer labels to one-hot encoding.
237
+
238
+ ### Extensibility
239
+ NeuralEngine is designed for easy extension and customization:
240
+ - **Custom Layers**: Create new layers by inheriting from the `Layer` base class and implementing the `forward(self, x)` method. You can add parameters, initialization logic, and custom computations as needed. All built-in layers follow this pattern, making it simple to add your own.
241
+ - **Custom Losses**: Define new loss functions by inheriting from the `Loss` base class and implementing the `compute(self, z, y)` method. This allows you to integrate any custom loss logic with autograd support.
242
+ - **Custom Optimizers**: Implement new optimization algorithms by inheriting from the `Optimizer` base class and providing your own `step(self)` method. You can manage optimizer state and parameter updates as required.
243
+ - **Custom Metrics**: Add new metrics by inheriting from the `Metric` base class and implementing the `compute(self, z, y)` method. Alternatively, you can pass a function of the form `func(x, y) -> dict[str, float | np.ndarray]` directly to the model's metrics argument for flexible evaluation.
244
+ - All core components are modular and can be replaced or extended for research, experimentation, or production use.
245
+
246
+ ## Contribution Guide
247
+
248
+ NeuralEngine is an open-source project, and I warmly welcome all kinds of contributions whether it's code, documentation, bug reports, feature ideas, or sharing cool examples. If you want to help make NeuralEngine better, you're in the right place!
249
+
250
+ ### How to Contribute
251
+ - **Fork the repository** and create a new branch for your feature, fix, or documentation update.
252
+ - **Keep it clean and consistent**: Try to follow the existing code style, naming conventions, and documentation patterns. Well-commented, readable code is always appreciated!
253
+ - **Add tests** for new features or bug fixes if you can.
254
+ - **Document your changes**: Update or add docstrings and README sections so others can easily understand your work.
255
+ - **Open a pull request** describing what you've changed and why it's awesome.
256
+
257
+ ### What Can You Contribute?
258
+ - New layers, loss functions, optimizers, metrics, or utility functions
259
+ - Improvements to existing components
260
+ - Bug fixes and performance tweaks
261
+ - Documentation updates and tutorials
262
+ - Example scripts and notebooks
263
+ - Feature requests, feedback, and ideas
264
+
265
+ Every contribution is reviewed for quality and consistency, but don't worry—if you have questions or need help, just open an issue or start a discussion. I'm happy to help and love seeing new faces in the community!
266
+
267
+ Thanks for making NeuralEngine better, together! 🚀
268
+
269
+ ## License
270
+ MIT License with attribution clause. See LICENSE file for details.
271
+
272
+ ## Attribution
273
+ If you use this project, please credit the original developer: Prajjwal Pratap Shah.
274
+
275
+ Special thanks to the Autograd Framework From Scratch project by Eduardo Leitão da Cunha Opice Leão, which served as a reference for tensor operations and autograd implementations.