Skip to main content

rawML

rawML is a hobby project where neural networks are implemented from scratch using pure Python and Numpy, with a class-based structure to define layers, optimizers, and loss functions. The goal is to implement how neural networks work at a low level, and create a (somewhat) modular custom code framework to implement ML algorithms, without relying on high-level deep learning frameworks like TensorFlow or PyTorch. rawML is a hobby project where neural networks are implemented from scratch using pure Python and Numpy, with a class-based structure to define layers, optimizers, and loss functions. The goal is to implement how neural networks work at a low level, and create a (somewhat) modular custom code framework to implement ML algorithms, without relying on high-level deep learning frameworks like TensorFlow or PyTorch. In a nutshell, my lite version of PyTorch/TF, powered by NumPy.

Overview of current implementations:

  • Linear Layers: Fully connected layers with He-initialization of weights.
  • Activation Functions: ReLU activation implemented using numpy.
  • Custom Tensor Class: jTensor, an extension of Numpy's ndarray, supports storing gradients in the .gd attribute.
  • Optimization: Gradient Descent Optimizer (GDOptimizer) is implemented with learning rate control.
  • Loss Function: Mean Squared Error (MSE) Loss is implemented to compute the loss during training.
  • Model Class: The CreateModel class stiches all layers together, providing methods for forward passes and training with backpropagation. I plan on making it more customizable.
  • Other basic functionality: General essential functions, like mean, min, max, std, rand, randn etc are implemented in the rawML library. All are powered by NumPy.

Requirements

just numpy :)

pip install numpy

Usage Instructions

Just clone this repository. In the cloned folder, create your python file, and start using away! (the rawML folder is essential. Copy it wherever you wish, and import from it)

Code Example

The demo.py file shows how one can define a rML model, define the loss function, optimzer and stich it together. Model training and inference examples are also shown.

import rawML as rML
from rawML.layers import relu, linear
from rawML.optimizers import GDOptimizer
from rawML.losses import MSELoss
from sklearn.model_selection import train_test_split as tts

LayerList = [
    linear(100,20),
    relu(),
    linear(20,40)
]

opt = GDOptimizer(lr = 1e-2)
loss = MSELoss()
model = rML.createModel(LayerList, opt, loss)

X = rML.rand((16, 100))
Y = rML.rand((16, 40))

##sklearn train-test-split works with jTensors
x_train, x_val, y_train, y_val = tts(X,Y,train_size=0.8)

#Training
model.train((x_train,y_train),(x_val,y_val),epochs=20,verbose_freq=5)

#Predicting
y = model(rML.randn([40,100]))
print(y.shape)
  • As jTensors inherit(and behave very similar to) from numpy arrays, they support operations like .shape, and they can also be fed into scikit-learn's train test split

Future Implementations

This project is at a very initial stage, and I aim to expand it further. I will add implementations of more Optimizers, Loss functions along with other layers like MaxPool2D, CNNs. There is no implementation of the concept of "batch size" which will be added very soon. An easier way to add more custom metrics will also be implemented into the model.train() method. Verbose control will be added Currently, the CreateModel class is restrictive to a sequential NN, which I plan on changing by implementing a more "functional" NN, to make more complex architectures like skip connections etc. The further aim to implement the famed UNet architecture using RawML. I also plan to explore GPU acceleration possibilities by migrating to CuPy instead of NumPy and etc...

(PS, there exists an rML.about())

Metadata

Release files for rawML 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rawML 0.1.1
File Size Uploaded
rawml-0.1.1.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rawML 0.1.1
File Interpreter ABI Platform
rawML-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / rawml-0.1.1.tar.gz

Download URL rawml-0.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
921e5170c61a6783394a99b9024e7ab5ed8b8d40a97bea4d3c85c4837d921274
BLAKE2b-256 checksum
How to use checksums
631b6ed0ed4d89075cc3f60c2b70edad49ac4f0eaf9daf6bb800c7e8551b2086
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release files / rawML-0.1.1-py3-none-any.whl

Download URL rawML-0.1.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
996ab329b04093b207ce867f4dff631cbd14146bbb3617ed4f5e4c071fbfe544
BLAKE2b-256 checksum
How to use checksums
550e95e2f4c291aac498336f2e1dd8bc37ec8805ca130f97238ad4edaf4d870e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page