• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    Jupyter Notebook
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Quick, visual, principled introduction to pytorch code through five colab notebooks.

David's Tips on How to Read Pytorch

Figure thumbnails

These five python notebooks are an illustrated introduction to core pytorch idioms. Click below to run them on Colab.

  1. Tensor arithmetic: the notation for manipulating n-dimensional arrays of numbers on CPU or GPU.
  2. Autograd: how to get derivatives of any scalar with respect to any tensor input.
  3. Optimization: ways to update tensor parameters to reduce any computed objective, using autograd gradients.
  4. Network modules: how pytorch represents neural networks for convenient composition, training, and saving.
  5. Datasets and Dataloaders: for efficient multithreaded prefetching of large streams of data.

Pytorch is a numerical library that makes it very convenient to train deep networks on GPU hardware. It introduces a new programming vocabulary that takes a few steps beyond regular numerical python code. Although pytorch code can look simple and concrete, much of of the subtlety of what happens is invisible, so when working with pytorch code it helps to thoroughly understand the runtime model.

For example, consider this code:

torch.nn.cross_entropy(model(images.cuda()), labels.cuda()).backward()
optimizer.step()

It looks like it computes some function of images and labels without storing the answer. But actually the purpose of this code is to update some hidden parameters that are not explicit in this formula. This line of code moves batches of image and label data from CPU to the GPU; runs a neural network to make a prediction; constructs a computation graph describing how the loss depends on the network parameters; annotates every network parameter with a gradient; then finally it runs one step of optimization to adjust every parameter of the model. During all this, the CPU does not see any of the actual answers. That is intentional for speed reasons. All the numerical computation is done on the GPU asynchronously and kept there.

The berevity of the code is what makes pytorch code fun to write. But it also reflects why pytorch can be so fast even though the python interpreter is so slow. Although the main python logic slogs along sequentially in a single very slow CPU thread, just a few python instructions can load a huge amount of work into the GPU. That means the program can keep the GPU busy churning through massive numerical computations, for most part, without waiting for the python interpreter.

Is is worth understanding five idioms that work together to make this possible. The five notebooks in this directory are a quick overview of these idioms.

The key ideas are illustrated with small, runnable, tweakable examples, and there are links to other reference material and resources.

All the notebooks can be run on Google Colab where GPUs can be used for free. Or they can be run on your own local Jupyter notebook server. The examples should all work with python 3.5 or newer and pytorch 1.0 or newer.

Start with the first notebook here!

--- David Bau, July 2020

(David is a PhD student at MIT and former Google engineer. His research pursues transparency in deep networks.)

More Repositories

1

seedrandom

seeded random number generator for Javascript
JavaScript
1,955
star
2

rewriting

Rewriting a Deep Generative Model, ECCV 2020 (oral). Interactive tool to directly edit the rules of a GAN to synthesize scenes with objects added, removed, or altered. Change StyleGANv2 to make extravagant eyebrows, or horses wearing hats.
Python
537
star
3

dissect

Code for the Proceedings of the National Academy of Sciences 2020 article, "Understanding the Role of Individual Units in a Deep Neural Network"
Python
300
star
4

ganseeing

Seeing what a GAN cannot generate. Visualizes and quantifies object classes within scenes that are outside the range of a GAN.
Python
182
star
5

baukit

Python
37
star
6

covid-19-chart

Chart of current COVID-19 time series data. Enables a variety of county- state- and nation-level comparisons and data exploration.
HTML
18
star
7

see

see.js interactive eval panel that allows debugging of nested javascript scope.
JavaScript
16
star
8

sidn-handbook

The Structure and Interpretation of Deep Networks Handbook
Jupyter Notebook
12
star
9

xsrand

Several fast 32-bit xor-shift random number generators implemented in Javascript.
JavaScript
12
star
10

conformal

An interactive HTML conformal map viewer.
HTML
7
star
11

gpwidget

Jupyter Notebook
7
star
12

envs

Conda environments for pytorch-oriented deep learning work.
Shell
5
star
13

quick-netdissect

Lightweight reimplementation of netdissect for pytorch (under development)
Python
4
star
14

webcrush

A utility for minifying and LZW compressing an HTML file.
JavaScript
4
star
15

colabsecrets

A repo for keeping track of colab tricks.
Python
4
star
16

splaylist

Splay tree in Javascript, with fast indexing and order statistics. Better than a linked list.
JavaScript
4
star
17

make-full-linux-history

Yoann Padioleau's scripts to graft together a nearly complete history of linux commits as a git repo.
OCaml
3
star
18

miniplaces

baseline setup for alexnet on miniplaces using pytorch
Python
2
star
19

mandelbrot

Updated code for http://davidbau.com/mandelbrot/, an early HTML5 demo of the canvas element. See http://davidbau.com/archives/2009/09/27/mandelbrot.html
HTML
2
star
20

ganclass

Python
1
star
21

heidisudoku

JavaScript
1
star
22

panoptic-segmentation

Python
1
star
23

thisxsite

"This X does not exist" website
Python
1
star
24

pytorch-setup

Just to share a conda setup that I use
Shell
1
star
25

glowjs

A package containing the glowscript javascript libaries as a single javascript file.
JavaScript
1
star
26

sudokui

A simplified sudoku UI for experimenting with A/B testing: MIT 6.831
JavaScript
1
star
27

net-intent

Simple experiment to explore a jacobian method for determining hidden unit intent.
Python
1
star