Cs231n lecture 8 tf and pytorch tutorial

* Note this is as of 2017, may be outdated.

CPU vs GPU (Appendix 3)

Deep Learning Framework

Allows you to easily build big computational graphs, easily compute gradients, and have optimizations done for you. Numpy can’t run on GPU and you have to compute your own gradients.

Tensorflow - Static computational graph, better for deployment

Appendix 1

You need to learn a new functional graph language to work with tf. However, The language is very optimized.

First define computational graph, then run the graph many times (Appendix 3) - Allows graph optimization.

Once graph is built, can serialize it and run it without the code that built the graph. You can train it in Python and deploy it in C++ or something.

Requires special tensorflow control flow operator to determine control logic in graph before we run it.

Requires special tensorflow control flow looping constraicts tf.foldl determine loop logic in graph before we run it (functional programming)

PyTorch- Dynamic computational graph, better for iteration

Appendix 2

You can use the imperative programming logic that you’re used to.

Creates new computataional graph each iteration (dynamic computational graph). Always need to keep the original code around if you want to keep using the graph.

However, code is a lot cleaner for conditionals and loops.

Recurrent Networks or Recursive Networks use different length graphs. This is harder to do in TF (static graph) than PyTorch (dynamic graph)

Facebook has Caffe2 for deployment, where you fill out a model template and then can deploy it.

High Level Comparison Between the Three

Screenshot 2020-02-26 at 1.19.38 PM.png

Appendices

Appendix 1:

Tensorflow

** **

Vanilla Tensorflow Code (copies tf.placeholder from CPU to GPU each call, inefficinent )

Screenshot 2020-02-26 at 1.20.59 PM.png

Screenshot 2020-02-26 at 1.21.43 PM.png

Screenshot 2020-02-26 at 1.22.06 PM.png

Screenshot 2020-02-26 at 1.23.02 PM.png** **

Screenshot 2020-02-26 at 1.23.20 PM.png** **

Screenshot 2020-02-26 at 1.23.26 PM.png** **

Screenshot 2020-02-26 at 1.23.39 PM.png** **

Screenshot 2020-02-26 at 1.25.03 PM.png

** **

Using tf.Variable to avoid copying from CPU to GPU each call

Screenshot 2020-02-26 at 1.25.14 PM.png** **

You have to use tf.assign to update tf.Variables

Screenshot 2020-02-26 at 1.25.45 PM.png** **

Screenshot 2020-02-26 at 1.26.07 PM.png** **

Screenshot 2020-02-26 at 1.26.31 PM.png** **

Adding Dummies to make sure that intermediate updates are computed

Tf will not compute any values not necessary to computing the outputs. Therefore, you have to make sure that everything that you want to compute is explicitly output (and set as ‘_’ ). This helps the tf optimizer know what to compute.

Screenshot 2020-02-26 at 1.27.20 PM.png** **

Use tf.train (GradientDescentOptimizer) instead of writing the update step yourself

Screenshot 2020-02-26 at 1.29.32 PM.png** **

Use tf.loss (mean_squared_error) instaed of computing the loss yourself

Screenshot 2020-02-26 at 1.30.45 PM.png** **

Use tf.layers to automatically set up weights (and bias), initializations, nonlinearity instaed of computing those yourself

Screenshot 2020-02-26 at 1.31.26 PM.png** **

Other Nice tf libraries

Screenshot 2020-02-26 at 1.32.33 PM.png

Screenshot 2020-02-26 at 1.33.45 PM.png

Screenshot 2020-02-26 at 1.34.04 PM.png

Screenshot 2020-02-26 at 1.34.09 PM.png** **

** **

Appendix 2:

Pytorch

Screenshot 2020-02-26 at 1.34.57 PM.png

Pytorch Tensors are basically numpy arrays that live in CPU/GPU (.cuda)

Screenshot 2020-02-26 at 1.35.19 PM.png

Screenshot 2020-02-26 at 1.36.01 PM.png

PyTorch Variabes are Nodes in a computational graph. They include a .data and a .grad attribute.

Pytorch Variables and Tensors have exactly the same API

Screenshot 2020-02-26 at 1.36.20 PM.png

Screenshot 2020-02-26 at 1.36.32 PM.png

Screenshot 2020-02-26 at 1.36.40 PM.png

Screenshot 2020-02-26 at 1.36.50 PM.png

Make sure you zero out the gradients before you recompute them!

Screenshot 2020-02-26 at 1.37.00 PM.png

You can make custom autograd functions in pytorch, although usually you won’t need to.

Screenshot 2020-02-26 at 1.37.23 PM.png

Screenshot 2020-02-26 at 1.37.40 PM.png

Use pytorch.nn as a high level wrapper for working with neural nets, like Keras

Screenshot 2020-02-26 at 1.37.50 PM.png

Screenshot 2020-02-26 at 1.38.16 PM.png

Screenshot 2020-02-26 at 1.38.22 PM.png

Screenshot 2020-02-26 at 1.38.36 PM.png

Use pytorch.optim to select different optimizers

Screenshot 2020-02-26 at 1.38.50 PM.pngScreenshot 2020-02-26 at 1.38.57 PM.png

Define Modules with torch.nn.module

Screenshot 2020-02-26 at 1.39.05 PM.png

Screenshot 2020-02-26 at 1.39.16 PM.png

Screenshot 2020-02-26 at 1.39.24 PM.png

Screenshot 2020-02-26 at 1.39.28 PM.png

Screenshot 2020-02-26 at 1.39.40 PM.png

Use DataLoader to get minibatching, shuffling, multithreading funcitonality

Screenshot 2020-02-26 at 1.39.58 PM.png

Screenshot 2020-02-26 at 1.40.11 PM.png

** **

Use Pytorch Pretrained Models (for vision it’s torchvision)

Screenshot 2020-02-26 at 1.40.20 PM.png

Screenshot 2020-02-26 at 1.40.31 PM.png

Tensorflow vs PyTorch: Static Graphs vs Dynamic Graphs. Loops and Conditionals

Screenshot 2020-02-26 at 1.41.33 PM.png

Screenshot 2020-02-26 at 1.44.50 PM.png

Screenshot 2020-02-26 at 1.47.16 PM.png

Appendix 3

CUDA - Write C-like code that runs directly on the GPU

OpenCL

Data Reads can be bottleneck in training. Solutions:

GPU has their own RAM on the chip** **

Screenshot 2020-02-26 at 1.07.49 PM.png

Screenshot 2020-02-26 at 1.12.33 PM.png

Using cuDNN optimizations gives 3x speed over vanilla CUDA