Skip to main content

An implementation of an MLP classifier (with an interface of that of scikit-learn's MLPClassifier class.

Project description

Neural-Network-From-Scratch-COSC-221-CSB

Links:

Documentation and derivation:

Neural Network to classify handwritten digits (a rite of passage project at this point lol).

We will try to re-implement a stripped down version of the MLPClassifier class from scikit-learn from first principles. With this, we can then train a general classifier using the Multi-Layered Perceptron model.

To run

So since we've re-implemented an MLP using scikit-learn's MLPClassifier as a template, the API should be familiar.

Downloading the package

pip install mlpclassifier

Or create a virtual environment to download it.

Using uv:

uv add mlpclassifier

To use the library:

from classifier.classifier import *
# to load the MNIST dataset.
images = get_images_fast("dataset/train-images.idx3-ubyte")
labels = get_labels_fast("dataset/train-labels.idx1-ubyte")

test_images = get_images_fast("dataset/t10k-images.idx3-ubyte")
test_labels = get_labels_fast("dataset/t10k-labels.idx1-ubyte")

# pre-processing
X = images.reshape(images.shape[0], -1)/255
X_test = test_images.reshape(test_images.shape[0], -1)/255

model = MLPClassifier(
        hidden_layer_sizes=(128, 64),
        activation='relu',
        max_iter=1000,
        alpha=1e-3,
        batch_size=200,
        verbose=True,
)

SAVE_PATH = "weights/self_trained.pkl"

# if Ctrl-C, then it will save to that file path
model.fit(X, labels, save_path=SAVE_PATH)

N = 10_000
score, incorrect_indicies = model.score(X_test[:N], test_labels[:N])
print("score: ", score)
model.save(SAVE_PATH)

Dataset

Download the dataset from Kaggle

curl -L https://www.kaggle.com/api/v1/datasets/download/hojjatk/mnist-dataset -o ./dataset.zip

Then just unzip it into a directory called ./dataset

unzip -d dataset ./dataset.zip

Optional, but clean redundancy:

rm -r *-idx*-ubyte

I've removed some duplicates, so currently I have:

$ ls ./dataset/
 t10k-images.idx3-ubyte   train-images.idx3-ubyte
 t10k-labels.idx1-ubyte   train-labels.idx1-ubyte

So it seems like by convention:

  • we divide our dataset into training data, and then testing data
  • currently, it seems like we have 60k training examples and 10k testing examples
  • they do this to see how well the model has generalized

Reference model

For now, we'll use a reference model through scikit-learn.

TODO

  • debug all the row vector stuff
  • package it in pip
  • document the API

Forward propagation

  • variable L for layer
  • a list $n^{[l]}$ for the size at each layer
  • initialize using He's initalization
  • forward propagation step using that forward propagation formula

Backward propagation

  • He's initialization
  • back propagation
  • scoring
  • saving
  • [] make the learn rate $\alpha$ more adjustable

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlpclassifier-0.1.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlpclassifier-0.1.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file mlpclassifier-0.1.3.tar.gz.

File metadata

  • Download URL: mlpclassifier-0.1.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for mlpclassifier-0.1.3.tar.gz
Algorithm Hash digest
SHA256 295df0c934bfe0d72dadd29be8fa0177c64361f18c20ae6653174f838bb7f91d
MD5 2213a262cdc4db63e0d3579982f46d13
BLAKE2b-256 fb2c84b0135cb3fb64a8cfa442b1763e8b1ce0603247d989f9111cd0eab28de1

See more details on using hashes here.

File details

Details for the file mlpclassifier-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: mlpclassifier-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for mlpclassifier-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9436b3bdf34bed3f010b569ba8f8c1ad02cb673b5f5799e1548fb5db8c966ad4
MD5 1b933ad6e3974fdfc488a96878f5446a
BLAKE2b-256 560cab0f46052c1141dc969c181a038c9ffd0ab4bfed561e5c780a217ca674d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page