Pure python implementation reverse-mode automatic differentiation

Related tags

Deep Learningminigrad
Overview

MiniGrad

A minimal implementation of reverse-mode automatic differentiation (a.k.a. autograd / backpropagation) in pure Python.

Inspired by Andrej Karpathy's micrograd, but with more comments and less cleverness. Thanks for the wonderful reference implementation and tests!

Overview

Create a Scalar.

a = Scalar(1.5)

Do some calculations.

b = Scalar(-4.0)
c = a**3 / 5
d = c + (b**2).relu()

Compute the gradients.

d.backward()

Plot the computational graph.

draw_graph(d)

Repo Structure

  1. demo.ipynb: Demo notebook of MiniGrad's functionality.
  2. tests.ipynb: Test notebook to verify gradients against PyTorch and JAX. Install both to run tests.
  3. minigrad/minigrad.py: The entire autograd logic in one (~100 loc) numeric class. See section below for details.
  4. minigrad/visualize.py: This just draws nice-looking computational graphs. Install Graphviz to run it.
  5. requirements.txt: MiniGrad requires no external modules to run. This file just sets up my dev environment.

Implementation

MiniGrad is implemented in one small (~100 loc) Python class, using no external modules.

The entirety of the auto-differentiation logic lives in the Scalar class in minigrad.py.

A Scalar wraps a float/int and overrides its arithmetic magic methods in order to:

  1. Stitch together a define-by-run computational graph when doing arithmetic operations on a Scalar
  2. Hard code the derivative functions of arithmetic operations
  3. Keep track of โˆ‚self/โˆ‚parent between adjacent nodes
  4. Compute โˆ‚output/โˆ‚self with the chain rule on demand (when .backward() is called)

This is called reverse-mode automatic differentiation. It's great when you have few outputs and many inputs, since it computes all derivatives of one output in one pass. This is also how TensorFlow and PyTorch normally compute gradients.

(Forward-mode automatic differentiation also exists, and has the opposite advantage.)

Not in Scope

This project is just for fun, so the following are not planned:

  • Vectorization
  • Higher order derivatives (i.e. Scalar.grad is a Scalar itself)
  • Forward-mode automatic differentiation
  • Neural network library on top of MiniGrad
Owner
Kenny Song
Research at UTokyo. Ex-Product @google.
Kenny Song
Team Enigma at ArgMining 2021 Shared Task: Leveraging Pretrained Language Models for Key Point Matching

Team Enigma at ArgMining 2021 Shared Task: Leveraging Pretrained Language Models for Key Point Matching This is our attempt of the shared task on Quan

Manav Nitin Kapadnis 12 Jul 08, 2022
Code for our CVPR 2022 Paper "GEN-VLKT: Simplify Association and Enhance Interaction Understanding for HOI Detection"

GEN-VLKT Code for our CVPR 2022 paper "GEN-VLKT: Simplify Association and Enhance Interaction Understanding for HOI Detection". Contributed by Yue Lia

Yue Liao 47 Dec 04, 2022
Discord Multi Tool that focuses on design and easy usage

Multi-Tool-v1.0 Discord Multi Tool that focuses on design and easy usage Delete webhook Block all friends Spam webhook Modify webhook Webhook info Tok

Lodi#0001 24 May 23, 2022
A PyTorch Library for Accelerating 3D Deep Learning Research

Kaolin: A Pytorch Library for Accelerating 3D Deep Learning Research Overview NVIDIA Kaolin library provides a PyTorch API for working with a variety

NVIDIA GameWorks 3.5k Jan 07, 2023
Continual Learning of Long Topic Sequences in Neural Information Retrieval

ContinualPassageRanking Repository for the paper "Continual Learning of Long Topic Sequences in Neural Information Retrieval". In this repository you

0 Apr 12, 2022
Source code for Transformer-based Multi-task Learning for Disaster Tweet Categorisation (UCD's participation in TREC-IS 2020A, 2020B and 2021A).

Source code for "UCD participation in TREC-IS 2020A, 2020B and 2021A". *** update at: 2021/05/25 This repo so far relates to the following work: Trans

Congcong Wang 4 Oct 19, 2021
Object DGCNN and DETR3D, Our implementations are built on top of MMdetection3D.

Object DGCNN & DETR3D This repo contains the implementations of Object DGCNN (https://arxiv.org/abs/2110.06923) and DETR3D (https://arxiv.org/abs/2110

Wang, Yue 539 Jan 07, 2023
Amazing-Python-Scripts - ๐Ÿš€ Curated collection of Amazing Python scripts from Basics to Advance with automation task scripts.

๐Ÿ“‘ Introduction A curated collection of Amazing Python scripts from Basics to Advance with automation task scripts. This is your Personal space to fin

Avinash Ranjan 1.1k Dec 29, 2022
Coursera - Quiz & Assignment of Coursera

Coursera Assignments This repository is aimed to help Coursera learners who have difficulties in their learning process. The quiz and programming home

ๆต…ๆขฆ 828 Jan 04, 2023
PushForKiCad - AISLER Push for KiCad EDA

AISLER Push for KiCad Push your layout to AISLER with just one click for instant

AISLER 31 Dec 29, 2022
ICLR 2021, Fair Mixup: Fairness via Interpolation

Fair Mixup: Fairness via Interpolation Training classifiers under fairness constraints such as group fairness, regularizes the disparities of predicti

Ching-Yao Chuang 49 Nov 22, 2022
Official implementation of "UCTransNet: Rethinking the Skip Connections in U-Net from a Channel-wise Perspective with Transformer"

[AAAI2022] UCTransNet This repo is the official implementation of "UCTransNet: Rethinking the Skip Connections in U-Net from a Channel-wise Perspectiv

Haonan Wang 199 Jan 03, 2023
YOLO-v5 ๊ธฐ๋ฐ˜ ๋‹จ์•ˆ ์นด๋ฉ”๋ผ์˜ ์˜์ƒ์„ ํ™œ์šฉํ•ด ์ฐจ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋ฉฐ ์ฃผํ–‰ํ•˜๋Š” Adaptive Cruise Control ๊ธฐ๋Šฅ ๊ตฌํ˜„

์ž์œจ ์ฃผํ–‰์ฐจ์˜ ์˜์ƒ ๊ธฐ๋ฐ˜ ์ฐจ๊ฐ„๊ฑฐ๋ฆฌ ์œ ์ง€ ๊ฐœ๋ฐœ Table of Contents ํ”„๋กœ์ ํŠธ ์†Œ๊ฐœ ์ฃผ์š” ๊ธฐ๋Šฅ ์‹œ์Šคํ…œ ๊ตฌ์กฐ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ ๊ฒฐ๊ณผ ์‹คํ–‰ ๋ฐฉ๋ฒ• ์ฐธ์กฐ ํŒ€์› ํ”„๋กœ์ ํŠธ ์†Œ๊ฐœ YOLO-v5 ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์•ˆ ์นด๋ฉ”๋ผ์˜ ์˜์ƒ์„ ํ™œ์šฉํ•ด ์ฐจ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋ฉฐ ์ฃผํ–‰ํ•˜๋Š” Adap

14 Jun 29, 2022
FairMOT for Multi-Class MOT using YOLOX as Detector

FairMOT-X Project Overview FairMOT-X is a multi-class multi object tracker, which has been tailored for training on the BDD100K MOT Dataset. It makes

Jonathan Tan 33 Dec 28, 2022
Framework for training options with different attention mechanism and using them to solve downstream tasks.

Using Attention in HRL Framework for training options with different attention mechanism and using them to solve downstream tasks. Requirements GPU re

5 Nov 03, 2022
Face2webtoon - Despite its importance, there are few previous works applying I2I translation to webtoon.

Despite its importance, there are few previous works applying I2I translation to webtoon. I collected dataset from naver webtoon ์—ฐ์• ํ˜๋ช… and tried to transfer human faces to webtoon domain.

์ด์ƒ์œค 64 Oct 19, 2022
Self-supervised Augmentation Consistency for Adapting Semantic Segmentation (CVPR 2021)

Self-supervised Augmentation Consistency for Adapting Semantic Segmentation This repository contains the official implementation of our paper: Self-su

Visual Inference Lab @TU Darmstadt 132 Dec 21, 2022
The missing CMake project initializer

cmake-init - The missing CMake project initializer Opinionated CMake project initializer to generate CMake projects that are FetchContent ready, separ

1k Jan 01, 2023
HODEmu, is both an executable and a python library that is based on Ragagnin 2021 in prep.

HODEmu HODEmu, is both an executable and a python library that is based on Ragagnin 2021 in prep. and emulates satellite abundance as a function of co

Antonio Ragagnin 1 Oct 13, 2021
Tensorflow implementation of "Learning Deep Features for Discriminative Localization"

Weakly_detector Tensorflow implementation of "Learning Deep Features for Discriminative Localization" B. Zhou, A. Khosla, A. Lapedriza, A. Oliva, and

Taeksoo Kim 363 Jun 29, 2022