Skip to main content

Implementing minimal Pytorch for Learning

Project description

MiniTorch

Minitorch is a way of me learning about autograd and creating a pytorch like library.

Features

  1. Imports like Pytorch,
  2. Implements basic functions like backpropagation, gradient descent, neurons, linear layers
  3. Extensible with suport of custom modules like Python
  4. You can save the model weights and load the earlier model weights too

Remaining Task

  1. Vectorization
  2. Implementing CNNs, Transformers

I am working on theory of Jacobian Matrix and it's use for computing derivatives in a vector. So, will do this in V2.

Usage

  1. Install the necessary packages using
pip install -r requirements.txt
  1. Run the individual demo using
python demos/00-autograd.py

Development Usage

Use this if only you are planning to update the package. do not use this to run the demo.

Installation

  1. Install all the necessary requirements using;
pip install -r requirements.txt
  1. Build the minitorch package for local installation.
python -m build
  1. Install the minitorch package locally
pip install .

Running Demos

  1. Demos can be found in /demos, you can run individual of them by;
python -m ./demos/<example-file-name>.py
  1. If you want to create your own model, please look at documentation below.

Note that since, the minitorch library doesn't use vectorization things are not efficient hence, you cannot train the model on big data even like on MNIST. It becomes too slow. Try implementing some simpler stuffs.

Documentation

Value

  • This is similar to Pytorch Tensor.
from minitorch import Value

a = Value(0.0)
a.grad    # gradient of a
a.backward()    # backward propagate the value

You can create a complex arithmetic operation and do backward on top to see demo of backprop. See demos/00-autograd.py.

Creating a custom model

You can import

  • Linear
  • Neuron
  • Module
  • RNN
  • CrossEntropy
  • MSE
  • NLL

from minitorch.nn and create your own model.

Examples are at demos/01-or-gate.py, demos/02-custom-model.py.

Using activations and optimizers

Following activations;

  • Sigmoid
  • Softmax
  • ReLU
  • LogSoftmax
  • TanH

can be imported from minitorch.activations.

And you can also load optimizer, for now gradient descent only.

See example at; 05-simple-polynomial.py

Demo Video

Upload from GitHub

Screenshots

Activation functions Loss curve for simple polynomial function Loss curve for complex polynomial function

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

minitorch_ki8yk8-1.0.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

minitorch_ki8yk8-1.0.3-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file minitorch_ki8yk8-1.0.3.tar.gz.

File metadata

  • Download URL: minitorch_ki8yk8-1.0.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for minitorch_ki8yk8-1.0.3.tar.gz
Algorithm Hash digest
SHA256 121e62895c19f14fbd3e603c7c2a9181a1573390a3592c42ebf6429fd9188b43
MD5 b504b293753f6321a1d37827d0bd5b32
BLAKE2b-256 f51390a6dc9db2e3d4102909aae5b911965243ec308bace0db7bcd022b5fddd6

See more details on using hashes here.

File details

Details for the file minitorch_ki8yk8-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for minitorch_ki8yk8-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5300c140d50e493297ac1a90e7a8f240e861501e5aca18bd17d33f8166449c8c
MD5 79c9314538b494c6689ce9a287448a73
BLAKE2b-256 2efcb68e13b1e0b96567d5e580951c47a9f3d169dfd92d69e4a3d11c03e807bd

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