aboutsummaryrefslogtreecommitdiff
path: root/MultiLayerPerceptron/main.py
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-08-27 12:10:55 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-08-27 12:10:55 +0200
commit024b0ead1b0839d3591bad3bdd3a6ab6bd20c381 (patch)
treea13ec8234cdbe089b136d52ddd4d2f2fc5163d17 /MultiLayerPerceptron/main.py
parentaad7043d2f4ea303c30e9490554d66b75fb58f14 (diff)
Migrate to uv and Python 3.14 and add MNIST downloader to make the project runnableHEADmaster
Context / Problem: setup.py + requirements.txt (Python 3.5) no longer install on modern Python, matplotlib missing from deps, main.py save/load broke on numpy 2.x (inhomogeneous array / allow_pickle), and demo required manual mnist.npy/data files. What: replace setup.py/requirements.txt with pyproject.toml (hatchling, PEP 621, requires-python >=3.10, classifiers 3.10-3.14), add .python-version 3.14 and uv.lock, add MultiLayerPerceptron/download.py (mlp-download) to fetch MNIST from storage.googleapis and generate data/train.npy, data/test.npy, mnist.npy, fix MultiLayerPerceptron/main.py layers class var and pickle handling with minimal changes, ignore .venv and track uv.lock, update README with uv workflow. QA: verified uv sync --all-groups, uv run mlp-download, uv run python -m MultiLayerPerceptron.main loads and trains, ruff check passes.
Diffstat (limited to 'MultiLayerPerceptron/main.py')
-rw-r--r--MultiLayerPerceptron/main.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/MultiLayerPerceptron/main.py b/MultiLayerPerceptron/main.py
index 2dbfa3d..d7a3138 100644
--- a/MultiLayerPerceptron/main.py
+++ b/MultiLayerPerceptron/main.py
@@ -49,9 +49,8 @@ class Layer():
self.grad_weights = np.outer(self.grad_inter, input)
class MultiLayerPerceptron():
- layers = []
-
def __init__(self, layer_sizes):
+ self.layers = []
for k in range(len(layer_sizes)-1):
self.layers.append(Layer(layer_sizes[k], layer_sizes[k+1]))
@@ -60,7 +59,8 @@ class MultiLayerPerceptron():
@staticmethod
def load(filename):
- array = np.load(open(filename, 'rb'))
+ with open(filename, 'rb') as f:
+ array = np.load(f, allow_pickle=True)
sizes = [array[0].shape[1]]
for layer in array:
sizes.append(layer.shape[0])
@@ -70,8 +70,9 @@ class MultiLayerPerceptron():
return mlp
def save(self, filename):
- array = [layer.weights for layer in self.layers]
- np.save(open(filename, 'wb'), array)
+ array = np.array([layer.weights for layer in self.layers], dtype=object)
+ with open(filename, 'wb') as f:
+ np.save(f, array, allow_pickle=True)
def error(self, output, expected):
return 1/2 * np.sum((expected - output)**2)
@@ -171,10 +172,12 @@ if __name__ == "__main__":
mlp = MultiLayerPerceptron.load('mnist.npy')
print("OK")
print("[Loading] Training set: ", end="")
- space = np.load(open("data/train.npy", "rb"))
+ with open("data/train.npy", "rb") as f:
+ space = np.load(f, allow_pickle=True)
space = [(input.reshape(28*28), expected) for input, expected in space]
print("OK Testing set: ", end="")
- test_space = np.load(open("data/test.npy", "rb"))
+ with open("data/test.npy", "rb") as f:
+ test_space = np.load(f, allow_pickle=True)
test_space = [(input.reshape(28*28), expected) for input, expected in test_space]
print("OK")