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

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.2.tar.gz (9.7 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.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minitorch_ki8yk8-1.0.2.tar.gz
  • Upload date:
  • Size: 9.7 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.2.tar.gz
Algorithm Hash digest
SHA256 ca914446cf38e9a561199fe97d07dd13ed1e659859915824a57db47a76fb9cb8
MD5 b6961a1120e7c0c9b0d5c581eaa07bc0
BLAKE2b-256 ec91bba86cbfabf81198555aadef6ba7c5b1b3fd0be25da70e7e61b17798a830

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minitorch_ki8yk8-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e1c11fc24000fc4fd80469367a0487a417bf0504c23232e6f60475a3edbaefdf
MD5 fefe242d314085685c2af42aacc8670f
BLAKE2b-256 a3e07ec060c937e27616b849e1c921cfd9040c46d2c4e539bc6ef8f9e086d389

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