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.1.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.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minitorch_ki8yk8-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 1b7400ebd46c882121dea8837bbf64a47480d5748052a0bfbde02e50b73b7da3
MD5 ceb4803b286031d7000dbc25dbc5f0aa
BLAKE2b-256 037cc680e9dbb0f7039353fab7cbd53e80affb83a6936f13a343b879308077cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for minitorch_ki8yk8-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f098a60670717d247ee23f500fbae237fc71afff346efc2fce4e7f75def3cfcd
MD5 084aa7696e4781f0943aeaf99f7aa764
BLAKE2b-256 2c5bd20c8180f4d35992a5a717ae1ff7083d0e84f6023d3dd825be5443679f76

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