ym-pure-ml 0.7__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.
Files changed (42) hide show
  1. ym_pure_ml-0.7/CITATION.cff +20 -0
  2. ym_pure_ml-0.7/LICENSE +201 -0
  3. ym_pure_ml-0.7/MANIFEST.in +6 -0
  4. ym_pure_ml-0.7/NOTICE +4 -0
  5. ym_pure_ml-0.7/PKG-INFO +191 -0
  6. ym_pure_ml-0.7/README.md +159 -0
  7. ym_pure_ml-0.7/pureml/__init__.py +27 -0
  8. ym_pure_ml-0.7/pureml/activations.py +195 -0
  9. ym_pure_ml-0.7/pureml/base.py +305 -0
  10. ym_pure_ml-0.7/pureml/datasets/MNIST/__init__.py +5 -0
  11. ym_pure_ml-0.7/pureml/datasets/MNIST/dataset.py +60 -0
  12. ym_pure_ml-0.7/pureml/datasets/MNIST/files/__init__.py +1 -0
  13. ym_pure_ml-0.7/pureml/datasets/MNIST/files/mnist-28x28_uint8.zarr.zip +0 -0
  14. ym_pure_ml-0.7/pureml/datasets/__init__.py +5 -0
  15. ym_pure_ml-0.7/pureml/evaluation.py +52 -0
  16. ym_pure_ml-0.7/pureml/general_math.py +272 -0
  17. ym_pure_ml-0.7/pureml/layers.py +512 -0
  18. ym_pure_ml-0.7/pureml/logging_util.py +54 -0
  19. ym_pure_ml-0.7/pureml/losses.py +292 -0
  20. ym_pure_ml-0.7/pureml/machinery.py +834 -0
  21. ym_pure_ml-0.7/pureml/models/__init__.py +1 -0
  22. ym_pure_ml-0.7/pureml/models/classical/__init__.py +5 -0
  23. ym_pure_ml-0.7/pureml/models/classical/knn.py +109 -0
  24. ym_pure_ml-0.7/pureml/models/neural_networks/__init__.py +5 -0
  25. ym_pure_ml-0.7/pureml/models/neural_networks/mnist_beater.py +52 -0
  26. ym_pure_ml-0.7/pureml/optimizers.py +683 -0
  27. ym_pure_ml-0.7/pureml/training_utils.py +279 -0
  28. ym_pure_ml-0.7/pureml/util.py +815 -0
  29. ym_pure_ml-0.7/setup.cfg +4 -0
  30. ym_pure_ml-0.7/tests/test_activations.py +139 -0
  31. ym_pure_ml-0.7/tests/test_knn.py +58 -0
  32. ym_pure_ml-0.7/tests/test_layers.py +139 -0
  33. ym_pure_ml-0.7/tests/test_losses.py +140 -0
  34. ym_pure_ml-0.7/tests/test_machinery.py +213 -0
  35. ym_pure_ml-0.7/tests/test_optim_sched.py +188 -0
  36. ym_pure_ml-0.7/tests/test_storage.py +217 -0
  37. ym_pure_ml-0.7/ym_pure_ml.egg-info/PKG-INFO +191 -0
  38. ym_pure_ml-0.7/ym_pure_ml.egg-info/SOURCES.txt +41 -0
  39. ym_pure_ml-0.7/ym_pure_ml.egg-info/dependency_links.txt +1 -0
  40. ym_pure_ml-0.7/ym_pure_ml.egg-info/not-zip-safe +1 -0
  41. ym_pure_ml-0.7/ym_pure_ml.egg-info/requires.txt +2 -0
  42. ym_pure_ml-0.7/ym_pure_ml.egg-info/top_level.txt +1 -0
@@ -0,0 +1,20 @@
1
+ cff-version: 1.2.0
2
+ title: "PureML: a lightweight, NumPy-based deep learning framework"
3
+ message: "If you use PureML in your work, please cite this software."
4
+ type: software
5
+ authors:
6
+ - family-names: Mishchyriak
7
+ given-names: Yehor
8
+ repository-code: https://github.com/Yehor-Mishchyriak/PureML
9
+ url: https://github.com/Yehor-Mishchyriak/PureML
10
+ license: Apache-2.0
11
+ version: 1.0.0
12
+ release-date: 2025-10-12
13
+ keywords: ["deep learning", "numpy", "autodiff", "education"]
14
+ preferred-citation:
15
+ type: software
16
+ title: "PureML"
17
+ authors:
18
+ - family-names: Mishchyriak
19
+ given-names: Yehor
20
+ url: https://github.com/Yehor-Mishchyriak/PureML
ym_pure_ml-0.7/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2025 Yehor Mishchyriak
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include README.MD
3
+ include LICENSE*
4
+ include NOTICE*
5
+ include CITATION.cff
6
+ recursive-include pureml/datasets/MNIST/files *.zarr.zip
ym_pure_ml-0.7/NOTICE ADDED
@@ -0,0 +1,4 @@
1
+ PureML
2
+ Copyright (c) 2025 Yehor Mishchyriak
3
+
4
+ This product includes software developed by Yehor Mishchyriak.
@@ -0,0 +1,191 @@
1
+ Metadata-Version: 2.4
2
+ Name: ym-pure-ml
3
+ Version: 0.7
4
+ Summary: Transparent, NumPy-only deep learning framework for teaching, small-scale projects, prototyping, and reproducible experiments.
5
+ Author: Yehor Mishchyriak
6
+ License: Apache-2.0
7
+ Project-URL: Source, https://github.com/Yehor-Mishchyriak/PureML
8
+ Project-URL: Tracker, https://github.com/Yehor-Mishchyriak/PureML/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Requires-Python: >=3.11
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ License-File: NOTICE
20
+ Requires-Dist: numpy>=2.0
21
+ Requires-Dist: zarr>=3.1
22
+ Dynamic: author
23
+ Dynamic: classifier
24
+ Dynamic: description
25
+ Dynamic: description-content-type
26
+ Dynamic: license
27
+ Dynamic: license-file
28
+ Dynamic: project-url
29
+ Dynamic: requires-dist
30
+ Dynamic: requires-python
31
+ Dynamic: summary
32
+
33
+ # PureML — a tiny, transparent deep-learning framework in NumPy
34
+
35
+ PureML is a learning-friendly deep-learning framework built entirely on top of **NumPy**. It aims to be **small, readable, and hackable** while still being practical for real experiments and teaching.
36
+
37
+ - **No hidden magic** — a minimal autodiff Tensor + clear VJPs
38
+ - **Batteries included** — core layers (Affine, Dropout, BatchNorm1d), common losses, an SGD optimizer, and a simple `DataLoader`
39
+ - **Self-contained dataset demo** — a ready-to-use MNIST reader and an end-to-end “MNIST Beater” model
40
+ - **Portable persistence** — zarr-backed `ArrayStorage` with zip compression for saving/loading model state
41
+
42
+ > If you like **scikit-learn’s** simplicity and wish **deep learning** felt the same way for small/medium projects, PureML is for you.
43
+
44
+ ---
45
+
46
+ ## Install
47
+
48
+ PureML targets Python **3.11+** and NumPy **2.x**.
49
+
50
+ ```bash
51
+ # (Option A) From PyPI (when available)
52
+ pip install pureml
53
+
54
+ # (Option B) From source
55
+ git clone https://github.com/<you>/pureml.git
56
+ cd pureml
57
+ pip install -e .
58
+ ```
59
+
60
+ The only runtime deps are: `numpy`, `zarr` (plus lightweight helpers listed in `requirements.txt`).
61
+
62
+ ---
63
+
64
+ ## Quickstart: Train MNIST in a few lines
65
+
66
+ ```python
67
+ from pureml.models.neural_networks import MNIST_BEATER
68
+ from pureml.datasets import MnistDataset
69
+
70
+ # 1) Load data (train uses one-hot labels; test gives class indices)
71
+ with MnistDataset("train") as train, MnistDataset("test") as test:
72
+ # 2) Build the tiny network: Affine(784→256) → ReLU → Affine(256→10)
73
+ model = MNIST_BEATER().train()
74
+
75
+ # 3) Fit on the training set
76
+ model.fit(train, batch_size=128, num_epochs=5)
77
+
78
+ # 4) Switch to eval: model.predict returns class indices
79
+ model.eval()
80
+ # Example: run on one batch from the test set
81
+ X_test, y_test = test[:128]
82
+ preds = model(X_test)
83
+ print(preds.data[:10]) # class ids
84
+ ```
85
+
86
+ What you get out of the box:
87
+
88
+ - A tiny network that learns MNIST
89
+ - Clean logging of epoch loss
90
+ - An inference mode (`.eval()`) that returns class indices directly
91
+
92
+ ---
93
+
94
+ ## Core concepts
95
+
96
+ ### 1) Tensors & Autodiff
97
+ PureML wraps NumPy arrays in a small `Tensor` that records operations and exposes `.backward()` for gradients. The Tensor supports:
98
+ - Elementwise + matmul ops (`+ - * / **`, `@`, `.T`)
99
+ - Reshaping helpers like `.reshape(...)` and `.flatten(...)`
100
+ - Non-grad ops like `.argmax(...)`
101
+ - A `no_grad` context manager for inference/metrics
102
+
103
+ > The goal is **clarity**: gradients are implemented as explicit vector-Jacobian products (VJPs) you can read in one file.
104
+
105
+ ### 2) Layers
106
+ - **Affine (Linear)** — `Y = X @ W + b` (with sensible init)
107
+ - **Dropout**
108
+ - **BatchNorm1d** — with running mean/variance buffers and momentum
109
+
110
+ Layers expose:
111
+ - `.parameters` (trainables)
112
+ - `.named_buffers()` (non-trainable state)
113
+ - `.train()` / `.eval()` modes (with a per-layer `on_mode_change` hook)
114
+
115
+ ### 3) Losses
116
+ - `MSE`
117
+ - `BCE` (probabilities) and `Sigmoid+BCE` (logits)
118
+ - `CCE` (categorical cross-entropy; supports `from_logits=True`)
119
+
120
+ ### 4) Optimizers & Schedulers
121
+ - **SGD** (learning rate, optional momentum/weight decay depending on your version)
122
+ - Drop-in schedulers are easy to add; see the `optimizers` module.
123
+
124
+ ### 5) Data utilities
125
+ - Minimal `Dataset` protocol (`__len__`, `__getitem__`)
126
+ - `DataLoader` with batching, shuffling, slice fast-paths, and an optional seeded RNG
127
+ - Helpers like `one_hot(...)` and `multi_hot(...)`
128
+
129
+ ---
130
+
131
+ ## Saving & Loading
132
+
133
+ PureML provides two levels of persistence:
134
+
135
+ - **Parameters only** — compact save/load of learnable weights
136
+ - **Full state** — parameters **+** buffers **+** top-level literals (versioned), using zarr with Blosc(zstd) compression inside a `.zip`
137
+
138
+ ```python
139
+ # Save only trainable parameters
140
+ model.save("mnist_params")
141
+
142
+ # Save full state (params + buffers + literals) to .pureml.zip
143
+ model.save_state("mnist_full_state")
144
+
145
+ # Load later
146
+ model = MNIST_BEATER().eval().load_state("mnist_full_state.pureml.zip")
147
+ ```
148
+
149
+ ---
150
+
151
+ ## MNIST dataset included
152
+
153
+ The repo ships a compressed zarr archive of MNIST (uint8, 28×28). The `MnistDataset`:
154
+ - Normalizes images to `[0,1]` float32
155
+ - Uses one-hot labels for training mode
156
+ - Supports slicing and context-manager cleanup
157
+
158
+ ---
159
+
160
+ ## Why PureML?
161
+
162
+ - **Read the source, learn the math.** Every gradient is explicit and local.
163
+ - **Great for teaching & research notes.** Small enough to copy into slides or notebooks.
164
+ - **Fast enough for classic datasets.** Vectorized NumPy code + light I/O.
165
+
166
+ If you need GPUs, distributed training, or huge model zoos, you should use PyTorch/JAX. PureML is intentionally small.
167
+
168
+ ---
169
+
170
+ ## Roadmap
171
+
172
+ - More optimizers (Adam, RMSProp), LR schedulers
173
+ - Convolutional layers and pooling
174
+ - RNN/CNN examples and docs
175
+ - Better metrics & visualization utilities
176
+ - Typed public API + docstrings/docs site
177
+
178
+ ---
179
+
180
+ ## Contributing
181
+
182
+ PRs, issues, and discussion are welcome! Please include:
183
+ - A small, focused change
184
+ - Clear rationale (what/why)
185
+ - Tests where appropriate
186
+
187
+ ---
188
+
189
+ ## License
190
+
191
+ MIT — see `LICENSE` in this repo.
@@ -0,0 +1,159 @@
1
+ # PureML — a tiny, transparent deep-learning framework in NumPy
2
+
3
+ PureML is a learning-friendly deep-learning framework built entirely on top of **NumPy**. It aims to be **small, readable, and hackable** while still being practical for real experiments and teaching.
4
+
5
+ - **No hidden magic** — a minimal autodiff Tensor + clear VJPs
6
+ - **Batteries included** — core layers (Affine, Dropout, BatchNorm1d), common losses, an SGD optimizer, and a simple `DataLoader`
7
+ - **Self-contained dataset demo** — a ready-to-use MNIST reader and an end-to-end “MNIST Beater” model
8
+ - **Portable persistence** — zarr-backed `ArrayStorage` with zip compression for saving/loading model state
9
+
10
+ > If you like **scikit-learn’s** simplicity and wish **deep learning** felt the same way for small/medium projects, PureML is for you.
11
+
12
+ ---
13
+
14
+ ## Install
15
+
16
+ PureML targets Python **3.11+** and NumPy **2.x**.
17
+
18
+ ```bash
19
+ # (Option A) From PyPI (when available)
20
+ pip install pureml
21
+
22
+ # (Option B) From source
23
+ git clone https://github.com/<you>/pureml.git
24
+ cd pureml
25
+ pip install -e .
26
+ ```
27
+
28
+ The only runtime deps are: `numpy`, `zarr` (plus lightweight helpers listed in `requirements.txt`).
29
+
30
+ ---
31
+
32
+ ## Quickstart: Train MNIST in a few lines
33
+
34
+ ```python
35
+ from pureml.models.neural_networks import MNIST_BEATER
36
+ from pureml.datasets import MnistDataset
37
+
38
+ # 1) Load data (train uses one-hot labels; test gives class indices)
39
+ with MnistDataset("train") as train, MnistDataset("test") as test:
40
+ # 2) Build the tiny network: Affine(784→256) → ReLU → Affine(256→10)
41
+ model = MNIST_BEATER().train()
42
+
43
+ # 3) Fit on the training set
44
+ model.fit(train, batch_size=128, num_epochs=5)
45
+
46
+ # 4) Switch to eval: model.predict returns class indices
47
+ model.eval()
48
+ # Example: run on one batch from the test set
49
+ X_test, y_test = test[:128]
50
+ preds = model(X_test)
51
+ print(preds.data[:10]) # class ids
52
+ ```
53
+
54
+ What you get out of the box:
55
+
56
+ - A tiny network that learns MNIST
57
+ - Clean logging of epoch loss
58
+ - An inference mode (`.eval()`) that returns class indices directly
59
+
60
+ ---
61
+
62
+ ## Core concepts
63
+
64
+ ### 1) Tensors & Autodiff
65
+ PureML wraps NumPy arrays in a small `Tensor` that records operations and exposes `.backward()` for gradients. The Tensor supports:
66
+ - Elementwise + matmul ops (`+ - * / **`, `@`, `.T`)
67
+ - Reshaping helpers like `.reshape(...)` and `.flatten(...)`
68
+ - Non-grad ops like `.argmax(...)`
69
+ - A `no_grad` context manager for inference/metrics
70
+
71
+ > The goal is **clarity**: gradients are implemented as explicit vector-Jacobian products (VJPs) you can read in one file.
72
+
73
+ ### 2) Layers
74
+ - **Affine (Linear)** — `Y = X @ W + b` (with sensible init)
75
+ - **Dropout**
76
+ - **BatchNorm1d** — with running mean/variance buffers and momentum
77
+
78
+ Layers expose:
79
+ - `.parameters` (trainables)
80
+ - `.named_buffers()` (non-trainable state)
81
+ - `.train()` / `.eval()` modes (with a per-layer `on_mode_change` hook)
82
+
83
+ ### 3) Losses
84
+ - `MSE`
85
+ - `BCE` (probabilities) and `Sigmoid+BCE` (logits)
86
+ - `CCE` (categorical cross-entropy; supports `from_logits=True`)
87
+
88
+ ### 4) Optimizers & Schedulers
89
+ - **SGD** (learning rate, optional momentum/weight decay depending on your version)
90
+ - Drop-in schedulers are easy to add; see the `optimizers` module.
91
+
92
+ ### 5) Data utilities
93
+ - Minimal `Dataset` protocol (`__len__`, `__getitem__`)
94
+ - `DataLoader` with batching, shuffling, slice fast-paths, and an optional seeded RNG
95
+ - Helpers like `one_hot(...)` and `multi_hot(...)`
96
+
97
+ ---
98
+
99
+ ## Saving & Loading
100
+
101
+ PureML provides two levels of persistence:
102
+
103
+ - **Parameters only** — compact save/load of learnable weights
104
+ - **Full state** — parameters **+** buffers **+** top-level literals (versioned), using zarr with Blosc(zstd) compression inside a `.zip`
105
+
106
+ ```python
107
+ # Save only trainable parameters
108
+ model.save("mnist_params")
109
+
110
+ # Save full state (params + buffers + literals) to .pureml.zip
111
+ model.save_state("mnist_full_state")
112
+
113
+ # Load later
114
+ model = MNIST_BEATER().eval().load_state("mnist_full_state.pureml.zip")
115
+ ```
116
+
117
+ ---
118
+
119
+ ## MNIST dataset included
120
+
121
+ The repo ships a compressed zarr archive of MNIST (uint8, 28×28). The `MnistDataset`:
122
+ - Normalizes images to `[0,1]` float32
123
+ - Uses one-hot labels for training mode
124
+ - Supports slicing and context-manager cleanup
125
+
126
+ ---
127
+
128
+ ## Why PureML?
129
+
130
+ - **Read the source, learn the math.** Every gradient is explicit and local.
131
+ - **Great for teaching & research notes.** Small enough to copy into slides or notebooks.
132
+ - **Fast enough for classic datasets.** Vectorized NumPy code + light I/O.
133
+
134
+ If you need GPUs, distributed training, or huge model zoos, you should use PyTorch/JAX. PureML is intentionally small.
135
+
136
+ ---
137
+
138
+ ## Roadmap
139
+
140
+ - More optimizers (Adam, RMSProp), LR schedulers
141
+ - Convolutional layers and pooling
142
+ - RNN/CNN examples and docs
143
+ - Better metrics & visualization utilities
144
+ - Typed public API + docstrings/docs site
145
+
146
+ ---
147
+
148
+ ## Contributing
149
+
150
+ PRs, issues, and discussion are welcome! Please include:
151
+ - A small, focused change
152
+ - Clear rationale (what/why)
153
+ - Tests where appropriate
154
+
155
+ ---
156
+
157
+ ## License
158
+
159
+ MIT — see `LICENSE` in this repo.
@@ -0,0 +1,27 @@
1
+ # pureml/__init__.py
2
+ from .machinery import (
3
+ Tensor, TensorValuedFunction, GradientNotDefined,
4
+ is_grad_enabled, no_grad,
5
+ )
6
+ from .util import ArrayStorage, compose_steps, batches_of
7
+
8
+ # expose subpackages on demand
9
+ def __getattr__(name):
10
+ if name in {
11
+ "layers","activations","losses","optimizers",
12
+ "base","general_math","training_utils","datasets","models"
13
+ }:
14
+ import importlib
15
+ return importlib.import_module(f"{__name__}.{name}")
16
+ raise AttributeError(name)
17
+
18
+ __all__ = [
19
+ # core
20
+ "Tensor","TensorValuedFunction","GradientNotDefined",
21
+ "is_grad_enabled","no_grad",
22
+ # utils
23
+ "ArrayStorage","compose_steps","batches_of",
24
+ # namespaces (lazy) (see above)
25
+ "layers","activations","losses","optimizers",
26
+ "base","general_math","training_utils","datasets","models",
27
+ ]